I'm trying to work on a project - we are creating a little game with a GUI.
I decided to start by working on a 'main menu'. Essentially, there will be buttons such as "Single Player", "Help", etc...
I've made the GUI with the menu, etc. I have added listeners as well.
How do I approach the problem now? If someone clicks, say, 'Single Player', I'd like the screen to change to display a title showing "SINGLE PLAYER" and get rid of the main menu.
What do I need to do in my actionPerformed() method to get this effect?
I guess I will be able to work it out from there.
Any help would be greatly appreciated.
best of all options could be use JMenuItem to interact with Cards layed by CardLayout, options are described incl. images in the tutorial
best of questions asked about CardLayout ever
The best approach would be to put the corresponding windows onto a separate Jpanel and have one master panel in your gui. Whenever the user clicks on a button, you remove whatever was on the master panel add the corrseponding panel, then repaint the gui.
something like this:
if (e.getSource() == button){
masterPanel.removeAll();
masterPanel.add(newWindow);
this.setVisible(true);
this.repaint();
}
Related
right now I´m trying to programm a little Tetris clone.
Therefore I want to have ONE jFrame, which should contain multiple jPanels (for the Main Menu, the game itself, Options, etc etc...).
I searched a bit and a lot of people say that one should use a CardLayout.
So I went to my NetBeans GUI Builder, made a jFrame switched it to CradLayout and added 2 Panels;
the first panel only contains a button, and the second panel contains my "game" (my graphical display of my Tetrismatrix, the graphical display of the next Block, and a exit button).
Pictures for better understanding:
The "Main Menu" is only a button that says "Start game" (can´t post more than 2 links because I´m new).
My current "Game Menu"
The structure of the "Card Layout in the NetBeans GUI Builder"
To achieve the switch between the Panels I´m using this (found after a little research):
#Action
public void cardSwitcher() {
CardLayout cl = (CardLayout) (gamePanel.getLayout());
cl.next(gamePanel);
}
Pressing the "Start Game" button then calls the method cardSwitcher().
When I now run my jFrame it starts just fine, I see my Start Game button and everything. But as soon as I press the button I get an ClassCastException.
"javax.swing.GroupLayout cannot be cast to java.awt.CardLayout"
So now my question is, can I even achieve my goal to have 1 Frame with multiple jPanels in it switching these with the CardLayout or is there a easier/better way to do this?
Thanks in advance for any help.
PS: I´m sorry for wrong spelling or bad grammar, I´m not a native speaker.
In addition if the question is already answered and I was to dumb to find the Post about it I´m going to remove this post immediately. And I´m always open for constructive critic.
Yes you can switch panels in cardLayout. You should take the cardLayout directly from the component on which you defined it (probably JFrame#getLayout()) not from panel inside the cardLayout (from what you've written I assume that gamePanel is inside cardLayout).
I have something like this...
Its a bunch of JLabels within JPanels inside a
--JFrame
--JPanel
--Set<JPanel>
--JLabel object contained in each JPanel object cotained in the set
I want to create an external panel/frame so that each time I hover over each individual JPanel, a new frame/panel pops up giving me some data. This will essentially overlap over the JFrame.
Its pretty brief but I just need some guidance as to what I need to look up.
Maybe you can just use JToolTip. When the mouse hovers over the label for a couple of seconds it will display automatically. See the section from the Swing tutorial on How to Use Tool Tips.
Or if you want something more complicated use a MouseListener. On mouseEntered() you can display a JPopupMenu or an undecorated JDialog.
You can start by reading the Swing tutorial on How to Write a Mouse Listener.
I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help
I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src
I am developing a desktop Java application with GUI implemented through Swing. I have made a JFrame and have added three buttons on it - Add, Edit, Delete.
Now I want that whenever a user clicks on any of the button, the content specific to that button appears besides those three buttons.
So how to implement this? Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPanel?
So far, I have taken a JFrame and have added 3 buttons on it. That's it.
For the Add button, I want to add some buttons and textfields to add information to the database.
For the Delete button, I want to add some buttons to find records in the database based on the information entered through the user in the textfield that appears when the user clicks on the Delete button.
Similar type of content for Edit button.
So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPane
That would be fine. When you push the button, you can call JPanel.removeAll() to remove all the controls currently in the control, and then just do the layout again, specific to whatever button you pushed.
If you have custom swing controls, just add your custom control the JPanel using a BorderLayout and putting in the center.
Another option would be to use a CardLayout, and flipping between the cards when a user presses one of the buttons. If the layouts for the buttons never change, that would probably be a better way to do it. Obviously if the content changes between button presses, you'll need to redo the layout each time.
Either of Chad's or Alex's answers would be fine. You will probably need to call a combination of revalidate() and repaint() on the panel that you've changed, as in the past I've noticed Swing doesn't always like panels being swapped out.
Also, have you considered using a JTabbedPane instead of manually coding the interaction with the add/edit/delete buttons?
I haven't done a lot of Java programming, but I think using 2-3 different JPanel, and make visible the one you need depending on the button that was clicked would do the trick.
I'm not sure if this is the right approach though.
I was using a JFrame to add all buttons and make a new JFrame for a new window and hide a previous one.
gven way are better. I will do that now.