我在四个不同的面板上各有四个按钮。如果我按下按钮,我希望它所在的面板改变颜色。问题是我只知道如何为一个按钮而不是所有四个按钮执行此操作。到目前为止,这是我的代码...public class tester implements ActionListener{JPanel B;JPanel A;public static void main(String[]args){ new tester();}public void tester(){ JFrame test = new JFrame("tester:"); B = new JPanel(); A= new JPanel(); JPanel cc = new JPanel(); JPanel dd = new JPanel(); JButton b = new JButton("ButtonB"); JButton a = new JButton("ButtonA"); JButton c = new JButton("ButtonC"); JButton d = new JButton("ButtonD"); test.setLayout(new GridLayout(2,2)); test.setSize(600,500); B.setBackground(Color.BLUE); A.setBackground(Color.RED); cc.setBackground(Color.BLACK); dd.setBackground(Color.WHITE); B.add(b); A.add(a); cc.add(c); dd.add(d); test.add(A); test.add(B); test.add(cc); test.add(dd); test.setVisible(true); b.addActionListener(this); a.addActionListener(this);}public void actionPerformed(ActionEvent e){ B.setBackground(Color.PINK);}}
添加回答
举报
0/150
提交
取消