How to transition from JFrame to another JFrame after a button click? - java

I am making a GUI for a game, and I have a splash screen which I created using JFrame. I have a button which says play and what I want after that to happen is when I press play, I want to switch to another JFrame, which is going to have different stuff in it. However, I do not want the window to close and open another one, I want it to just switch from one frame to another frame.
I have no experience on GUI, if you have any information that would help it would be greatly appreciated.

Here is an example:
import javax.swing.JFrame;
import javax.swing.JButton;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setBounds(10, 10, 500, 200);
JButton b1 = new JButton("b1");
b1.addActionListener((c) -> {
buttonPressed(f);
});
f.setContentPane(b1);
f.setVisible(true);
}
private static void buttonPressed(JFrame f) {
JButton b2 = new JButton("b2");
f.setContentPane(b2);
f.revalidate();
}
When b1 is pressed, the content pane for the frame is replaced with a new button. the revalidate() call is needed in order for the UI to refresh after the change.

Related

Weird Issue with JComponent Visibility

I am working on a GUI project with Swing in Java and the program is generally working fine. However, under each screen, I have a back button that calls the method of the screen before it and goes through the ArrayList containing all of the elements on the current screen and calls setVisible(false) on them. Upon running the program, the back button works correctly if you click it once but if you go back on the screen, and click it again, it takes two clicks for it to correctly work and then four clicks and then eight clicks and so on. I have no idea what is going on or why it is behaving this way as nothing in my code seems to do it. Also, sometimes, the button correctly returns to the previous screen but then keeps the components on the current screen active as if setVisible(false) was never called. The following code represents the general structure of my project. Is there anything that it is doing that is generating this problem?
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MAIN {
static JFrame frame;
static JPanel panel;
public static void main(String [] args) {
mainScreen();
}
public static void mainScreen() {
JButton newScreen = new JButton("Next Screen");
frame = new JFrame();
panel = new JPanel();
panel.setBounds(0,0,1920,1080);
panel.setBackground(Color.cyan);
newScreen.setBounds(50, 500, 100, 500);
newScreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newScreen.setVisible(false);
JButton returnButton = new JButton("return");
returnButton.setBounds(50, 50, 100, 100);
panel.add(returnButton);
returnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnButton.setVisible(false);
mainScreen();
}
});
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(newScreen);
frame.add(panel);
frame.setSize(1920,1080);
frame.setLayout(null);
frame.setVisible(true);
}
}

My JFrame Closes When I Close Another Frame

I have a project that I'm working on a project that requires 2 JFrames in a single program. The problem is that when I close one the other will also close so I made a test class to see what the issue was and I still couldn't figure it out so here is the test case that I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class frameTest {
public static void main(String[] args) {
JFrame f1 = new JFrame();
JButton open = new JButton("open");
open.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFrame f2 = new JFrame();
f2.setVisible(true);
f2.setDefaultCloseOperation(f2.EXIT_ON_CLOSE);
f2.setSize(200, 200);
}
});
f1.setDefaultCloseOperation(f1.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.setSize(500, 500);
f1.add(open);
}
}
When I click the open button the popup (f2) will appear but when I close it the other window will also close, why does this happen?
f2.setDefaultCloseOperation(f2.EXIT_ON_CLOSE);
EXIT_ON_CLOSE means close the Java VM.
If you just want to close the current frame then use:
f2.setDefaultCloseOperation(f2.DISPOSE_ON_CLOSE);
Take a look on this line:
f2.setDefaultCloseOperation(f2.EXIT_ON_CLOSE);
It means that your application is terminated when you close the frame. So, not second frame is closed. Whole application is terminated.
If you do not want this behavior remove this line.

How do I create 2 frames in Java and Link them together?

I just created a GUI, now I want to create another GUI and link both together.
So on the first GUI when the user selects 'next' button, the second GUI is displayed.
For this, do I have to create a new class and just create a GUI again?
Here is what I have now:
import java.awt.Color;
import javax.swing.*;
public class Wizard {
private JLabel lblPicture;
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JButton btNext;
private JPanel panel;
public static void main(String[] args) {
new Wizard();
}
public Wizard() {
JFrame frame = new JFrame("Wizard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,360);
frame.setVisible(true);
MyPanel();
RadioButtons();
Button();
Image();
groupButton();
frame.add(panel);
frame.setVisible(true);
}
public void MyPanel() {
panel = new JPanel();
panel.setLayout(null);}
public void RadioButtons() {
btLdap = new JRadioButton ("Ldap");
btLdap.setBounds(60,85,100,20);
panel.add(btLdap);
btKerbegos = new JRadioButton ("Kerbegos");
btKerbegos.setBounds(60,115,100,20);
panel.add(btKerbegos);
btSpnego =new JRadioButton("Spnego");
btSpnego.setBounds(60,145,100,20);
panel.add(btSpnego);
btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60,175,100,20);
panel.add(btSaml2);
}
public void Button() {
btNext = new JButton ("Next");
btNext.setBounds(400,260,100,20);
panel.add(btNext);
}
public void Image() {
ImageIcon image = new ImageIcon("image.jpg");
lblPicture = new JLabel(image);
lblPicture.setBounds(200,20, 330, 270);
panel.add(lblPicture);
}
private void groupButton() {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(btLdap);
bg1.add(btKerbegos);
bg1.add(btSpnego);
bg1.add(btSaml2);
}
}
To display another window, you would create the window, be it a JFrame, JDialog, or what have you, and call setVisible(true) just like you do for your first window.
You ask if your other "window" should be in another class, and likely that answer is yes. Since it will have a completely different set of behaviors and goals from the first class, better to separate out concerns.
Having said that, what you plan to do, to show multiple windows is not always the best user interface design. Better often is to show multiple views using a container that uses a CardLayout.
If you want to display another window in a modal fashion, that is, have the first window wait for the second window to be processed before allowing user interaction, the second window should be a modal JDialog or JOptionPane (a JDialog in disguise).
I think for what you want to achieve, the use of a CardLayout would be appropriate.
This enables you to have multiple panels within the one frame with only one panel visible at a time and has functionality to 'flip' through the panels like a 'deck of cards'. So on initialising your frame you create the panels you want, and specify which one to start at then your next button will go to the next panel in the list.
See the tutorial here there are also some video tutorials available on youtube.
Write the two GUI's in different classes. When you start your program, start the first GUI.
FirstGUI frame1 = new FirstGUI("Title text");
frame1.setVisible(true);
Then, in the action listener code for the button you call "next"...
frame1.setVisible(false); //if you want to save the frame
frame1.dispose(); //if you want to kill the frame
SecondGUI frame2 = new SecondGUI("Title text");
frame2.setVisible(true);

Where have I messed up regarding creating a Game Menu?

I am trying to create a basic game menu for a game right now. I am just testing out the menu for now, and most of the options I wrote are just to test out whether the menu actually works or not. So I have a Menu class and a OptionPanel class as well.
Here is the Menu Class:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
JPanel cardPanel;
public Main(String title) {
super(title);
setBounds(100, 100, 800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardPanel = new JPanel();
CardLayout cl = new CardLayout();
cardPanel.setLayout(cl);
OptionPanel panel1 = new OptionPanel(this);
Board panel2 = new Board();
Rules panel3 = new Rules();
cardPanel.add(panel1,"1");
cardPanel.add(panel2,"2");
cardPanel.add(panel3,"3");
add(cardPanel);
setVisible(true);
}
public static void main(String[] args)
{
Main w = new Main("AP Animation Demo");
}
public void changePanel() {
((CardLayout)cardPanel.getLayout()).next(cardPanel);
requestFocus();
}
}
And here is my Option Panel class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class OptionPanel extends JPanel implements ActionListener {
Main w;
public OptionPanel(Main w) {
this.w = w;
JButton button = new JButton("Press me!");
button.addActionListener(this);
add(button);
JButton button2 = new JButton("Game rules");
button2.addActionListener(this);
add(button2);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.BLACK);
}// Call JPanel's paintComponent method to paint the background
public void actionPerformed(ActionEvent e) {
w.changePanel();
}
}
How do I make it so when the menu pops up, I can click on one button that leads to the game, and when clicking on another button, get linked to another screen. I think it has something to do with the actionPerformed thing, so I tried adding if (e.getSource == button) and stuff like that, but it could not find any button variable. Any advice/feedback?
If you want the actionPerformed() method to be able to access a button variable, then the variable has to have an instance scope (or static, less preferable almost always). Referring to it in the method as you have it written won't work because the button variable is local to the constructor.
The suggestion in the comments is to make a separate ActionListener for each button; you only need to use the if (e.getSource() == button) if the one actionPerformed() method is getting called for multiple buttons. The difference between these is a little much for a SO answer; you can get a tutorial on action listeners in the Java tutorials at Oracle.
The way you have started above suggests you are going to use the OptionPanel as a single action listener for all buttons, and therefore it needs to test which button invoked it. If instead you have a separate action listener for each button, then it knows which button invoked it and doesn't need to test.
Try looking up "anonymous inner classes" as they relate to action listeners in Java.

Modal dialog not always on top of an undecorated JFrame when another JFrame is visible

I have a weird problem with modal dialogs and undecorated JFrame.
If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.
But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.
This problem doesn't happen:
if the main frame is decorated
or if the second frame is not visible
EDIT
I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32
The code I used to test:
public static void main(String[] args) {
// creates main frame and set visible to true
final JFrame mainFrame = new JFrame();
mainFrame.setUndecorated(true); // if I comment this line, everything goes well
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(500, 500));
mainFrame.setVisible(true);
// creates a dialog and set visible to true
JFrame anotherFrame = new JFrame();
anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
// display a modal dialog
JOptionPane.showMessageDialog(mainFrame, "A message");
}
But, if create another JFrame (or another JDialog), the modal dialog
still prevent me to interact with the main frame, but now the modal
dialog is not always on top and go underneath the main frame when I
click on it.
not true at all, both are not accesible untill JOptioPane is visible
JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM
there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS
code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
private JFrame mainFrame = new JFrame();
private JFrame anotherFrame = new JFrame();
public Main() {
mainFrame.setUndecorated(true);
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(100, 60));
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anotherFrame.setVisible(true);
anotherFrame.setLocation(110, 0);
anotherFrame.setSize(new Dimension(100, 60));
anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(mainFrame, "A message");
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
}

Categories

Resources