// This code example is created for educational purpose // by Thorsten Thormaehlen (contact: www.thormae.de). // It is distributed without any warranty. import javax.swing.*; import java.awt.event.*; class MyGui extends JFrame implements ActionListener{ private int counter = 0; JLabel label = new JLabel("Hello World"); public void createGUI() { setTitle("HelloGUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(320, 240); setLayout(null); label.setBounds(120, 0, 150, 30); getContentPane().add(label); JButton button = new JButton("Increment"); button.addActionListener(this); button.setBounds(120, 30, 150, 30); getContentPane().add(button); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Counter"); menu.setMnemonic(KeyEvent.VK_C); menuBar.add(menu); JMenuItem item = new JMenuItem("Increment", KeyEvent.VK_I); item.addActionListener(this); menu.add(item); JMenuItem item2 = new JMenuItem("Reset", KeyEvent.VK_R); item2.addActionListener(this); item2.setActionCommand("ResetCmd"); menu.add(item2); setJMenuBar(menuBar); setVisible(true); } public void actionPerformed(ActionEvent event) { counter++; if(event.getActionCommand().equals("ResetCmd") ) counter = 0; label.setText("Click #" + Integer.toString(counter)); } } public class ActionMenu { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MyGui myGUI = new MyGui(); myGUI.createGUI(); } }); } }