Thread: SWING stuff...
View Single Post
Old Oct 23rd, 2006, 11:49 AM   #1
dsbabyGurl
Newbie
 
dsbabyGurl's Avatar
 
Join Date: Oct 2006
Location: Philippines
Posts: 13
Rep Power: 0 dsbabyGurl is on a distinguished road
Thumbs up SWING stuff...

Hei guys, i just thought i needed help, i was studying swing and i made this Frame and an internal frame and new set of menus, but the problem is i dont know how to add commands that these menu items will perform and so does adding new labels and components, i really dont know how.. can u all please help.... please...


This is Main Frame:
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;


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


/*
 * PEOFrame.java requires:
 *   MyInternalFrame.java
 */
public class PEOFrame extends JFrame
                               implements ActionListener {
    JDesktopPane desktop;

    public PEOFrame() {
        super("Perfect Entertainment ONLINE Video Rental Frame");

        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width  - inset*2,
                  screenSize.height - inset*2);

        
        //Set up the GUI.
        desktop = new JDesktopPane();
        createFrame(); 
        setContentPane(desktop);
        setJMenuBar(createMenuBar());
       
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    }

    protected JMenuBar createMenuBar() {
        
        JMenuBar menuBar = new JMenuBar();

        //Set up the first menu.
        JMenu home = new JMenu("Home");
        home.setMnemonic(KeyEvent.VK_H);
        menuBar.add(home);

        //Set up the first menu item.
        JMenuItem menuItem = new JMenuItem("New");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("new");
        menuItem.addActionListener(this);
        home.add(menuItem);

        //Set up the second menu item.
        menuItem = new JMenuItem("Quit");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        home.add(menuItem);
        
        
        //Set up the 2nd menu
        JMenu works = new JMenu("Works");
        works.setMnemonic(KeyEvent.VK_K);
        menuBar.add(works);
        
        //Set up the first item on the 2nd Menu
        JMenuItem how = new JMenuItem("How We do IT");
        how.setMnemonic(KeyEvent.VK_W);
        how.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_W, ActionEvent.ALT_MASK));
        how.setActionCommand("how");
        how.addActionListener(this);
        works.add(how);
        
        //set up the 2nd menu item
        menuItem = new JMenuItem("Categories");
        menuItem.setMnemonic(KeyEvent.VK_C);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_C, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("cat");
        menuItem.addActionListener(this);
        works.add(menuItem);
        
        //set up the 3nd menu item
        JMenuItem plan = new JMenuItem("Plan");
        plan.setMnemonic(KeyEvent.VK_P);
        plan.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_P, ActionEvent.ALT_MASK));
        plan.setActionCommand("plan");
        plan.addActionListener(this);
        works.add(plan);
        
        
        
        //Set up the 3rd menu
        JMenu accounts = new JMenu("Accounts");
        accounts.setMnemonic(KeyEvent.VK_O);
        menuBar.add(accounts);
		
		//set up the 1st menu item on the 3rd menu
        JMenuItem join = new JMenuItem("Join");
        join.setMnemonic(KeyEvent.VK_J);
        join.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_J, ActionEvent.ALT_MASK));
        join.setActionCommand("join");
        join.addActionListener(this);
        accounts.add(join);
        
        
        //set up the 2nd menu item on the 3rd Menu
        JMenuItem cancel = new JMenuItem("Cancel");
        cancel.setMnemonic(KeyEvent.VK_L);
        cancel.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_L, ActionEvent.ALT_MASK));
        cancel.setActionCommand("cancel");
        cancel.addActionListener(this);
        accounts.add(cancel);        
        	
        	        
        //Set up the 4th menu
        JMenu help = new JMenu("Help");
        help.setMnemonic(KeyEvent.VK_E);
        menuBar.add(help);
        
        //set up the 1st menu item on the 4th MEnu
        JMenuItem abPEO = new JMenuItem("About PEO");
        abPEO.setMnemonic(KeyEvent.VK_A);
        abPEO.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_A, ActionEvent.ALT_MASK));
        abPEO.setActionCommand("abPEO");
        abPEO.addActionListener(this);
        help.add(abPEO);
        
		//set up the 2nd menu item on the 4th MEnu
        JMenuItem credits = new JMenuItem("Credits");
        credits.setMnemonic(KeyEvent.VK_R);
        credits.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_R, ActionEvent.ALT_MASK));
        credits.setActionCommand("credits");
        credits.addActionListener(this);
        help.add(credits);
        help.addSeparator();
        
        //set up the quit menu item on the 4th menu
        menuItem = new JMenuItem("Quit");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        help.add(menuItem);

        return menuBar;
    }

    //React to menu selections.
    public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) { //new
            createFrame();
        } else { //quit
            quit();
        }
    }
    

    //Create a new internal frame.
    protected void createFrame() {
        MyInternalFrame frame = new MyInternalFrame();
        frame.setVisible(true); 
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }


    //Quit the application.
    protected void quit() {
        System.exit(0);
    }

     private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        PEOFrame frame = new PEOFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


The Internal Frame Code:
import javax.swing.JInternalFrame;

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

/* Used by InternalFrameDemo.java. */
public class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
	
    public MyInternalFrame() {
        super("PEONLINE Form #" + (++openFrameCount), 
              false, //resizable
              false, //closable
              false, //maximizable
              true);//iconifiable

	
        setSize(300,300);
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
    
    

}


Where do i need to put the methods and what do i have to do to make them work? Please help me, i really would want this program to work... Thanks...




dsbabyGurl
dsbabyGurl is offline   Reply With Quote