Java:Swing:设置 JButton 的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4478021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Swing: Set position of JButton
提问by Poru
I wanted to realize the following layout:
我想实现以下布局:
--------------------------------------------
| |
| |
| |
| |
| |
| ---------- |
| | OK | |
| ---------- |
| |
--------------------------------------------
I wanted to set the size of the button and it shoud be centered in the souther position of the JFrame but it shouldn't have the full width of the JFrame and it shouln't be completly on the bottom of the JFrame, it should have a "margin". It would be also possible to set a absolute pixel position of the button because the JFrame has a fixed width/height. At the moment the button has the full JFrame width/height. Please notice that the JFrame has a background image. My Code:
我想设置按钮的大小,它应该在 JFrame 的南部位置居中,但它不应该具有 JFrame 的全宽,也不应该完全位于 JFrame 的底部,它应该具有一个“边距”。也可以设置按钮的绝对像素位置,因为 JFrame 具有固定的宽度/高度。目前该按钮具有完整的 JFrame 宽度/高度。请注意 JFrame 有一个背景图像。我的代码:
JFrame j = 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());
JButton button = new JButton("OK");
button.setSize(80, 200);
button.setFont(new Font("Arial", 1, 40));
p.add(button);
f.add(p);
class ImagePanel extends JPanel {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
采纳答案by Hovercraft Full Of Eels
A combination of simple layout managers should do the trick. For instance the main JPanel could have an EmptyBorder where you add appropriate margins and could use BorderLayout. Then add a southPanel JPanel that uses FlowLayout arranged FlowLayout.CENTER (the default), and you could add your JButton to this southPanel. Don't forget to call pack and to make sure that all JPanels call setOpaque(false). For example:
简单布局管理器的组合应该可以解决问题。例如,主 JPanel 可以有一个 EmptyBorder,您可以在其中添加适当的边距并可以使用 BorderLayout。然后添加一个使用 FlowLayout 排列的 FlowLayout.CENTER(默认)的 southPanel JPanel,你可以将你的 JButton 添加到这个 southPanel。不要忘记调用 pack 并确保所有 JPanel 都调用 setOpaque(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);
回答by barjak
Swing provides many "basic" layouts which, when combined together, can make up most of the layouts one can have in mind. But sometimes, it's easier to create its own layout. In your specific case, that's what I'd recommend.
Swing 提供了许多“基本”布局,当它们组合在一起时,可以构成人们可以想到的大多数布局。但有时,创建自己的布局更容易。在您的具体情况下,这就是我的建议。
Here is the part of the Swing tutorial you can follow : http://download.oracle.com/javase/tutorial/uiswing/layout/custom.html
这是您可以遵循的 Swing 教程的一部分:http: //download.oracle.com/javase/tutorial/uiswing/layout/custom.html
If you really want to do this by combining standard layouts, then you can try this :
如果你真的想通过组合标准布局来做到这一点,那么你可以试试这个:
- set the layout of the main panel to
BorderLayout
- add a panel in the
SOUTH
part of the main panel - set the layout of this panel to
FlowLayout
- set an
EmptyBorder
to this panel, and set the bottom value (this is your margin) - add the button to this panel
- 将主面板的布局设置为
BorderLayout
SOUTH
在主面板的部分添加一个面板- 将此面板的布局设置为
FlowLayout
- 设置一个
EmptyBorder
到这个面板,并设置底部值(这是你的保证金) - 将按钮添加到此面板
Alternatively, you can add the border to your main panel. It depends on how you want the other components of the main panel to be laid out.
或者,您可以将边框添加到主面板。这取决于您希望如何布置主面板的其他组件。
回答by Jo?o Silvestre
Try using GridBagLayout, it provides the freedom you want but it's a bit hard to learn. But it can definitely do what you are expecting to do.
尝试使用 GridBagLayout,它提供了您想要的自由度,但它有点难学。但它绝对可以做你期望做的事情。
回答by mikera
If you are frequently solving these kind of layout problems, then I'd strongly suggest downloading and leaning MiGLayout- it's basically a layout manager on steroids that can handle pretty much any layout you can think of.
如果您经常解决这类布局问题,那么我强烈建议您下载并学习MiGLayout- 它基本上是一个布局管理器,几乎可以处理您能想到的任何布局。
It takes a bit of time to figure out how to use it, but once you get the principles it is a fantastic tool to have in your armoury.
弄清楚如何使用它需要一些时间,但是一旦掌握了原理,它就会成为您军械库中的绝佳工具。
回答by Ewen
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))
);