Results 1 to 3 of 3
- 12-09-2007, 08:34 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
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??????
//
}
});
- 12-10-2007, 06:46 AM #2
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
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); } }
Hope I got your question correct...
-R
- 12-10-2007, 09:02 AM #3
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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");
Last edited by staykovmarin; 12-10-2007 at 09:05 AM.
Similar Threads
-
Buttons to show new panels
By Lehane_9 in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 05:22 PM -
Selecting a JMenu paints over the JPanel on the content pane
By Swingset in forum AWT / SwingReplies: 3Last Post: 01-06-2008, 12:13 AM -
Working with Labels on Panels.
By vargihate in forum AWT / SwingReplies: 2Last Post: 01-04-2008, 05:09 AM -
Need a tutorial for Studying about Panels
By ramachandran in forum New To JavaReplies: 1Last Post: 10-25-2007, 10:05 AM -
JMenu and JRadioButtonMenuItem
By doron70 in forum AWT / SwingReplies: 3Last Post: 07-18-2007, 07:13 PM
Bookmarks