Trying desperately to put a label over another label in netbeans because the one label is acting as the background image and I want the label in the foreground to have a different image.
Every time i drag another label on top, the JFrame gets bigger and the label tries to slot down the side.
Here is what I've got (I want the label where the red circle in the middle is not on the right down the side):
:
If you want to put one component on another, then you can use Layered Panes.
A layered panel will let you specify that some child components should be layered above other child components.
Update:
An example:
JLayeredPane pane = new JLayeredPane();
JLabel lbOne= new JLabel("Label one");
JLabel lbTwo = new JLabel("Label two");
pane.add(lbOne, 0);
pane.add(lbTwo , 1);
You just need to set the JFrame layout to null..
If you use panel below set the panel layout also null hope its work for you .
Related
I have a JFrame and a JPanel on it. JPanel has a label that works as a picture. On that label I created buttons manually. It works fine when I set its z-order to 1. But when I delete that buttons and re-create, it just does not work and goes back of the label. They come front when I hover my mouse on them. Could anyone help me to solve this pls?
panel.add(buttons[indexButtons],3); This is where I add the buttons manually. This one runs for multiple times after deleting older buttons each time when clicked change month button.
if(buttons[i]!=null)
{
//panel.setComponentZOrder(buttons[i], -1);
panel.remove(buttons[i]);
panel.repaint();
} This is how I remove oldest buttons.
label = new JLabel("");
label.setBounds(0, -11, 372, 309);
panel.add(label); This is the background picture where I place buttons to
. JPanel has a label that works as a picture
So the label is now the background component (not the panel).
But when I delete that buttons and re-create, it just does not work and goes back of the label
The buttons should be added to the JLabel NOT the JPanel so you have a component hierarchy like:
- JPanel
- JLabel (with background image)
- JButtons added to the label
I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.
It just involves a JFrame and a JComboBox object.
I want to have a JComboBox aligned onto the center of a frame and it must not stretch to fill the entire width.
If I directly add it onto the frame, it will expand and stretch. I think I need to add it to a panel and add that panel to the frame. But how should I add it to that panel?
If I use flow layout for the panel and add the JComboBox, it will appear to the top of the frame and not center. :(
You can use a GridBagLayout:
JComboBox comboBox = new JComboBox(...);
frame.setLayout( new GridBagLayout() );
frame.add(comboBox, new GridBagConstraints());
So I am making a game, and I want to know if it is possible so when lets say "You created a fire" it deletes that line and then displays "Your fire turns into ashes".
two more,
I want to make a jframe background, and let's say I "login" the background disappears, and a new background comes in(but the game, not a background).
I want to add a image icon( already added) (IMAGE = FIRE) it deletes that image and a new one appears( IMAGE = ASHES), how can I do this?
public class FireLabel extends JPanel {
public LabelDemo() {
super(new GridLayout(3,1)); //3 rows, 1 column
JLabel label1;
//Create the first label.
label1 = new JLabel("You created a Fire", JLabel.CENTER);
//Add the labels.
add(label1);
add(label2);
add(label3);
}
The context is a little light, however.
For swicthing from one view to another, I would suggest using a CardLayout, which would allow you to change from the login screen to the game screen.
If you're using JLabel as you primary output...simple change the text or icon using setText or setIcon as required...
To change that, use JLabel.setText. You will then have to call validate for the change to take effect.
I recommend swapping out the content pane for this. Put the login screen in a JPanel and set that as the content pane, then when needed, change the content pane to a second JPanel for the game.
Use the same technique as #1. Use a JLabel to display the image.
I have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane