Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TwoButtons extends JFrame implements ActionListener
- {
- JButton redButton ;
- JButton grnButton ;
- JButton bluButton ;
- JButton gryButton ;
- public TwoButtons(String title)
- {
- super( title );
- redButton = new JButton("Red");
- grnButton = new JButton("Green");
- bluButton = new JButton("Blue");
- gryButton = new JButton("Gray");
- redButton.setActionCommand( "red" );
- grnButton.setActionCommand( "green" );
- bluButton.setActionCommand( "blue" );
- gryButton.setActionCommand( "gray" );
- redButton.addActionListener( this );
- grnButton.addActionListener( this );
- bluButton.addActionListener( this );
- gryButton.addActionListener( this );
- setLayout( new FlowLayout() );
- add( redButton );
- add( grnButton );
- add( bluButton );
- add( gryButton );
- setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- }
- public void actionPerformed( ActionEvent evt)
- {
- if ( evt.getActionCommand().equals( "red" ) )
- getContentPane().setBackground( Color.red );
- else if ( evt.getActionCommand().equals( "green" ) )
- getContentPane().setBackground( Color.green );
- else if ( evt.getActionCommand().equals( "blue" ) )
- getContentPane().setBackground( Color.blue );
- else
- getContentPane().setBackground( Color.gray );
- repaint();
- }
- public static void main ( String[] args )
- {
- TwoButtons demo = new TwoButtons( "Click a Button" ) ;
- demo.setSize( 200, 150 );
- demo.setVisible( true );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement