Java swing 布局没有达到我想要的效果
我正在尝试编写我认为应该易于指定的对话框,我有一些标签和它们对面的一些文本区域,然后是“确定”和“取消”按钮。 在中间,我有一个宽组件(例如,我使用了标签)
|label| [combo box]
|label| [txtfield]
|label| [txtfield]
| long label here |
[btn1] [btn2]
,我正在尝试使用 GridBagLayout,它没有做我想要的事情,我不确定我明白为什么。基本上我希望按钮固定在对话框的底部。
下面的代码说明了我如何使用它:
package gui;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class GridBagLayoutExample2 extends JFrame {
private static final long serialVersionUID = -1972347726217162551L;
final private JLabel lbl1 = new JLabel("LABEL1: ");
final private JLabel lbl2 = new JLabel("LABEL2: ");
final private JLabel lbl3 = new JLabel("LABEL3: ");
final private JTextArea txt1 = new JTextArea(" ");
final private JComboBox cmb1 = new JComboBox();
final private JTextArea txt2 = new JTextArea("");
final private JLabel lblLine = new JLabel("a compenent on all the line");
final private JButton btnOK = new JButton("OK");
final private JButton btnCancel = new JButton("Cancel");
public GridBagLayoutExample2() {
GridBagLayout bl = new GridBagLayout();
Container pane = getContentPane();
pane.setLayout(bl);
GridBagConstraints c = new GridBagConstraints();
int r = 0;
placeComponentInGridBagLayout(lbl1, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(cmb1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl2, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt2, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl3, pane, bl, c, 0, r, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lblLine, pane, bl, c, 0, r++, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 2, null, null, null);
placeComponentInGridBagLayout(btnOK, pane, bl, c, 0, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(btnCancel, pane, bl, c, 1, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
setSize(new Dimension(850, 350));
pack();
setVisible(true);
}
public static void placeComponentInGridBagLayout(Component component, Container container, GridBagLayout bagLayout, GridBagConstraints c, Integer gridX, Integer gridY, Double weightX, Double weightY, Integer anchor, Integer fill, Integer ipadx, Integer ipady,
Integer gridWidth, Integer gridHeight) {
if (c == null) {
c = new GridBagConstraints();
}
if (gridX != null) {
c.gridx = gridX;
}
if (gridY != null) {
c.gridy = gridY;
}
if (weightX != null) {
c.weightx = weightX;
}
if (weightY != null) {
c.weighty = weightY;
}
if (fill != null) {
c.fill = fill;
}
if (anchor != null) {
c.anchor = anchor;
}
if (ipadx != null) {
c.ipadx = ipadx;
}
if (ipady != null) {
c.ipady = ipady;
}
if (gridWidth != null) {
c.gridwidth = gridWidth;
}
if (gridHeight != null) {
c.gridheight = gridHeight;
}
bagLayout.setConstraints(component, c);
container.add(component);
}
}
知道我做错了什么吗? Swing 中是否有更现代的方法来实现相同的目标?
谢谢
I'm trying to write what I thought should be an easy to specify dialog, I have a few labels and a few text areas opposite them and then an OK and cancel button.
In the middle I have a wide component (I used a label for example)
|label| [combo box]
|label| [txtfield]
|label| [txtfield]
| long label here |
[btn1] [btn2]
I'm trying to use the GridBagLayout, it's not doing what I want, and I'm not sure I understand why. Basically I want the buttons to be anchored to the bottom of the dialog.
The code below shoes how I'm using it:
package gui;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class GridBagLayoutExample2 extends JFrame {
private static final long serialVersionUID = -1972347726217162551L;
final private JLabel lbl1 = new JLabel("LABEL1: ");
final private JLabel lbl2 = new JLabel("LABEL2: ");
final private JLabel lbl3 = new JLabel("LABEL3: ");
final private JTextArea txt1 = new JTextArea(" ");
final private JComboBox cmb1 = new JComboBox();
final private JTextArea txt2 = new JTextArea("");
final private JLabel lblLine = new JLabel("a compenent on all the line");
final private JButton btnOK = new JButton("OK");
final private JButton btnCancel = new JButton("Cancel");
public GridBagLayoutExample2() {
GridBagLayout bl = new GridBagLayout();
Container pane = getContentPane();
pane.setLayout(bl);
GridBagConstraints c = new GridBagConstraints();
int r = 0;
placeComponentInGridBagLayout(lbl1, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(cmb1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl2, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt2, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl3, pane, bl, c, 0, r, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lblLine, pane, bl, c, 0, r++, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 2, null, null, null);
placeComponentInGridBagLayout(btnOK, pane, bl, c, 0, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(btnCancel, pane, bl, c, 1, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
setSize(new Dimension(850, 350));
pack();
setVisible(true);
}
public static void placeComponentInGridBagLayout(Component component, Container container, GridBagLayout bagLayout, GridBagConstraints c, Integer gridX, Integer gridY, Double weightX, Double weightY, Integer anchor, Integer fill, Integer ipadx, Integer ipady,
Integer gridWidth, Integer gridHeight) {
if (c == null) {
c = new GridBagConstraints();
}
if (gridX != null) {
c.gridx = gridX;
}
if (gridY != null) {
c.gridy = gridY;
}
if (weightX != null) {
c.weightx = weightX;
}
if (weightY != null) {
c.weighty = weightY;
}
if (fill != null) {
c.fill = fill;
}
if (anchor != null) {
c.anchor = anchor;
}
if (ipadx != null) {
c.ipadx = ipadx;
}
if (ipady != null) {
c.ipady = ipady;
}
if (gridWidth != null) {
c.gridwidth = gridWidth;
}
if (gridHeight != null) {
c.gridheight = gridHeight;
}
bagLayout.setConstraints(component, c);
container.add(component);
}
}
Any idea what I'm doing wrong?
Also is there a more modern way of achieving the same thing in Swing?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当做一个简单的表单时, SpringLayout 是一个有用的布局管理器。要将组件打包在一行上,使用 BoxLayout。
这是一个简单的示例:
这是代码:
您可能想要使用 BorderLayout 例如将
formPanel
保持在顶部而不拉伸时调整窗口大小。但我希望我给了你一些关于如何将 SpringLayout 用于表单的想法。When doing a simple form, SpringLayout is a useful layout manager. And to pack components on a line, it's useful to use BoxLayout.
Here is a quick example:
And here is the code:
You may want to use BorderLayout to e.g. keep the
formPanel
at the top without stretching when resizing the window. But I hop I gave you some ideas on how to use SpringLayout for forms.我发现的一个问题是,您的长标签与“确定”按钮位于同一网格单元格中。
如果您希望按钮锚定在整个对话框的底部,请尝试使用
BorderLayout
。为按钮创建一个单独的面板并将其放入BorderLayout.SOUTH
中。然后使用 GridBagLayout 将主要内容放入另一个面板中,并将其放入 BorderLayout.CENTER 中。这些按钮不会与您的文本组件完美对齐,但我认为没关系。One problem I can see is that your long label is going in the same grid cell as your OK button.
If you want the buttons anchored at the bottom of the entire dialog, try using a
BorderLayout
. Make a separate panel for the buttons and put that inBorderLayout.SOUTH
. Then put your main stuff in another panel with theGridBagLayout
, and put that inBorderLayout.CENTER.
The buttons won't be perfectly aligned with your text components, but I think that's OK.您可以使用 borderlayout 和 gridlayout 的组合,如下所示
You can use a combination of borderlayout and gridlayout as shown below
用于这些目的的一个不错的
LayoutManager
是FormLayout
< /a> 来自 JGoodies。A nice
LayoutManager
for those purposes is theFormLayout
from JGoodies.