Studyon Minte9.com
Java SE




Action



         // Menu actions
         // Default action


Menu actions (switch)

package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 600, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem menuItemNew = new JMenuItem("New"); JMenu menuOpen = new JMenu("Open"); JMenuItem submenuOpen1 = new JMenuItem("Open submenu"); menuFile.setMnemonic('F'); menuItemNew.setMnemonic('N'); menuOpen.setMnemonic('O'); menuBar.add(menuFile); menuFile.add(menuItemNew); menuFile.add(menuOpen); menuOpen.add(submenuOpen1); setJMenuBar(menuBar); // --- Look here --- // menuItemNew.addActionListener(new MenuAction(new Action1())); submenuOpen1.addActionListener(new MenuAction(new Action2())); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } // --- Look here --- // @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { getContentPane().removeAll(); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } } // --- Look here --- // class Action1 extends JPanel { public Action1() { setBackground(Color.gray); JLabel message = new JLabel(); message.setPreferredSize(new Dimension(440, 27)); message.setForeground(Color.white); message.setBorder(BorderFactory.createLineBorder(Color.white)); message.setText("Action 1"); add(message); } } // --- Look here --- // class Action2 extends JPanel { public Action2() { setBackground(Color.black); JLabel message = new JLabel(); message.setForeground(Color.white); message.setBorder(BorderFactory.createLineBorder(Color.red)); message.setText("Action 2"); add(message); } }
  Default action (on window load)
package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 600, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() { initMenu(); // --- Look here --- // // default action this.addWindowListener( new WindowAdapter() { @Override public void windowOpened( WindowEvent e ){ changePanel(new Action2()); } }); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem menuItemNew = new JMenuItem("New"); JMenuItem menuItemOpen = new JMenuItem("Open"); menuFile.setMnemonic('F'); menuBar.add(menuFile); menuFile.add(menuItemNew); menuFile.add(menuItemOpen); setJMenuBar(menuBar); menuItemNew.addActionListener(new MenuAction(new Action1())); menuItemOpen.addActionListener(new MenuAction(new Action2())); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { getContentPane().removeAll(); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } } class Action1 extends JPanel { public Action1() { setBackground(Color.gray); JLabel message = new JLabel(); message.setPreferredSize(new Dimension(440, 27)); message.setForeground(Color.white); message.setBorder(BorderFactory.createLineBorder(Color.white)); message.setText("Action 1"); add(message); } } class Action2 extends JPanel { public Action2() { setBackground(Color.black); JLabel message = new JLabel(); message.setForeground(Color.white); message.setBorder(BorderFactory.createLineBorder(Color.red)); message.setText("Action 2"); add(message); } }