Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class GUI {
- private JFrame frame;
- private JButton button;
- public static void main(String[] args){
- SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- GUI gui = new GUI();
- gui.setup();
- }
- });
- }
- public GUI(){
- frame = new JFrame("SET COLOR");
- button = new JButton("CHANGE");
- button.addActionListener(new ColorChanged(frame));
- }
- public void setup(){
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setSize(500,500);
- frame.setResizable(false);
- GridLayout layout = new GridLayout(7,1);
- frame.setLayout(layout);
- frame.add(button);
- frame.getContentPane().setBackground(Color.orange);
- }
- /*
- class colorChanged implements ActionListener{
- public void actionPerformed(ActionEvent evt){
- frame.getContentPane().setBackground(Color.cyan);
- }
- }
- */
- }
- /***************** CLASSE ESTERNA **********************/
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JFrame;
- public class ColorChanged implements ActionListener {
- private JFrame frame;
- public ColorChanged(JFrame frame){
- this.frame = frame;
- }
- public void actionPerformed(ActionEvent ext){
- if(frame.getContentPane().getBackground()== Color.orange)
- frame.getContentPane().setBackground(Color.cyan);
- else
- frame.getContentPane().setBackground(Color.orange);
- }
- }
Add Comment
Please, Sign In to add comment