2
改變我有一個由兩個JPanels
JPanel的顏色從另一個
Panel1
包含一個按鈕,當按鈕被點擊我要的是Panel2在顏色改變,併成爲紅色的框架,這沒」牛逼發生
因此,任何幫助,並解釋如何在面板的顏色從另一個面板中更改
public class MyForm extends JFrame {
public MyForm() {
// TODO Auto-generated constructor stub
super();
// setLayout(new FlowLayout());
Panel1 panel1 = new Panel1();
add(panel1, BorderLayout.NORTH);
Panel2 panel2 = new Panel2();
add(panel2, BorderLayout.CENTER);
}
class Panel1 extends JPanel {
public Panel1() {
// TODO Auto-generated constructor stub
JButton btn = new JButton("Change Color");
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Sqsq");
Panel2 panel2 = new Panel2();
panel2.setBackground(Color.red);
panel2.repaint();
}
});
}
}
class Panel2 extends JPanel {
public Panel2() {
// TODO Auto-generated constructor stub
super();
setBackground(Color.black);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
}
}
public static void main(String[] args) {
MyForm form = new MyForm();
// form.setLocationRelativeTo(null);
form.setSize(500, 500);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);
}
}
打我吧:) –
好工作!謝謝 – HADEV