Here's an example of the Java Code to change the color of the background -
import java.awt.*;
import java.awt.event.*;
Code:
import javax.swing.*;
public class BackgroundFrame extends JFrame
implements ActionListener {
private WindowTerminator wt = new WindowTerminator(); // Code below
private JButton red,blue; // the JButtons which will cause the background colors to change
private Container cp; // our contentpane
public BackgroundFrame( ) {
setSize(300,300);
setLocation(100,100);
setTitle(getClass().getName());
addWindowListener(wt);
red = new JButton("Red");
blue = new JButton("Blue");
red.addActionListener(this);
blue.addActionListener(this);
cp = getContentPane();
cp.setBackground(Color.pink);
cp.setLayout(new FlowLayout());
cp.add(red);
cp.add(blue);
show();
}
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource(); //get the source of the event
if ( s == red) cp.setBackground(Color.red);
else if ( s == blue) cp.setBackground ( Color.blue);
cp.repaint();
}
public static void main (String[] args) {
JFrame f = new BackgroundFrame();
}
}
class WindowTerminator extends WindowAdapter {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
}
Bookmarks