0

This is the result i want : https://i.stack.imgur.com/N9xWD.jpg , but centered using BorderLayout. Instead, i get this :https://i.stack.imgur.com/mXJgp.jpg . So, basically setting a layout resets my background and puts the button on the whole frame:

frame.setContentPane(new Pane());
frame.getContentPane().setBackground(Color.GRAY);

I've also tried this:

JPanel panel = new JPanel();
panel.add(bStart);
add(panel, BorderLayout.CENTER);

But it doesn't work.

public class Pane extends JPanel{
    public Pane(){
        ImageIcon start = new ImageIcon("Start.png");
        ImageIcon startroll = new ImageIcon("Start-Hover.png");
        ImageIcon startselect = new ImageIcon("Start-Pressed.png");

        JButton bStart = new JButton(start);


        Insets margin = new Insets(-10,-10,-10,-10);
        bStart.setMargin(margin);
        bStart.setRolloverEnabled(true);
        bStart.setRolloverIcon(startroll);
        bStart.setPressedIcon(startselect);
        bStart.setBorderPainted(false);


        setLayout(new BorderLayout());
        add(bStart, BorderLayout.CENTER);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
zaa
  • 79
  • 8
  • *"This is the result i want : http://imgur.com/tGleIly , but centered using BorderLayout."* Use a `GridBagLayout` to center a component. See [this answer](http://stackoverflow.com/a/7181197/418556) for an example. – Andrew Thompson Jan 31 '15 at 19:10
  • setting the layout before, has no effect. – zaa Jan 31 '15 at 19:11
  • 1
    @zaa, the GridBagLayout will work. If you have a problem then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Jan 31 '15 at 20:17

1 Answers1

2
frame.getContentPane.setLayout(new GridBagLayout());
nyxaria
  • 479
  • 1
  • 3
  • 13