Java:Swing:JButton的设置位置
我想实现以下布局:Java:Swing:JButton的设置位置,java,swing,Java,Swing,我想实现以下布局: -------------------------------------------- | | | | | | | | |
--------------------------------------------
| |
| |
| |
| |
| |
| ---------- |
| | OK | |
| ---------- |
| |
--------------------------------------------
我想设置按钮的大小,它应该居中于JFrame的南部位置,但它不应该有JFrame的全宽,也不应该完全位于JFrame的底部,它应该有一个“边距”。
也可以设置按钮的绝对像素位置,因为JFrame具有固定的宽度/高度。此时按钮具有完整的JFrame宽度/高度。
请注意,JFrame有一个背景图像。
我的代码:
Swing提供了许多“基本”布局,当这些布局组合在一起时,就可以构成人们心目中的大多数布局。但有时,创建自己的布局更容易。就你的具体情况而言,这就是我的建议 以下是Swing教程中您可以遵循的部分: 如果您确实希望通过组合标准布局来实现此目的,则可以尝试以下方法:
- 将主面板的布局设置为
BorderLayout
- 在主面板的南部添加面板
- 将此面板的布局设置为
FlowLayout
- 为此面板设置
,并设置底部值(这是您的边距)EmptyBorder
- 将按钮添加到此面板
或者,您可以将边框添加到主面板。这取决于您希望主面板其他组件的布局方式。尝试使用GridBagLayout,它提供了您想要的自由度,但学习起来有点困难。但是它确实可以做你期望做的事情。简单布局管理器的组合应该可以做到这一点。例如,主JPanel可以有一个EmptyBorder,您可以在其中添加适当的边距,并可以使用BorderLayout。然后添加一个使用FlowLayout排列的FlowLayout.CENTER(默认设置)的southPanel JPanel,您可以将JButton添加到此southPanel。不要忘记调用pack并确保所有JPanel都调用set不透明(false)。例如:
JFrame f = new JFrame("Foobar");
BufferedImage myImage = null;
try {
myImage = ImageIO.read(new File("background.png"));
} catch (Exception ex) {}
JPanel p = new ImagePanel(myImage);
p.setLayout(new BorderLayout());
int gap = 15;
p.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // new FlowLayout not needed
southPanel.setOpaque(false);
JButton button = new JButton("OK");
//button.setSize(80, 200);
button.setPreferredSize(new Dimension(80, 200)); // ?? I don't like this.
button.setFont(new Font("Arial", 1, 40));
southPanel.add(button);
p.add(southPanel, BorderLayout.SOUTH);
f.add(p);
f.pack(); // call after everything has been added
f.setLocationRelativeTo(null); // to center
f.setVisible(true);
如果你经常解决这种布局问题,那么我强烈建议你下载并学习——它基本上是一个使用类固醇的布局管理器,可以处理你能想到的任何布局
弄清楚如何使用它需要一点时间,但一旦你掌握了原理,它将是一个极好的工具,可以放在你的武器库中。是的,它比GridBagLayout更容易使用,也更灵活。更正:所有位于图像绘制JPanel顶部的JPanel都不应该是不透明的。图像绘制JPanel应该是不透明的,尤其是当它充当contentPane时。
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(188)
.addComponent(**button**)
.addContainerGap(189, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(106)
.addComponent(**button**)
.addContainerGap(171, Short.MAX_VALUE))
);
JFrame f = new JFrame("Foobar");
BufferedImage myImage = null;
try {
myImage = ImageIO.read(new File("background.png"));
} catch (Exception ex) {}
JPanel p = new ImagePanel(myImage);
p.setLayout(new BorderLayout());
int gap = 15;
p.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // new FlowLayout not needed
southPanel.setOpaque(false);
JButton button = new JButton("OK");
//button.setSize(80, 200);
button.setPreferredSize(new Dimension(80, 200)); // ?? I don't like this.
button.setFont(new Font("Arial", 1, 40));
southPanel.add(button);
p.add(southPanel, BorderLayout.SOUTH);
f.add(p);
f.pack(); // call after everything has been added
f.setLocationRelativeTo(null); // to center
f.setVisible(true);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(188)
.addComponent(**button**)
.addContainerGap(189, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(106)
.addComponent(**button**)
.addContainerGap(171, Short.MAX_VALUE))
);