I'm looking for a specific code that if a button is clicked the whole applet will clear screen and a new interfaces like labels, images, buttons will appear, is it possible? or if not are there any methods on terminating an applet then run a different applet at the same time when a button was pressed?
I'm not 100% if this is what you're asking, but I'll try and help with it anyway:
You may want to do some research into JPanels and the setViewport() method, this way, you can add all the items you want.
e.g. Say you had 2 JPanels, a and b, you can put a load of buttons and labels and whatever you want on one JPanel and then a load more onto the other, then you can simply switch between them using setViewport(a); or setViewort(b);
It's really simple and useful if you don't want to make a new JFrame pop- up for seperate parts of your program.
Related
I'm currently developing the menu of my application and I would like to know what's the proper way of coding a "stage based menu". What I mean by stage based menu is that, user clicks a button, the entire interface changes to the next "stage". Here are the pictures I designed in Photoshop in order to explain my idea:
First picture would be the first stage and the second picture the second.
Each round looking thing is a JButton
So far I got the main menu (fist picture) made on eclipse using WindowBuilder, made it as a JPanel and then I instantiate it on the window class.
My idea was to have an event listener listen for clicks on each button and then once the even is triggered, have the JPanel variable on the frame change to the next "stage". So I was wondering if this is actually the proper way of doing this or are there any better ways?
You could use a Card Layout to make it easy to swap panels.
Check out the section from the Swing tutorial on How to Use Card Layout for more information and examples.
I need to make a Java desktop application for a client and the last time I did Java it was 2 years ago and a little odd.
My main query is regarding navigation between GUI.
In the past, I would just create a new JForm (JFrame maybe?) whenever a button was pressed and a new GUI form/window would open up.
This time, I'd like the GUI to be inside one JForm/JFrame with just the inner content changing, how most applications look when you press a button.
I assume this is done by putting all of my GUI elements in JPanels, and deleting/creating them when buttons are pressed on the same JForm?
If not how do I do it properly?
I'll also be using Netbeans GUI editor, if anyone has a better alternative for a Java GUI builder/IDE, let me know.
Thanks!
The simplest approach would be to use a CardLayout
This will allow you to add multiple components to the UI and control which one is actively visible
Another approach could be to create menus and menuItem and having multiple JFrames now you can get the same JFrame to be displayed whenever you click the same menuItme button. This way You can minimize the creation of JFrames.
I am trying to write a slide show program in Java and would like to make the implementation as simple as possible.
The goal is to show a series of slides, each of which has navigation buttons, in addition to other buttons that depend on the slide's content. (A slide showing text would have a magnifyTextButtonand a slide with an image would not have this button.)
I was thinking an abstract Slide class would be appropriate, with subclasses for each slide type: TextSlide and ImageSlide. How would I implement these subclasses so that the magnifyTextButton would show up in TextSlides , and not in any other slide?
Also, my Slide class extends JFrame. Would each instance of a subclass of Slide need to construct a JFrame object if the show is designed to take place in a single window, like in PowerPoint?
How would I implement these subclasses so that the magnifyTextButton
would show up in TextSlides , and not in any other slide?
You can take a flag to decide whether to show magnify button or not. That flag will be true in TextSlides and false in other. Or you can have this button directly in TextSlide and not in other, this way no need to check anything. And processing related to magnify will only go in one class that is TextSlide.
Would each instance of a subclass of Slide need to construct a JFrame
object if the show is designed to take place in a single window, like
in PowerPoint?
In my opinion slide classed should extend JPanel. You can easily change panels on a single frame.
Some questions/answers that can help you in this:
slide effect with JPanel
Slide JPanel Content in a JForm on Java
have look at CardLayout
put Images as Icon / ImageIcon to JLabel
put JLabel contains Image as new Card
You've got a lot going on here, but let's see if I can help out.
1 : I see two options for how to lay out "magnifyTextButton." The first would be to make it a method exclusive to TextSlide. There's no reason that an ImageSlide would need to know anything about magnifyTextButton. In this format, you would then have a "draw" abstract method in your overarching abstract class (which then might be better left as an interface). I don't like this method as much as the one that follows.
Your other option is to make MagnifyTextButton a decorator. This way, you can mix and match MagnifyTextButton onto other classes with text in them (extensions of TextSlide, which you may very well want). This will give you more versatility and will require that your Slide classes need to know even less about MagnifyTextButtons!
2 : I think you want this to be a Jpanel rather than a Jframe.
Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:
-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)
-when "next" is clicked remove all the components from the current JFrame and add new components as needed
The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.
Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?
Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.
To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.
There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.
I have been playing around with trying to get a menu screen for my game. I have only been able to figure out how to paint a new Screen on top of an existing one; meaning that the actual game is still running behind the title screen. I don't need actual code but just a description of how I would go about this.
One way to display a menu would by by using a JDialog outside of your main application window. Take a look at the How to Make Dialogs tutorial for more information.
Another possibility would be to use JInternalFrame for your game and menu so they can be wrapped in a larger application frame. These are explained nicely in How to Use Internal Frames