JavaSwingUI布局
我正在Swing中创建一个基本的用户界面,希望得到一些帮助。下面是我试图实现的目标的屏幕截图: 我目前的代码如下:JavaSwingUI布局,java,swing,layout-manager,Java,Swing,Layout Manager,我正在Swing中创建一个基本的用户界面,希望得到一些帮助。下面是我试图实现的目标的屏幕截图: 我目前的代码如下: package testui; import java.awt.Container; import javax.swing.*; public class TestUI{ private JTextField outputArea = new JTextField(); private JTextField errorReportArea = new JTe
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();
}
}
看起来是这样的:
所以我要做的是为前两个JTextFields设置一个特定的大小,因为前一个包含多行文本,下面一个只包含一行文本。我不确定在不使用setSize的情况下如何做到这一点,因为有人告诉我,使用这个是不好的编码实践
我还想在底部JPanel中的jlabel、JTextFields和JButtons之间添加一些填充
如果有人能给我一些调整这些组件大小的建议,我将不胜感激。有一些布局管理器可以灵活地执行此操作,例如Mig、Gridbag和 在您的情况下,您将有以下约束:
outputarea
-south
边界约束为从contentPane
errorReportArea
-north
边界从outputarea
的南部被限制为0px,而南部
边界从输入面板
的北部被限制为0px
inputPanel
-north
边界约束为从contentPane
的南边界起##px
GUI构建器(如)将允许您快速完成此操作。您只需将布局放入contentPane,然后设置约束 - 由于希望文本字段是多行的,请使用。JTextFields仅为单行
- 您的组件彼此紧挨着,这与您的预期结果不一样。在调用
frame.pack()
- 寻找任何方法,可以使一个组成部分填补它的总空间;特别是当你想要一些东西来填满一大块空间的时候
- 您可以为JTextFields/JTextAreas设置列数,而不是使用
。只是说说而已setSize()
- 回顾所有这些将帮助您掌握每个布局管理器的功能和用例
MigLayout
管理器创建了一个解决方案
以下是一些建议:
- 将应用程序代码放在构造函数之外;在解决方案中,代码
放置在
initUI()方法中
- 应用程序应在EDT上通过调用
。(请参阅所提供解决方案的EventQueue.invokeLater()
方法。)main()
- 使用现代化、灵活的布局管理器:
、migloayout
或GroupLayout
。 花些时间研究它们,以充分了解布局管理流程。信息技术 很重要的一点是要充分理解这个主题FormLayout
- 缩短标签;使用更多描述性工具提示 相反
谢谢您的建议,很遗憾,我们只在我正在学习的课程中学习了Swing,因此我们只允许这样做use@user3371750是Swing的一部分,文本区域为:+1。但请取出“卸下框架.pack()”。这个框架应该包装好。使用BorderLayout和文本区域的行/列提示应该为OP提供他们想要的*修复。我认为在大多数情况下,你应该使用
frame.pack()
,这样你就不会有空余的空间。谢谢你的建议,我已经将第一个文本字段改为一个文本区域,并使用outputrea.setRows(30)来设置它的大小。在查看布局管理器之后,我还将底部面板的boxlayout更改为FlowLayout,它工作良好,默认情况下还包括组件上的5px填充
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);
}
});
}
}