gpt4 book ai didi

Java Swing JTextfield 调整大小(高度)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:30 28 4
gpt4 key购买 nike

我在将程序中的 JTextfield 设置为适当大小时遇到​​了一些问题,我正在尝试使用文本“Price must be a real value”修改文本字段。这就是我希望它看起来像的样子:

enter image description here

这是我现在的样子:

enter image description here

我的代码如下:

package test1;

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;

public class Test1{

//Should components be final? Should they be defined outside of the class?
private final JTextArea outputArea = new JTextArea();
private final JTextField errorReportField = new JTextField();

private final JPanel inputPanel = new JPanel();

private final JLabel nameLabel = new JLabel("Item Name");
private final JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private final JLabel priceLabel = new JLabel("Price per unit (or L) in pence");

private final JTextField nameField = new JTextField(10);
private final JTextField numberField = new JTextField(10);
private final JTextField priceField = new JTextField(10);

private final JButton addVolumeButton = new JButton("Add by Volume");
private final JButton addNumberButton = new JButton("Add by number of units");

public Test1() {
JFrame frame = new JFrame("Fuel station shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

outputArea.setEditable(false);
outputArea.setRows(30);
JScrollPane scrollPanel = new JScrollPane(outputArea);
scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
errorReportField.setEditable(false);

//Better way of adding multiple components to panel?
inputPanel.setLayout(new FlowLayout());
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();
//Why is it adding components from bottom to top?
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(scrollPanel);
contentPane.add(errorReportField);
contentPane.add(inputPanel);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
Test1 test = new Test1();
}

}

所以基本上我想增加文本字段的高度,同时保持文本居中,以及将背景更改为白色同时保持其不可编辑。

最佳答案

使用 BorderLayout 而不是 BoxLayout

将文本区域放在 BorderLayout.CENTER 中,并使用 GridLayout(0, 1) 将文本字段和底部面板包裹在另一个面板中。将该面板添加到 BorderLayout.PAGE_END。文本字段将与底部面板的大小相同

放在一边


重构

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Test1{

//Should components be final? Should they be defined outside of the class?
private final JTextArea outputArea = new JTextArea();
private final JTextField errorReportField = new JTextField();

private final JPanel inputPanel = new JPanel();

private final JLabel nameLabel = new JLabel("Item Name");
private final JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private final JLabel priceLabel = new JLabel("Price per unit (or L) in pence");

private final JTextField nameField = new JTextField(10);
private final JTextField numberField = new JTextField(10);
private final JTextField priceField = new JTextField(10);

private final JButton addVolumeButton = new JButton("Add by Volume");
private final JButton addNumberButton = new JButton("Add by number of units");

public Test1() {
JFrame frame = new JFrame("Fuel station shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//outputArea.setEditable(false);
outputArea.setRows(30);
JScrollPane scrollPanel = new JScrollPane(outputArea);
scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//errorReportField.setEditable(false);

//Better way of adding multiple components to panel?
inputPanel.setLayout(new FlowLayout());
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();
//Why is it adding components from bottom to top?
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPanel);
JPanel wrapper = new JPanel(new GridLayout(0, 1));
wrapper.add(errorReportField);
wrapper.add(inputPanel);
contentPane.add(wrapper, BorderLayout.PAGE_END);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
Test1 test = new Test1();
}
});
}
}

注意

如果您希望字段稍微大一点(在高度方面),您可以向 inputPanel 添加一个空边框,从而使字段也扩展。但这意味着 inputPanel 也会更大一些(可能需要也可能不需要)。但无论哪种方式,对于 GridLayout,字段和 inputPanel 的大小都将相同。

inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

另一种选择是给 wrapper 面板一个 BorderLayout 并将字段添加到中心,给它一个 EmptyBorder 来扩展它的尺寸。

contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPanel);
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(errorReportField);
wrapper.add(inputPanel, BorderLayout.PAGE_END);

Border border = errorReportField.getBorder();
CompoundBorder compound = BorderFactory.createCompoundBorder(
border,BorderFactory.createEmptyBorder(10, 10, 10, 10));
errorReportField.setBorder(compound);

contentPane.add(wrapper, BorderLayout.PAGE_END);

这将在字段左侧留出少量空白(这可能是理想的,也可能不是理想的)。如果您不想要它,只需更改空边框的值即可。

createEmptyBorder(top, left, bottom, right)

关于Java Swing JTextfield 调整大小(高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551910/

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