![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Location: Philippines
Posts: 13
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
You must add an ActionListener to those menu items. For example
[php] import javax.swing.*; import java.awt.event.*; public class Example { public static void main(String args[]) { JFrame frame = new JFrame("Example Frame"); JMenu filemenu = new JMenu("File"); JMenuItem exititem = new JMenuItem("Exit"); exititem.addActionListener(new ExitAction()); filemenu.add(exititem); JMenuBar bar = new JMenuBar().add(filemenu); frame.addJMenuBar(bar); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); frame.setVisible(true); } private class ExitAction implements ActionListener { public void actionPerformed(ActionEvent ev) { System.exit(0); } } } [/php] Here I have set the ActionListener "ExitAction " to the menu item object "exititem" which was add to a JMenu object, "filemenu". This was a simple example but what I do myself is make a lot of inner classes for different tasks and set those tasks according to the menu items/buttons I want those to execute that tasks.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Location: Philippines
Posts: 13
Rep Power: 0
![]() |
Thank you so much..
i also have one question, how do i add components to an the internal frame that i have??? Sorry if i keep asking questions, i just want to learn stuff and i know u could help me.. hehe... |
|
|
|
|
|
#4 |
|
Sexy Programmer
|
Make a class that extends the JInternalFrame and then add component to it's content pane. Then add that class to the JDesktopPane. I am a little rusty on this but I hope I make sense.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2006
Location: Philippines
Posts: 13
Rep Power: 0
![]() |
This Menu Item that i want to put commands in and which needs a new Internal Frame and its components is the JOIN Menu Item... Ive already created sorts of Internal frames on how to add components but more errors appear and when i keep changing them, i feel its more out of the logic... The main frame which contains the JOIN Internal Frame from the Join Menu Item is called PEOFrame. please look through my codes if its ok or if im really really really lost...
Thanks really for helpin me out on this one reggaeton king, here is the internal frame i made, and its compnents, please try to correct me if my codes are wrong... 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 javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
/*
* PEOFrame.java requires:
* MyInternalFrame.java
*/
public class PEOFrame extends JFrame implements ActionListener
{
JDesktopPane desktop;
final String abtPEO = "PERFECT ENTERTAINMENT ONLINE VIDEO RENTAL SYSTEM Beta V1.0ab\n\nThis system is made by:\nROJAS, JANE CIRFEL L.\n0533-0404-1580\n\nPlease contact for more informations:\nEmail: rojasjanecirfel@gmail.com\nMobile Phone#: +63906-4872703\nHome Phone#: +6332-4709242";
final String cred = "THANK YOU TO:\nJulie Espino\nHenry James Cinco\nRigel Conag\n\nAND MOST ESPECIALLY TO:\nMrs. Joy M. Dy\nINFORMATICS INTERNATIONAL COLLEGE\nUPTOWN CEBU";
public PEOFrame() {
super("Perfect Entertainment ONLINE Video Rental BETA V1.0ab");
//Makes the main window indented 50 pixels from each edge
//of the screen.
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(); //a specialized layered pane
//createFrame(); //create first "window" form creatFrame method
setContentPane(desktop);
setJMenuBar(createMenuBar());// set up the main windows menus which is made in the createMenuBar method..
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));
accounts.add(join);
join.addActionListener(new ActionListener()
{
public void actionsPerformed(ActionEvent ja)
{
buildjoinInternal();
}
});
//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);
abPEO.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent m)
{
abtMSG();
}
});
//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);
credits.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent cre)
{
credMSG();
}
});
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;
}
protected void buildjoinInternal()
{
JoinInternalFrame joinInternal = new JoinInternalFrame();
joinInternal.setVisible(true);
desktop.add(joinInternal);
}
public void abtMSG()
{
JOptionPane.showMessageDialog(this,abtPEO);
}
public void credMSG()
{
JOptionPane.showMessageDialog(this,cred);
}
//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();
}
});
}
}This is the JOIN INTERNAL FRAME: import javax.swing.JInternalFrame;
import java.awt.event.*;
import java.awt.*;
public class JoinInternalFrame extends JInternalFrame {
static int openFrameCount = 1;
static final int xOffset = 30, yOffset = 30;
JFrame frame;
// String email, createP, retypeP;
public JoinInternalFrame() {
super("Join Frame #" + (++openFrameCount),
false, //resizable
false, //closable
false, //maximizable
true);//iconifiable
setSize(500, 500);
setVisible(true);
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public static void main (String args[])
{
compJoin contoComp = new compJoin();
contoComp.setVisible(true);
frame.add(compJoin);
}
}and this code is supposed to be the components of the JOIN Internal Frame: /**
* @(#)compJoin.java
*
*
* @author
* @version 1.00 2006/10/27
*/
public class compJoin extends JoinInternalFrame{
JLabel emailL;
JLabel createPL;
JLabel retypePL;
JTextField emailF;
JTextField createPF;
JTextField retypePF;
JButton submit, clear;
public compJoin()
{
protected void createME(String email, createP, retypeP)
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel toplevelC = new JPanel();
toplevelC.setLayout(new BorderLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
emailL = new JLabel("Email: ");
emailF = new JTextField(10);
p1.add(emailL);
p1.add(emailF);
createPL = new JLabel("Create Password: ");
createPF = new JTextField(10);
p2.add(createPL);
p2.add(createPF);
retypePL = new JLabel("Re-Type Password");
retypePF= new JTextField(10);
p3.add(retypePL);
p3.add(retypePF);
p4.add(submit);
p4.add(clear);
toplevelC.add(p1, BorderLayout.NORTH);
toplevelC.add(p2, BorderLayout.CENTER);
toplevelC.add(p3, BorderLayout.SOUTH);
toplevelC.add(p4, BorderLayout.SOUTH);
}
}Im Sorry if thaz too much code for u, i really need help on these and thank you so much reggaeton king for ur time! you rock! lol! :banana: :banana: :banana: :banana: |
|
|
|
|
|
#6 |
|
Sexy Programmer
|
When I have time. I will go through your code and check to see what's wrong.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2006
Location: Philippines
Posts: 13
Rep Power: 0
![]() |
ok thanks..
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java Swing FocusTraversalPolicy | hemanth.balaji | Java | 0 | May 11th, 2006 11:25 PM |
| Swing || AWT Tutorials | Java_König | Java | 2 | Jan 28th, 2006 11:19 PM |
| Random Stuff I made | Indigno | Coder's Corner Lounge | 17 | Jan 8th, 2006 8:08 AM |
| swing GUI stuff | rsnd | Java | 4 | Aug 10th, 2005 4:39 AM |
| Swing books? | Mjordan2nd | Java | 0 | Mar 2nd, 2005 8:50 PM |