Results 1 to 3 of 3
  1. #1
    plodos is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default JMenu calling another Forms/Panels

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class Hw extends JFrame{

    JButton[] buttons = new JButton[26];

    public Hw(){
    Menu();
    getContentPane().setLayout(new GridLayout(2,13));
    JButton []buttons=new JButton[26];

    for(int i=0; i<buttons.length;i++)
    {
    buttons[i]=new JButton(""+(char)('A'+i));
    getContentPane().add(buttons[i]);
    }

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    }

    public void Menu(){
    // set up File menu and its menu items
    JMenu fileMenu = new JMenu( "File" );
    fileMenu.setMnemonic( 'F' );

    // set up About... menu item
    JMenuItem saveItem = new JMenuItem( "Save Game" );
    saveItem.setMnemonic( 'A' );

    fileMenu.add( saveItem );

    // set up About... menu item
    JMenuItem loadItem = new JMenuItem( "Load Game" );
    loadItem.setMnemonic( 'A' );

    fileMenu.add( loadItem );


    // set up Exit menu item
    JMenuItem exitItem = new JMenuItem( "Exit" );
    exitItem.setMnemonic( 'x' );


    fileMenu.add( exitItem );

    // create menu bar and attach it to MenuTest window
    JMenuBar bar = new JMenuBar();
    setJMenuBar( bar );
    bar.add( fileMenu );
    ///////////////////////////////////////////////////////////////
    // create Format menu, its submenus and menu items
    JMenu optionsMenu = new JMenu( "Options" );
    optionsMenu.setMnemonic( 'F' );

    // set up About... menu item
    JMenuItem optionsItem = new JMenuItem( "Options" );
    optionsItem.setMnemonic( 'A' );

    optionsMenu.add( optionsItem );

    // add Format menu to menu bar
    bar.add( optionsMenu );


    }

    public static void main(String[] args) {
    Hw win = new Hw();
    win.setVisible(true);
    win.setTitle("Hangman Game");
    }



    }

    Problem is calling another Panel, if I click the Save Game , program will show me another Form/Panel ... What is the code block to call another forms?
    But i dont want to use JFıleChooser , I just want to empty form

    saveItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //what will I write??????
    //
    }
    });

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    If all you need is an empty frame when clicking the save game option, here is what you can do

    Java Code:
    public class Hw extends JFrame implements ActionListener{
    ......
    
    saveItem.addActionListener(this);
    
    .................
    
    	public void actionPerformed(ActionEvent e) {
    		JFrame newFrame = new JFrame("Empty frame");
    		newFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		newFrame.setSize(200,200);
    		newFrame.setVisible(true);
    	}
    }
    This would open an empty frame when you click the save game.you can do whatever you want in it.
    Hope I got your question correct...

    -R

  3. #3
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Or even easier, you can call a small dialog to appear using JOptionPane:
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    Java Code:
    JOptionPane.showMessageDialog(prentFrame, "TextMsg");
    There are also options for adding more buttons and such.
    Last edited by staykovmarin; 12-10-2007 at 09:05 AM.

Similar Threads

  1. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:22 PM
  2. Replies: 3
    Last Post: 01-06-2008, 12:13 AM
  3. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 05:09 AM
  4. Need a tutorial for Studying about Panels
    By ramachandran in forum New To Java
    Replies: 1
    Last Post: 10-25-2007, 10:05 AM
  5. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •