将布局设置为 JPanel 会重置按钮
Setting Layout to JPanel resets Buttons
这是我想要的结果:http://imgur.com/tGleIly , but centered using BorderLayout. Instead, i get this :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);
frame.getContentPane.setLayout(new GridBagLayout());
这是我想要的结果:http://imgur.com/tGleIly , but centered using BorderLayout. Instead, i get this :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);
frame.getContentPane.setLayout(new GridBagLayout());