I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in se开发者_JAVA百科parate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
- How to Use Card Layout
- How to Use Menus
Other people have already talked about ActionListeners
and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame
per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame
will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog
would be give you independent windows like JFrame
does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}
精彩评论