Java JFrame set background

Description

Java JFrame set background

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
  JButton red, green, blue;//from  w ww .  j a  v a 2  s .c om

  public Main() {
    super("ColorFrame");
    setSize(322, 122);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    red = new JButton("Red");
    add(red);
    green = new JButton("Green");
    add(green);
    blue = new JButton("Blue");
    add(blue);
    // begin anonymous inner class
    ActionListener act = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        if (event.getSource() == red) {
          getContentPane().setBackground(Color.RED);
        }
        if (event.getSource() == green) {
          getContentPane().setBackground(Color.GREEN);
        }
        if (event.getSource() == blue) {
          getContentPane().setBackground(Color.BLUE);
        }
      }
    };
    // end anonymous inner class
    red.addActionListener(act);
    green.addActionListener(act);
    blue.addActionListener(act);
    setVisible(true);
  }

  public static void main(String[] arguments) {
    new Main();
  }
}



PreviousNext

Related