gpt4 book ai didi

Java swing 布局不做我想做的

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:15:22 25 4
gpt4 key购买 nike

我正在尝试编写我认为应该易于指定的对话框,我有一些标签和一些文本区域,然后是一个确定和取消按钮。在中间我有一个宽组件(例如我使用了一个标签)

 |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 中实现同样的事情吗?

谢谢

最佳答案

做简单表格时,SpringLayout是一个有用的布局管理器。并在一条线上打包组件,使用 BoxLayout 很有用.

这是一个简单的例子:

enter image description here

代码如下:

public class LayoutDemo extends JFrame {

public LayoutDemo() {

JLabel label1 = new JLabel("label 1");
JLabel label2 = new JLabel("label 2");
JLabel label3 = new JLabel("label 3");
JComboBox<String> combobox = new JComboBox<>();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();

JPanel formPanel = new JPanel(new SpringLayout());
formPanel.add(label1);
formPanel.add(combobox);
formPanel.add(label2);
formPanel.add(field1);
formPanel.add(label3);
formPanel.add(field2);

SpringUtilities.makeCompactGrid(formPanel, 3, 2, 2, 2, 3, 3);

JLabel longLabel = new JLabel("This is a longer label");
longLabel.setAlignmentX(CENTER_ALIGNMENT);

JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.add(button1);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(button2);

JPanel basePanel = new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.PAGE_AXIS));
basePanel.add(formPanel);
basePanel.add(longLabel);

add(basePanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new LayoutDemo();
}
}

您可能想使用 BorderLayout例如在调整窗口大小时,将 formPanel 保持在顶部而不拉伸(stretch)。但我希望我给了你一些关于如何将 SpringLayout 用于表单的想法。

关于Java swing 布局不做我想做的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com