can someone look and check my codes and tell me why it didn't work when i choose Minimize, Maximize, Resize and change Title in Menu.....nothing happened if i chose from [Minimize, Maximize, Resize and change Title]any of them, is there something wrong with my actionPerformed...
here are the codes.....
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Application {
/**
* Creates an instance of JFrame with certain attribute passed
* as argument.
*
* @param titleWindow title
* @param dimensionDimension of window
* @return instance of JFrame
*/
protected JFrame createMainFrame(
String title, Dimension dimension) {
// create an instance of the window
JFrame frm = new JFrame(title);
// set the size of the window
frm.setSize(dimension);
// move the window to coordinate X:200, and Y:300
frm.setLocation(new Point(200,300));
return frm;
}
/**
* This section creates a menu bar instance that can be added in an
instance of
* JFrame.
*
* @return
*/
protected JMenuBar createMainMenu() {
// create the main menu bar
JMenuBar menuBar = new JMenuBar();
// create a menu holder
JMenu fileMenu = new JMenu("File");
// this change the font of the menu item
fileMenu.setFont(new Font("Tahoma", Font.PLAIN, 12));
// add the menu holder to the main menu
menuBar.add(fileMenu);
// to create a menu item that will hava an action use JMenuItem
JMenuItem newMenu = new JMenuItem("New...");
newMenu.setFont(new Font("Tahoma", Font.PLAIN, 12));
// add item to filemenu
fileMenu.add(newMenu);
/*
* Adding sub menus, to add sub menus you add an instance of JMenu
* within JMenu
*/
JMenu recentFiles = new JMenu("Recent Files");
// you dont need tochagne the font every time, i just want to use tahoma
recentFiles.setFont(new Font("Tahoma", Font.PLAIN, 12));
//add to file menu
fileMenu.add(recentFiles);
// then add JMenuItem in the newly create JMenu
JMenuItem item1 = new JMenuItem("File 1");
recentFiles.add(item1);
JMenuItem item2 = new JMenuItem("File 2");
recentFiles.add(item2);
JMenuItem item3 = new JMenuItem("File 3");
recentFiles.add(item3);
//adding a separator
fileMenu.addSeparator();
// adding exit menu item under file menu after the separator
JMenuItem exitMenu = new JMenuItem("Exit");
exitMenu.setFont(new Font("Tahoma", Font.PLAIN, 12));
// add item to filemenu
fileMenu.add(exitMenu);
/*
* associating an action to a menu; there are different ways to do
this but this is
* the simplest form; use the method addActionlistener()
* 1: create an instance of ActionListener; however ActionListener is
an interface,
* the only way you can create an instance is to implement it, this
is an inline
* implementation
*/
ActionListener exitAction = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// this will be called if the menu is clicked
// exit the application
System.exit(0);
}
};
/*
* 2: associate to a menu item (the exit menu item)
*/
exitMenu.addActionListener(exitAction);
// to add another menu with the same level of File menu, you need to add another
// JMenu in the instance of JMenuBar
JMenu windowMenu = new JMenu("Window");
menuBar.add(windowMenu);
//adding subMenu for windowMenu
JMenuItem minimizeMenu = new JMenuItem("Minimize");
windowMenu.add(minimizeMenu);
ActionListener minimizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.ICONIFIED|frame.getExtendedState());
}
};
minimizeMenu.addActionListener(minimizeAction);
JMenuItem maximizeMenu = new JMenuItem("Maximize");
windowMenu.add(maximizeMenu);
ActionListener maximizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
};
maximizeMenu.addActionListener(maximizeAction);
JMenuItem resizeMenu = new JMenuItem("Resize to 500,500");
windowMenu.add(resizeMenu);
ActionListener resizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
new Application().createMainFrame("Example Application",new Dimension(500,400));
}
};
resizeMenu.addActionListener(resizeAction);
JMenuItem changetitleMenu = new JMenuItem("title to Welcome");
windowMenu.add(changetitleMenu);
ActionListener changetitleAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
new Application().createMainFrame("Welcome", new Dimension(500,400));
}
};
changetitleMenu.addActionListener(changetitleAction);
// each JMenuBar, JMenu, JMenuItem are treated as individual instance or objects
// they are registed to each other on how you issue the add command call
return menuBar;
}
/**
* Use to create and start the frame
*
*/
public void start() {
// create main window
JFrame frame = createMainFrame("Example Application", new Dimension(500,400));
// add main menu, and create children
frame.setJMenuBar(createMainMenu());
// show the frame, if this is not called, the application will be terminated.
// and now window will be displayed.
frame.setVisible(true);
}
/**
* Main executable point, entry point of execution.
* @param args
*/
public static void main(String[] args) {
new Application().start();
}
}