(Java) There are white borders around all my buttons

375 views Asked by At

In java, My buttons are surrounded by white, thick borders that only appear when I have a image in a jlabel for the background.

Example: enter image description here

The problem here is that white borders go around all of my buttons, making it look terrible. The intended use was for the buttons to just go over the image, like this image:

enter image description here

Here is my code:

    public class Gui extends JFrame {
    private JTextField TextField;
    private JButton Hi, Bye, Exit;
    private JPanel Panel, Panel1, Panel2;
    private JLabel label;
            public Gui() {
            super("My Program");

            TextField = new JTextField("");
            TextField.setEditable(false);

            Hi = new JButton("Hi");
            Bye = new JButton("Bye");
            Exit = new JButton("Exit");

            Actions a = new Actions();

            Hi.addActionListener(a);
            Bye.addActionListener(a);
            Exit.addActionListener(a);

            Dimension tfd = new Dimension(780, 25);
            Dimension bd = new Dimension (75, 25);
            Dimension lpd = new Dimension (800, 600);

            TextField.setPreferredSize(tfd);

            Hi.setPreferredSize(bd);
            Bye.setPreferredSize(bd);
            Exit.setPreferredSize(tfd);

            ImageIcon image = new ImageIcon("C:/Users/Dakota/Desktop/Coding/Coding/img/Background.png");
            label = new JLabel(image);

            Panel = new JPanel();
            this.setContentPane(label);
            this.add(Panel);
            this.setLayout(new FlowLayout());
            Panel.add(TextField);
            Panel1 = new JPanel();
            this.add(Panel1);
            Panel1.add(Hi);
            Panel1.add(Bye);
            Panel2 = new JPanel();
            this.add(Panel2);
            Panel2.add(Exit);
}
1

There are 1 answers

0
Hovercraft Full Of Eels On BEST ANSWER

Your problem appears that you're adding JPanels to your GUI and not changing the opaque property. This property is by default true, meaning that the JPanel will paint a background that visually covers up anything below it, but if you set it to false by calling myPanel.setOpaque(false);, then any components below the JPanel will show through. This is true for most any Swing component, although is not quite straight-forward for component components such as JScrollPanes, and JTextComponents.