import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.Graphics;
import java.io.*;
import javax.imageio.*;
//商店类
public class w18{
JFrame j=new JFrame();
Container c=j.getContentPane();
public w18(){
//面板布局
j.setLayout(null);
//面板按钮
Icon kj1=new ImageIcon("sd3.png");
Icon kj2=new ImageIcon("kj.png");
JPanel j1=new JPanel();
JButton w1=new JButton(" 装 备 ");
JButton w2=new JButton(" 药 剂 ");
JButton w3=new JButton(" 材 料 ");
JButton w4=new JButton(" 道 具 ");
j1.setLayout(new GridLayout(1,4,0,0));
j1.setBounds(0,0,285,30);
j1.add(w1);
j1.add(w2);
j1.add(w3);
j1.add(w4);
j.add(j1);
//面板1
JButton a1[]=new JButton[40];
JPanel j11=new JPanel();
j11.setPreferredSize(new Dimension(265,700));
j11.setLayout(new GridLayout(20,2,1,1)); {for(int i=0;i<40;i++){
a1[i]=new JButton(kj2);
j11.add(a1[i]);
}}
JScrollPane s1=new JScrollPane(j11);
s1.setBounds(0,30,285,410);
//左右
//s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//左右
//上下
s1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//上下
j.add(s1);
//面板2
JButton a2[]=new JButton[40];
JPanel j12=new JPanel();
j12.setPreferredSize(new Dimension(265,700));
j12.setLayout(new GridLayout(20,2,1,1)); {for(int i=0;i<40;i++){
a2[i]=new JButton(kj1);
j12.add(a2[i]);
}}
JScrollPane s2=new JScrollPane(j12);
s2.setBounds(0,30,285,410);
//左右
//s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//左右
//上下
s2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//上下
//面板3
JButton a3[]=new JButton[40];
JPanel j13=new JPanel();
j13.setPreferredSize(new Dimension(265,700));
j13.setLayout(new GridLayout(20,2,1,1)); {for(int i=0;i<40;i++){
a3[i]=new JButton(kj2);
j13.add(a3[i]);
}}
JScrollPane s3=new JScrollPane(j13);
s3.setBounds(0,30,285,410);
//左右
//s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//左右
//上下
s3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//上下
//面板4
JButton a4[]=new JButton[40];
JPanel j14=new JPanel();
j14.setPreferredSize(new Dimension(265,700));
j14.setLayout(new GridLayout(20,2,1,1)); {for(int i=0;i<40;i++){
a4[i]=new JButton(kj1);
j14.add(a4[i]);
}}
JScrollPane s4=new JScrollPane(j14);
s4.setBounds(0,30,285,410);
//左右
//s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//左右
//上下
s4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//上下
//列 行
j.setVisible(true);
j.setSize(300,475);
j.setLocationRelativeTo(null);//窗体居中
j.setDefaultCloseOperation(2);//窗体关闭方式
j.setResizable(false);//用户禁止拉伸窗体
//面板按钮的动作事件
w1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j.add(s1);
s1.revalidate();
}
});
w2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j.add(s2);
s2.revalidate();
}
});
w3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j.add(s3);
s3.revalidate();
}
});
w4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j.add(s4);
s4.revalidate();
}
});
}
public static void main(String[]args){
new w18();
}//主方法
}
如上代码,实现点击按钮切换面板,但是我每个按钮第一次点击切换没问题,后面点击就没反应了,而且鼠标放在面板上会有覆盖效果,求指教