将布局设置为JPanel重置按钮

时间:2015-01-31 19:03:50

标签: java swing layout-manager

这是我想要的结果:http://imgur.com/tGleIly,但使用BorderLayout进行居中。相反,我得到了这个:http://imgur.com/jYgjM1F。因此,基本上设置布局会重置我的背景并将按钮放在整个框架上:

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

我也试过这个:

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

但它不起作用。

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);

1 个答案:

答案 0 :(得分:2)

frame.getContentPane.setLayout(new GridBagLayout());