-1

Can somebody explain me how can I put few jButtons inside jLabel which have background image like on this image? The main jFrame is undecorated and is set to full screen.

I saw a lot of different examples like this or like this, but these examples are showing only single button in jPanel.

Community
  • 1
  • 1
Andriy
  • 550
  • 5
  • 15

2 Answers2

2

Personally, I'd avoid using a JLabel for this purpose, it does not calculate it's required size based on it's content, but rather off it's icon and text properties.

This might be a good or bad thing, but it can catch your unawares if you're not aware of it.

Instead, I'd use a customised JPanel, which would allow you to define things like the resize and fill rules, for example and for example

Now, once you have that covered, you need to create a panel of your buttons. I prefer to create a dedicated class, as it makes it easier to isolate functionality and management, but that's me...

public class ButtonPane extends JPanel {

    public ButtonPane() {
        setLayout(new GridBagLayout());
        setBorder(new EmptyBorder(8, 8, 8, 8));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(2, 2, 2, 2);

        add(new JButton("Button 1"), gbc);
        add(new JButton("Button 2"), gbc);
        add(new JButton("Button 3"), gbc);
    }

}

Next, you need to add this panel to your background

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(backgroundPane);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.insets = new Insets(30, 30, 30, 30);

ButtonPane buttonPane = new ButtonPane();
frame.add(buttonPane, gbc);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Which can generate something like...

Background image

Have a look at Laying Out Components Within a Container and How to Use GridBagLayout for some more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @MagProgrammer How would you get rid of that grey background of those buttons. Basically, how could you make the buttons have a transparent background so that that background image is the only thing there behind the buttons. – Code Doggo Jan 10 '16 at 00:19
  • It's just a JPanel, you can change the opaque property to make it transparent setOpaque(false) – MadProgrammer Jan 10 '16 at 01:24
0

These examples are truly good enough, I think you should just learn more about swing. For now, You could simply do:

JFrame frame = new JFrame("Hi there");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
frame.add(b1);
frame.add(b2);
b1.setBounds(60, 60, 40, 40);
b2.setBounds(10, 10, 40, 40);
frame.setVisible(true);         //in case, add frame.setLayout(null);

You can of course add buttons to JPanel instead of JFrame

Asakura
  • 127
  • 3
  • 14