Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame and JMenu  RSS feed

Ruel Soriano
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

// 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

// 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

* 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

// then add JMenuItem in the newly create JMenu
JMenuItem item1 = new JMenuItem("File 1");
JMenuItem item2 = new JMenuItem("File 2");
JMenuItem item3 = new JMenuItem("File 3");

//adding a separator

// 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

* 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

* 2: associate to a menu item (the exit menu item)

// 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");

//adding subMenu for windowMenu
JMenuItem minimizeMenu = new JMenuItem("Minimize");
ActionListener minimizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
JFrame frame = new JFrame();

JMenuItem maximizeMenu = new JMenuItem("Maximize");
ActionListener maximizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
JFrame frame = new JFrame();


JMenuItem resizeMenu = new JMenuItem("Resize to 500,500");
ActionListener resizeAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
new Application().createMainFrame("Example Application",new Dimension(500,400));

JMenuItem changetitleMenu = new JMenuItem("title to Welcome");
ActionListener changetitleAction = new ActionListener(){
public void actionPerformed(ActionEvent args){
new Application().createMainFrame("Welcome", new Dimension(500,400));
// 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

// show the frame, if this is not called, the application will be terminated.
// and now window will be displayed.

* Main executable point, entry point of execution.
* @param args
public static void main(String[] args) {
new Application().start();

Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!