I am trying to make a Quiz game and my idea is that i have a main frame class with buttons and a label like this
So when I click the Start button i want to stay in the same frame just delete all these buttons and label and replace them with the question and answers to it. How can I accomplish this? Read something about repaint() method and removeAll() but what is the best practice. I come up with this(dunno is it the right thing to do in the situation) but call removeAll() in the actionListener of the start button and create the new frame in this listener? Or can I make another class e.g called StartFrame and then in the actionListener of the start button call removeAll() and then MainFrame.this = new StartFrame() assuming that my class holding the initial state of the game like in the pic is called MainFrame
Related
I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.
Ok anyways moving on.
I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.
What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.
Any advice?
EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.
Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.
I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.
That is my issue.
See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.
I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it's not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this: getWindowAncestor(Component c). Then you could call dispose() on the window returned.
i.e.,
Window win = SwingUtilities.getWindowAncestor(myJPanel);
win.dispose();
Note that this is fine if you're using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.
The button is already on the frame (on the upper right, here). Just call JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) when constructing it and it will work as the user expects.
Other tip
Don't extend frame or panel, but instead just create and use them.
If I've created a JFrame with 2 JButtons in it. Both buttons call another JFrame through actionPerformed(ActionEvent event) (so 3 JFrames in total, the main one and the two which are called from it).
I'll name the JFrame with the JButton's main, and the two called JFrame's frame1 and frame2.
I call frame1 from one of the JButton's in main. Then I press the second JButton which calls up frame2.
I want frame1 to be closed automatically when frame2 is called and vice-versa.
I've looked for a solution and not been able to find one but I'm hoping it's fairly simple.
Thanks, help is appreciated.
I wouldn't recommend using multiple containers. Instead, use an appropriate layout manager (e.g. CardLayout). This way you'll have a single container with multiple views.
...deja vu...
I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.
I am trying to make an MP3 player, something similar to WinAmp. I'm currently having a minor issue with the GUI:
I have a main frame that will have the equalizer/volume/time elapsed etc. on it, and a sub-frame that will be the playlist. The sub-frame is a class that extends JFrame and is a field inside the main frame's class. Currently I have a button on the mainframe that changes the visibility of the sub-frame. My problem is: when the sub-frame appears, it creates a new task on the task-bar (when it's invisible the task disappears as well). I know it's not a major issue, but I find it somewhat annoying. Is there a way around this?
The subframe is a class that extends JFrame
take JDialog instead of JFrame. This will not create a new task item on task bar.