I have a class that extends JFrame, and I have decided to create an opening panel, a JPanel with 4 JButtonss which is added add(initPanel) in the constructor.
However, ideally a background image would help the JFrame look cleaner and decorated.
Someone on here suggested I use setContentPane(), and this in fact works.
However, the image appears and all the buttons/text disappear, and I can't get them to show.
I am wondering if there is any way to stack layers / JComponents within a JFrame, so I could have my image in the background and my JPanel in the foreground.
Related
So basically, I am trying to get a JPanel window which will display all components inside dynamically. In other words, which will re-size the window, and display to fit its content.
I have been able to do it with help of JFrame and its pack() method which : "causes this Window to be sized to fit the preferred size and layouts of its subcomponents".
In my situation, I dont want to use JFrame because it will require much effort to make all changes.
Right now, I am able to make it work but only with the help of jscroll inside which wraps the text and or any new lines, so the window size is more static. So my JPanel is extending a TopComponent and am able to display it with:
jpanel.open();
jpanel.requestActive();
So the question is how to resize a window to fit its content upon actions in that window.
The JPanel has to be added to a Window in order to make sense. So I suggest you use layout managers correctly and you will get to a decent user interface.
When you add/remove components from a visible panel you need to use:
panel.revalidate();
panel.repaint();
Then the layout manager will lay out the components again.
I'm struggling with two Java GUI things at the moment.
This is the situation: I'm designing a word game using Swing components. I have a main JFrame where everything is placed (my GUI class extends JFrame). There are two things I want to do:
1st: I would like to set an image as the background image of the main frame, it has to be displayed behind all components. i've searched around but haven't found a working solution. I tried making an extended BackGroundPanel class but when I create an instance of BackGroundPanel I have no idea how to make it the background of the frame... I also haven't find a good way to load in an image from an 'images' directory in my src folder...
2nd: when the program starts the user is greeted with an undecorated JDialog, the main frame needs to be disabled, which I figured out, but I would also like to make it a bit darker. I believe it should be possible with the GlassPane, but I have no idea how to set the GlassPane to cover the panel with one color...
Help will be much appreciated, I don't think I have any helpful code to share, but I think the situation explained above gives a general idea? I would just like someone to get me on track with this so I can further work this out! Thanks!
My Main class extends JFrame and it has a BorderLayout.
Add your BorderLayout to a JPanel having, e.g. GridLayout().
This AnimationTest illustrates painting a background image behind components.
This Translucent example illustrates using an AlphaComposite; see also this AlphaTest.
Well for your first question, you can use a label and set the icon of it:
JLabel lblimage = new JLabel("");
lblimage.setIcon(new ImageIcon(Main.class.getResource("/img/background.png")));
lblimage.setBounds(0, 0, 794, 711); //size of frame
contentPane.add(lblimage); //bottom
contentPane.add(component1); //middle low
contentPane.add(component2); //middle top
contentPane.add(component3); //top
as for your second question.. you could possibly do the same thing, just use an image with a solid color and lower the transparency, and place on top of your other components (not sure on this solution though).
In my java program, when the user clicks on a button in the main JFrame another JFrame becomes visible on top of the main frame. Now how to make the previous frame blurred?
Exactly like the background in the image :
Now how to make the previous frame blurred?
have look at JLayer(Java7) based on JXLayer(Java6),
similair effect is possible with GlassPane, or with JViewport (not proper of ways, but good Swing GUI contains JScrollPane as 1st. JComponent in JFrame / JDialog / JWindow)
I'm assuming that you are coding using Java Swing, and the picture is the effect you want.
You would use a JDialog to create the top window, rather than a JFrame. A JDialog is designed to be modal. In other words, lock the JFrame until the JDialog is closed.
The default behavior of a JDialog is to not blur or darken the underlying JFrame. In order to darken the underlying JFrame, you're going to have to paint a black image on the glass pane of the JFrame.
This tutorial shows you how to manipulate the root panes of a JFrame.
There is the setOpacity(float opacity) method that is available in JFrame (inherited from Frame actually).
I am currently attempting to move a JFrame (the same way you would by dragging the window's title bar) when the mouse is being held down over a JToolbar. The JToolbar is inside several layers of JPanels nested in the JFrame. How would I go about doing this?
EDIT: There seems to be some confusion about what I'm aiming for here. I still want the titlebar to be draggable, but I just want to extend that functionality to the JToolbar as well (while keeping JToolbar not floatable - locked in place in the frame). See screenshot:
The Component Mover should work. See the last example. Instead of a titleBar you have a toolBar.
This probably isn't the most elegant solution, but...
You could create a class that extends a JToolbar and that implements a MouseMotionListener. When the user clicks on it, you would use setBounds() on the JFrame and give it a new location relative to how much the user moves the mouse within the JToolbar.
I'm coding a simple graphics program in Java.
So, I have 3 classes.
The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)
The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.
The third class is an animation class, it displays an animation. It also extends JPanel.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!
All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.
If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).
As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).
You might want to write
GUIFrame.remove(drawingPanel);
GUIFrame.add(animationPanel);
GUIFrame.pack();
However the behavior may vary if you have other components added to your GUIFrame. It is difficult to help you exactly because you have not posted an SSCCE.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class.
Use a Card Layout.