I need some help with a GUI project.
What I want to do is when someone select the File Menu and chose New a Jpanel will open.
I have tried to write the code which is called class labelListener implements ActionListener. The Bold font is where I need help in...
Here is the code I have so far
public class GUIMenu extends JFrame {
Expand|Select|Wrap|Line Numbers
- public GUIMenu() {
- super("Menus");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Build first menu in the menu bar.
- JMenuItem f1 = new JMenuItem("New");
- f1.setMnemonic(KeyEvent.VK_N);
- f1.getAccessibleContext().setAccessibleDescription(
- "If this was working you could open a new file");
- JMenuItem f2 = new JMenuItem("Open");
- f2.setMnemonic(KeyEvent.VK_O);
- f2.addActionListener(new labelListener());//loads/sets the listener
- JMenuItem f3 = new JMenuItem("Exit");
- f3.setMnemonic(KeyEvent.VK_E);
- f3.addActionListener(new exitListener());//loads/sets the listener
- //Build second menu in the menu bar.
- JMenuItem e1 = new JMenuItem("Copy");
- e1.setMnemonic(KeyEvent.VK_C);
- JMenuItem e2 = new JMenuItem("Cut");
- e2.setMnemonic(KeyEvent.VK_X);
- JMenuItem e3 = new JMenuItem("Paste");
- e3.setMnemonic(KeyEvent.VK_P);
- //Build third menu in the menu bar.
- JMenuItem a1 = new JMenuItem("Close");
- a1.setMnemonic(KeyEvent.VK_D);
- a1.addActionListener(new exitListener());//loads/sets the listener
- //Create the menu bar.
- JMenuBar menubar = new JMenuBar();
- //Build the first menu.
- JMenu menu = new JMenu("File");
- menu.add(f1);
- menu.add(f2);
- menu.addSeparator();
- menu.add(f3);
- menubar.add(menu);
- //Build the Second menu.
- JMenu menu2 = new JMenu("Edit");
- menu2.add(e1);
- menu2.add(e2);
- menu2.add(e3);
- menubar.add(menu2);
- //Build the Third menu.
- JMenu menu3 = new JMenu("Close the program");
- menu3.add(a1);
- menubar.add(menu3);
- // prepare user interface
- JTextArea edit = new JTextArea(8, 40);
- JScrollPane scroll = new JScrollPane(edit);
- BorderLayout bord = new BorderLayout();
- setLayout(bord);
- add("Center", scroll);
- setJMenuBar(menubar);
- pack();
- setVisible(true);
- }
- //inner class Section used for exit on menu 3 for fun
- class exitListener implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- int x = JOptionPane.showOptionDialog(GUIMenu.this, "Exit Program?",
- "Exit Request", JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE, null, null,
- JOptionPane.NO_OPTION);
- if (x == JOptionPane.YES_OPTION) {
- GUIMenu.this.dispose();
- }//close the if statement
- }//close public void actionPerformed
- }//close the class exitListener
- class labelListener implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };
- JPanel JPanelPane;
- JPanel x = JPanelPane = new JPanel();
- JLabel formatLabel = new JLabel("Output formats:");
- rootPane.add(formatLabel);
- JComboBox formatBox = null;
- for (int i = 0; i < formats.length; i++)
- formatBox.addItem(formats[i]);
- rootPane.add(formatBox);
- add(rootPane);
- setVisible(true);
- {
- GUIMenu.this.dispose();
- }
- }
- }
- public static void main(String[] arguments) {
- GUIMenu frame = new GUIMenu();
- }
- }
thanks
sandyw