【发布时间】:2015-10-08 12:10:38
【问题描述】:
使用下面的代码,我正在使用 JTextFields 和 JLabels 制作一个 JPanel,并将该面板添加到另一个 JPanel。如何调整 infoPanel 上 JTextField 之间的间距?
我尝试了 GridBagLayout 和 GridLayout,但结果不同且不理想。现在的方式至少使它们垂直对齐,但我似乎无法在它们上方和下方添加空间。有没有这方面的大师可以提供帮助?
public DrawPanelMain() {
JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
JPanel infoPanel = new JPanel();
JPanel fields = new JPanel();
//Text boxes for infoPanel
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
JTextField textField3 = new JTextField(20);
JTextField textField4 = new JTextField(20);
JTextField textField5 = new JTextField(20);
JTextField textField6 = new JTextField(20);
//JLabels for infoPanel
JLabel jLabel1 = new JLabel("Serial Number: ");
JLabel jLabel2 = new JLabel("Information: ");
JLabel jLabel3 = new JLabel("Information: ");
JLabel jLabel4 = new JLabel("Information: ");
JLabel jLabel5 = new JLabel("Information: ");
JLabel jLabel6 = new JLabel("Information: ");
//These are the buttons that will be added to the btnPanel
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));
//Fields that will be added to infoPanel
fields.add(jLabel1);
fields.add(textField1);
fields.add(jLabel2);
fields.add(textField2);
fields.add(jLabel3);
fields.add(textField3);
fields.add(jLabel4);
fields.add(textField4);
fields.add(jLabel5);
fields.add(textField5);
fields.add(jLabel6);
fields.add(textField6);
//Sets border padding for the infoPanel
fields.setBorder(new EmptyBorder(20, 20, 0, 20));
//Draws border for the infoPanel
infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//Sets layout for the fields panel
fields.setLayout(new GridLayout(6, 1));
//Add fields to infoPanel
infoPanel.add(fields);
//Add panels to tabbedPane
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
add(infoPanel, BorderLayout.EAST);
}
【问题讨论】:
-
请看这篇关于providing whitespaces in a Swing GUI的帖子。希望它会对这个主题有所帮助:-) 简单来说,为每个
Layout关注点使用重载的构造函数。 -
@nIcEcOw 你这个男人,这正是我一直在寻找的!
标签: java swing whitespace