import javax.swing.*; import java.awt.*; import listeners.*; /** * Basic GUI example that demonstrates multiple layouts with JPanels * This class has no event handling. Therefore, if you close this Frame * the program will still run. */ public class SimpleGUI extends JFrame { private FrameListener fListener; private ButtonListener bListener; public SimpleGUI() { super( "Simple GUI Example" ); JPanel mainPanel = new JPanel(); JPanel userPanel = new JPanel(); // username panel JPanel passPanel = new JPanel(); // password panel JPanel buttonPanel = new JPanel(); // button panel JTextField userTxtFld = new JTextField( 30 ); JPasswordField passFld = new JPasswordField( 30 ); // NOTICE -- Swing labels can parse and interpret html code JLabel userLbl = new JLabel( "Username:" ); JLabel passLbl = new JLabel( "Password:" ); JButton okBtn = new JButton( "OK" ); JButton cancelBtn = new JButton( "CANCEL" ); userPanel.add( userLbl ); userPanel.add( userTxtFld ); passPanel.add( passLbl ); passPanel.add( passFld ); buttonPanel.add( okBtn ); buttonPanel.add( cancelBtn ); // We use a gridlayout to set the components mainPanel.setLayout( new GridLayout( 3, 1 ) ); mainPanel.add( userPanel ); mainPanel.add( passPanel ); mainPanel.add( buttonPanel ); getContentPane().add( mainPanel ); // all components for JFrame, JWindow, and JDialog // are added to container called the ContentPane.1 // Add Event Listeners fListener = new FrameListener(); bListener = new ButtonListener( this, userTxtFld, passFld ); addWindowListener( fListener ); okBtn.addActionListener( bListener ); cancelBtn.addActionListener( bListener ); setSize( 500, 500 ); pack(); // set all components to their preferred size } public static void main( String[] args ) { SimpleGUI gui = new SimpleGUI(); gui.setVisible( true ); } }