I am writing a GUI for my program using Java Swing. I wrote a class to extend my JFrame using the following link. My problem is that my buttons are not centered on the background at all. That is, something like this:
--------------------------------------
| | |
| | |
| my picture | |
--------------------------------------
| | | |
| | Label | |
| | text | |
| | button | |
--------------------------------------
I have included some pseudocode below:
public class demo extends JFrame implements ActionListener
{
class demo()
{
CustomJFrame frame = new CustomJFrame();
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextField dummyText = new JTextField("I'm some text");
JLabel dummyLabel = new JLabel("I'm a label);
JButton dummyButton = new JButton("I'm a button");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
pane.add(dummyText, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
pane.add(dummyLabel, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 2;
pane.add(dummyButton, c);
frame.add(pane);
}
}
Here is my custom JFrame class based on the link.
public class CustomJFrame extends JFrame
{
private ImageIcon background = new ImageIcon("mypicture.png");
private JLabel label = new JLabel(background);
public CustomJFrame()
{
this.SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Traffic Alert Demo");
setSize(800,480); //specific to my picture though
add(label, BorderLayout.CENTER);
setVisible(true);
}
}
So I want some way to be able to have the buttons be centered on my picture or at least the ability for me to move the buttons around anywhere I please on the picture. I have found nothing to help me thus far, does any ideas? Or is my picture, since I made it a label, interacting badly with the GridBag? Please help.
Regards, Geeky