Reputation: 1
I would like to position some buttons so that they are centered, but on the right side of the panel (where the pale boxes are). The background re-sizes when the window is re-sized, therefore I would like for the buttons to remain centered within that region of the panel. The background is split in half with the logo on the left, and the pale boxes (where I want to position the buttons) on the right. I need the four buttons so that they are over the top of them four pale boxes, and remain there when the window is re-sized, therefore all the locations/sizes need to be relative to the size of the window. How could I do this without having to manually re-size/re-position the buttons when the window is re-sized (as I have done with the background using a component listener)? I thought about possibly using a box layout, however this only centers in the center of the program.
(Ignore the button in the top left)
Upvotes: 0
Views: 52
Reputation: 208994
You can nest panels. One option you may want to consider is to create four panels with GridBagLayout
(or labels). Use some simple photoshop to cut out one of the pale boxes and use it for the background of each panel/label. You can then you can just add each button on top of the panel/label. This way you will ensure that no matter what size the frame, the buttons will be in the pale image
Here's an example using these images
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonsWithBackground {
public ButtonsWithBackground() {
BufferedImage[] images = getImages();
ImagePanel backgroundPanel = new ImagePanel(images[0]);
backgroundPanel.setLayout(new GridLayout(1, 2, 0, 0));
JPanel buttonsPanel = createButtonsPanel(images);
JPanel leftPanel = new JPanel();
leftPanel.setOpaque(false);
backgroundPanel.add(leftPanel);
backgroundPanel.add(buttonsPanel);
JFrame frame = new JFrame();
frame.setContentPane(backgroundPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createButtonsPanel(BufferedImage[] images) {
JPanel panel = new JPanel(new GridLayout(4, 1));
for (int i = 0; i < 4; i++) {
ImagePanel buttonPanel = new ImagePanel(images[1]);
buttonPanel.add(new JButton("Hey I'm a Button"));
panel.add(buttonPanel);
}
return panel;
}
private BufferedImage[] getImages() {
BufferedImage[] bi = new BufferedImage[2];
try {
BufferedImage bg = ImageIO.read(new URL("https://i.sstatic.net/YFfO4.png"));
BufferedImage jbtbg = ImageIO.read(new URL("https://i.sstatic.net/DtO9U.png"));
bi[0] = bg;
bi[1] = jbtbg;
} catch (IOException ex) {
Logger.getLogger(ButtonsWithBackground.class.getName()).log(Level.SEVERE, null, ex);
}
return bi;
}
private class ImagePanel extends JPanel {
private BufferedImage img;
public ImagePanel(BufferedImage img) {
this.img = img;
setLayout(new GridBagLayout());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ButtonsWithBackground();
}
});
}
}
You can resize the window and the buttons will stay in place.
Upvotes: 1
Reputation: 811
Put the buttons in a single JPanel:
Jpanel buttonHolder = new JPanel;
buttonHolder.add(button1);
buttonHolder.add(button2);
buttonHolder.add(button3);
buttonHolder.add(button4);
frame.add(buttonHolder, FlowLayout.RIGHT); //justify to the right side
Generally, it's a good idea to have all your buttons in the same panel so that if you decide to add a fifth button, it's really easy to do so without ruining the rest of your GUI. As peeskillet has suggested, use a GridBagLayout if you need the GUI to handle resizing (take a look at fill
in the link provided.
Upvotes: 0