Sunday, April 15, 2012

Swing Menus

This code represents a simple example of how to use and implement Swing Menus to open additional forms. An alternate way, that doesn't use multiple forms but rather switches out JPanels in a single frame is the CardLayout. You may want to explore this in the documentation and with on-line examples.

Here are pictures of the programs menus and forms running.

File Menu



Edit Menu



After clicking the Form 1 menu



Here is the code for MainWindow.java which includes the menus.


import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.event.*;

public class MainWindow extends JFrame implements ActionListener {
 
 /**
  * this form creates a simple menu
  * under File there is only one menu item, Exit
  * under Edit you have option to open Form1 
  * and Form2
  */
 private static final long serialVersionUID = 1L;
 JFrame frame;
 JPanel panel;
 JMenu menuFile;
 JMenu menuEdit;
 JMenuBar menuBar;
 JMenuItem menuItem1;
 JMenuItem menuItem2;
 JMenuItem menuItem3;
 
 public MainWindow(){
  createMenu();
  frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  this.setJMenuBar(menuBar);
  createPanel();
  
  this.add(panel);
 
  
 }

 private void createMenu(){
  
  //Declare a new menu bar
  menuBar = new JMenuBar();
  
  //create a Menu on that menu bar
  menuFile = new JMenu("File");
  //this sets a keyboard equivalent 
  //on PCs ALT F
  menuFile.setMnemonic(KeyEvent.VK_F);
  
  //Define the menu items
  menuItem1 = new JMenuItem("Exit");
  menuItem1.setMnemonic(KeyEvent.VK_X);
  menuItem1.addActionListener(this);
  
  menuEdit = new JMenu("Edit");
  menuEdit.setMnemonic(KeyEvent.VK_E);
  
  menuItem2=new JMenuItem("Form1");
  menuItem2.setMnemonic(KeyEvent.VK_1);
  menuItem2.addActionListener(this);
 
  menuItem3 = new JMenuItem("Form2");
  menuItem3.setMnemonic(KeyEvent.VK_2);
  menuItem3.addActionListener(this);
  
  //Add the menu items to the menus
  menuFile.add(menuItem1);
  menuEdit.add(menuItem2);
  menuEdit.add(menuItem3);
  
  //add the menus to the menu bar
  menuBar.add(menuFile);
  menuBar.add(menuEdit);
  
  
  
 }
 
 
 
 private void createPanel(){
  panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  JLabel label=new JLabel("Main Form");
  panel.add(label);
  //frame.add(panel);
 }

 @Override
 public void actionPerformed(ActionEvent arg0) {
  Object source = arg0.getSource();
  if(source==menuItem1)
   System.exit(0);
  if(source==menuItem2){
   JFrame frame = new Form1();
   frame.setBounds(200,200,350,120);
   frame.setVisible(true);
   
  }
  if(source==menuItem3){
   JFrame frame = new Form2();
   frame.setBounds(200,200,350,120);
   frame.setVisible(true);
   
  }
  
 }

 
}


Here are the two forms

Form1.java

import javax.swing.*;
import java.awt.FlowLayout;

public class Form1 extends JFrame {
 
 public Form1(){
  JFrame frame=new JFrame();
  JPanel panel; 
  panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  JLabel label=new JLabel("Form1");
  panel.add(label);
  this.add(panel);
 }
}


Form2.java

import javax.swing.*;

import java.awt.FlowLayout;

public class Form2 extends JFrame{
 
 public Form2(){
 JFrame frame=new JFrame();
 JPanel panel; 
 panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 JLabel label=new JLabel("Form2");
 panel.add(label);
 this.add(panel);
 }
}


Here is the Main.java class

import javax.swing.*;

public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  JFrame m = new MainWindow();
  m.setBounds(100,100,350,120);
  m.setVisible(true);

 }

}

No comments:

Post a Comment