Java 将布局设置为JPanel会重置按钮
这是我想要的结果:,但使用BorderLayout居中。相反,我得到了这个:。因此,基本上设置布局会重置我的背景,并在整个框架上放置按钮:Java 将布局设置为JPanel会重置按钮,java,swing,layout-manager,Java,Swing,Layout Manager,这是我想要的结果:,但使用BorderLayout居中。相反,我得到了这个:。因此,基本上设置布局会重置我的背景,并在整个框架上放置按钮: frame.setContentPane(new Pane()); frame.getContentPane().setBackground(Color.GRAY); 我也试过: JPanel panel = new JPanel(); panel.add(bStart); add(panel, BorderLayout.CENTER); 但它不起作用
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);
“这是我想要的结果:,但使用BorderLayout居中。”使用
GridBagLayout
将组件居中。请参阅以获取示例。在之前设置布局没有任何效果。@zaa,GridBagLayout将起作用。如果您有问题,请发布一个适当的说明。@zaa是的,对不起。。。德普。这不是水平居中,而不是垂直居中吗?@MadProgrammer从图片操作中发布,这似乎是他想要做的。那就好了;)在图片之后,我说“但是居中”也是垂直的,就像borderlayout一样。
frame.getContentPane.setLayout(new GridBagLayout());