我在将程序中的JTextfield调整到合适的大小时遇到了一些问题,我正在尝试使用文本“Price必须是真正的价值”修改文本字段。这就是我希望它看起来像:
这就是我目前的样子:
我的代码如下:
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();
}
}
所以基本上我想在保持文本居中的同时增加文本字段的高度,以及将背景更改为白色,同时保持文本不可编辑。
答案 0 :(得分:2)
使用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)
答案 1 :(得分:1)
如果要增加字段高度,请修改其首选大小(将方法应用于您的字段):
private void increaseFieldHeight(JTextField field, int times) {
Dimension d = field.getPreferredSize();
d.height = d.height * times;
field.setPreferredSize(d);
}