john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

swing awt simple gui button framelistener closing window adapter

// Adapter class saves the work of having to implement all of the empty methods in an Interface

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class FrameListener extends WindowAdapter
{
    public void windowClosing( WindowEvent e )
    {   System.exit( 0 );
    }
}


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

public class Listener implements ActionListener
{
    GUI x;

    Listener( GUI a )
    {
        x = a;
    }

    public void actionPerformed( ActionEvent e )
    {
        if( e.getActionCommand().equals( "buttonA") )
        {
            x.buttonA.setText("press me!");
            x.setTitle( "buttonA " );
        }
        System.out.println( e.getActionCommand() );
    } //end actionPerformed()
} //end class


import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JButton;

public class GUI extends JFrame
{
    private static final long serialVersionUID = 1L;
    JButton buttonA = new JButton( "buttonA" );
    Listener listen = new Listener( this );
    FrameListener frameListener = new FrameListener();

    GUI()
    {
        Container c = getContentPane();
        c.setLayout( null );        //requires absolute layout definition
        c.add( buttonA );
        buttonA.setBounds( 10 , 10 , 120 , 70 );
        buttonA.addActionListener( listen );
        this.addWindowListener( frameListener );

        setSize( 250 , 250 );
        setTitle( "Button Press" );
        setVisible( true );
        setResizable( false );
    }
} //end class


// MAIN
public class GUIMain
{
    @SuppressWarnings("unused")
    public static void main( String[] args )
    {
        GUI x = new GUI();
    }
} //end class

  • « BAT wget silent msi install dir find
  • DumpInstalledComponentList.vbs »

Published

Aug 25, 2011

Category

java

~147 words

Tags

  • adapter 1
  • awt 3
  • button 10
  • closing 2
  • framelistener 2
  • gui 34
  • java 252
  • simple 11
  • swing 9
  • window 6