天天看點

java panel 背景色_java 面闆背景色

題目要求單擊按鈕,改變面闆背景色,下面是我寫的2個類,不能得到要求的結果,高手幫我改下,謝謝了類一:importjavax.swing.JFrame;importjavax.swing.JButton;importjavax.swing.JP...

題目要求單擊按鈕,改變面闆背景色,下面是我寫的2個類,不能得到要求的結果,高手幫我改下,謝謝了 類一: import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; public class Test4 extends JFrame{ Test4() { super("單擊按鈕,改變視窗背景顔色"); setSize(200,150); setLocation(400,300); JButton jb=new JButton("按鈕"); JPanel jp=new JPanel(); jp.add(jb); add(jp); ChangeWindowColor c=new ChangeWindowColor(this); jb.addActionListener(c); } public static void main(String[] args) { Test4 f=new Test4(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } 類二: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Color; import java.awt.Container; public class ChangeWindowColor implements ActionListener{ Test4 f; ChangeWindowColor(Test4 f) { this.f=f; } public void actionPerformed(ActionEvent e) { Container con=new Container(); con=f.getContentPane(); con.setBackground(Color.RED); f.setTitle("change color"); } }

展開