我正在尝试使用基本的 JPanel 布局,JGoodies.FormLayout
但我遇到了一个问题,即无论我为解决该问题所做的任何事情,我放置的 JTextField 组件都保持在最小尺寸。无需再费周折,
Program.java
:
package example;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import example.gui.*;
public class Program {
private static JTabbedPane tabbedViewControl;
private static TabPanel someTabPanel;
private static void createAndShowGUI() {
JFrame mainFrame = new JFrame("Stack Overflow Example");
mainFrame.setResizable(true);
mainFrame.setSize(1200, 800);
mainFrame.setTitle("Example");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
tabbedViewControl = new JTabbedPane(JTabbedPane.TOP);
someTabPanel = new TabPanel();
tabbedViewControl.addTab("Example Tab", someTabPanel);
mainFrame.getContentPane().add(tabbedViewControl);
//Display the window.
mainFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
}
example.gui.TabPanel.java
:
package example.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class TabPanel extends JPanel {
private static Border textFieldBorderStyle;
private static ButtonGroup userChoiceButtonGroup;
private static PanelBuilder builder;
private static FormLayout layout;
private static JLabel descriptionLabel;
private static JLabel userChoiceLabel;
private static JLabel nameLabel;
private static JLabel data2Label;
private static JLabel data1Label;
private static JRadioButton userChoice1RadioButton;
private static JRadioButton userChoice3RadioButton;
private static JRadioButton userChoice4RadioButton;
private static JRadioButton userChoice2RadioButton;
private static JRadioButton userChoice5RadioButton;
private static JTextArea descriptionField;
private static JTextField nameField;
private static JTextField data2Field;
private static JTextField data1Field;
private static JPanel subPanel;
public TabPanel() {
super(new GridBagLayout());
initComponents();
}
private void initComponents() {
nameLabel = new JLabel("Name");
nameField = new JTextField();
descriptionLabel = new JLabel("Description");
descriptionField = new JTextArea("");
data1Label = new JLabel("Data No. 1");
data1Field = new JTextField();
data2Label = new JLabel("Data No. 2");
data2Field = new JTextField();
userChoiceLabel = new JLabel("User choices");
userChoiceButtonGroup = new ButtonGroup();
userChoice1RadioButton = new JRadioButton("Choice I");
userChoice3RadioButton = new JRadioButton("Choice II");
userChoice4RadioButton = new JRadioButton("Choice III");
userChoice2RadioButton = new JRadioButton("Choice IV");
userChoice5RadioButton = new JRadioButton("Choice V");
subPanel = constructPanel();
this.add(subPanel);
}
private JPanel constructPanel() {
layout = new FormLayout(
"r:p, 3dlu, l:max(1in;p), 7dlu, r:p, 3dlu, l:p, 3dlu, l:p, 3dlu, l:p",
"p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"
);
builder = new PanelBuilder(layout);
CellConstraints cc = new CellConstraints();
textFieldBorderStyle = nameField.getBorder();
descriptionField.setBorder(textFieldBorderStyle);
userChoiceButtonGroup.add(userChoice1RadioButton);
userChoiceButtonGroup.add(userChoice3RadioButton);
userChoiceButtonGroup.add(userChoice4RadioButton);
userChoiceButtonGroup.add(userChoice2RadioButton);
userChoiceButtonGroup.add(userChoice5RadioButton);
builder.addSeparator("Name & Description", cc.xyw(1, 1, 3));
builder.add(nameLabel, cc.xy (1, 3));
builder.add(nameField, cc.xy (3, 3));
builder.add(descriptionLabel, cc.xy (1, 5));
builder.add(descriptionField, cc.xywh(3, 5, 1, 4));
builder.addSeparator("Other Data for the User to Enter", cc.xyw(5, 1, 5));
builder.add(data1Label, cc.xy(5, 3));
builder.add(data1Field, cc.xy(7, 3));
builder.add(data2Label, cc.xy(5, 5));
builder.add(data2Field, cc.xy(7, 5));
builder.add(userChoiceLabel, cc.xy(5, 7));
builder.add(userChoice2RadioButton, cc.xy(7, 7));
builder.add(userChoice5RadioButton, cc.xy(7, 9));
builder.add(userChoice3RadioButton, cc.xy(7, 11));
builder.add(userChoice4RadioButton, cc.xy(9, 7));
builder.add(userChoice1RadioButton, cc.xy(9, 9));
return builder.getPanel();
}
}
结果如下:
理想情况下,我希望发生三件事,而不是以上:
- JPanel 从返回
PanelBuilder
以占据其父级的全部尺寸 - JTextFields 和 JTextArea 具有合理的宽度(而不是调整大小*)
- 这两个方面更成比例 - 我不期望 50%/50%,但我不希望右侧将左侧挤压到边界上
我已经为此做了大量的功课,但大多想出了以下非结果:
- 尝试在 SO 上搜索主要会产生“本机”AWT 布局系统的问题
- 浏览JGoodies Forms 白皮书,在给我使用的同时
max(constantSize;preferredSize)
,只帮助调整分隔符的大小,并没有做任何事情:JTextFields
所以,简而言之,有没有办法强制 JTextFields 在 a 中占据其列的给定宽度FormLayout
?