[英]Java Swing UI Layout
I am creating a basic user interface in Swing and was hoping for some help. 我正在Swing中创建一个基本的用户界面,希望获得一些帮助。 Below is a screenshot of what I am trying to achieve:
以下是我要实现的屏幕截图:
My code currently is as follows: 我的代码当前如下:
package testui;
import java.awt.Container;
import javax.swing.*;
public class TestUI{
private JTextField outputArea = new JTextField();
private JTextField errorReportArea = new JTextField();
private JPanel inputPanel = new JPanel();
private JLabel nameLabel = new JLabel("Item Name");
private JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private JLabel priceLabel = new JLabel("Price per unit (Or L) in pence");
private JTextField nameField = new JTextField(10);
private JTextField numberField = new JTextField(10);
private JTextField priceField = new JTextField(10);
private JButton addVolumeButton = new JButton("Add by Volume");
private JButton addNumberButton = new JButton("Add by number of units");
public TestUI() {
JFrame frame = new JFrame("Fuel Station");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputArea.setEditable(false);
errorReportArea.setEditable(false);
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(addVolumeButton);
inputPanel.add(addNumberButton);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(outputArea);
contentPane.add(errorReportArea);
contentPane.add(inputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
TestUI test1 = new TestUI();
}
}
Which looks like this: 看起来像这样:
So what I would like to do is set a specific size for the top two JTextFields, as the top one will contain multiple lines of text, and the one below will contain just one line of text. 所以我想为顶部的两个JTextField设置一个特定的大小,因为顶部的JTextField将包含多行文本,而下面的JTextField将仅包含一行文本。 I am unsure how to do this without using setSize, as I have been told it is bad coding practice to use this.
我不确定在不使用setSize的情况下如何执行此操作,因为有人告诉我,使用此功能是不好的编码做法。
I would also like to add some padding between the JLabels, JTextFields and JButtons in the bottom JPanel. 我还想在底部JPanel的JLabel,JTextField和JButton之间添加一些填充。
If anyone could give me some suggestions on resizing these components I would be most grateful 如果有人可以给我一些调整这些组件大小的建议,我将不胜感激
frame.pack()
frame.pack()
之前,可能有一些方法为组件提供了喘息的空间。 setSize()
for your JTextFields/JTextAreas. setSize()
。 Just saying. There are a few layout managers that are flexible enough to perform this, such as Mig, Gridbag, and SpringLayout . 有一些布局管理器足够灵活地执行此操作,例如Mig,Gridbag和SpringLayout 。
In your case, you'd have the following constraints: 就您而言,您将受到以下限制:
outputarea
- south
border constrained to be ###px from the north border of the contentPane
outputarea
south
边界限制为距contentPane
北部边界### px
errorReportArea
- north
border constrained to be 0px from outputarea
's south, and south
border constrained to be 0px from inputPanel
's north. errorReportArea
north
边界距离outputarea
的南部为0px, south
边界距离inputPanel
的北部为0px。
inputPanel
- north
border constrained to be ##px from the south border of the contentPane
. inputPanel
north
边界限制为距contentPane
的南边界## px。
GUI builders such as WindowBuilder will allow you to do this pretty quickly. 诸如WindowBuilder之类的 GUI构建器将使您能够快速完成此操作。 You just drop in the layout onto the contentPane and then set the constraints.
您只需将布局放到contentPane上,然后设置约束即可。
I have created a solution with the MigLayout
manager. 我已经使用
MigLayout
管理器创建了一个解决方案。
Here are some recommendations: 以下是一些建议:
initUI()
method. initUI()
方法中。 EventQueue.invokeLater()
. EventQueue.invokeLater()
在EDT上启动应用程序。 (See the main()
method of the provided solution.) main()
方法。) MigLayout
, GroupLayout
, or FormLayout
. MigLayout
, GroupLayout
或FormLayout
。 Take some time to study them to fully understand the layout management process. package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutSolution extends JFrame {
private JTextArea outputArea;
private JTextField errorReportField;
private JLabel nameLabel;
private JLabel numberLabel;
private JLabel priceLabel;
private JTextField nameField;
private JTextField numberField;
private JTextField priceField;
private JButton addVolumeButton;
private JButton addNumberButton;
public MigLayoutSolution() {
initUI();
}
private void initUI() {
setLayout(new MigLayout());
outputArea = new JTextArea(10, 20);
errorReportField = new JTextField(15);
nameLabel = new JLabel("Item name");
numberLabel = new JLabel("# of units");
numberLabel.setToolTipText("Number of units (or Volume in L)");
priceLabel = new JLabel("Price per unit");
priceLabel.setToolTipText("Price per unit (Or L) in pence");
nameField = new JTextField(10);
numberField = new JTextField(10);
priceField = new JTextField(10);
addVolumeButton = new JButton("AddVol");
addVolumeButton.setToolTipText("Add by Volume");
addNumberButton = new JButton("AddNum");
addNumberButton.setToolTipText("Add by number of units");
add(new JScrollPane(outputArea), "grow, push, wrap");
add(errorReportField, "growx, wrap");
add(nameLabel, "split");
add(nameField);
add(numberLabel);
add(numberField);
add(priceLabel);
add(priceField);
add(addVolumeButton);
add(addNumberButton);
pack();
setTitle("Fuel station");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutSolution ex = new MigLayoutSolution();
ex.setVisible(true);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.