import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.BorderFactory; import javax.swing.UIManager; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.GridLayout; public class frame_c extends JFrame { public frame_c( String titel ) { super( titel ); this.num_lbl = new JLabel( frame_c.num_prefix + "0 " ); JButton count_btn = new JButton( "Jag är Swingknapp!" ); count_btn.setMnemonic( 'J' ); count_btn.addActionListener( new count_btn_listener() ); // ActionListener obj JPanel panel = new JPanel(); panel.setBorder( BorderFactory.createEmptyBorder( 30, // top 30, // left 10, // bottom 30 ) ); // right panel.setLayout( new GridLayout( 0, // rows, 0 = hur många som helst 1)); // cols panel.add( count_btn); panel.add( this.num_lbl); super.getContentPane().add( panel ); super.pack(); super.addWindowListener( new window_exit_listener() ); //WindowListener obj } // frame_c private class count_btn_listener implements ActionListener { public void actionPerformed( ActionEvent aev ) { handle_count_btn(); } } // count_btn_listener private class window_exit_listener extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // window_exit_listener private void handle_count_btn() { this.num_clicks++; this.num_lbl.setText( this.num_prefix + this.num_clicks ); } // handle_count_btn private static String num_prefix = "Antal klickningar: "; private JLabel num_lbl; private int num_clicks = 0; public static void main(String[] args) { try { if ( args[0].equalsIgnoreCase( "W" ) ) { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } else { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } // if } // try catch (Exception e) { } // catch frame_c frame = new frame_c( "SwingApplikation"); frame.setVisible(true); } // main } // frame_c