我想制作一个包含三个文本字段的GUI和一个使用Jlabel显示的结果,这些结果与文本字段垂直对齐。但是,我还想为此标签添加边框,以使边框的大小固定,并且与文本字段的大小相同。如果标签溢出边框,则没问题。它现在看起来像这样:GUI 我手动添加空格以保持空间,但是我仍然无法使标签与文本字段对齐相同的宽度,任何人都知道请帮助解决问题。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestScorePanel extends JPanel{
private JPanel p5, label_panel,text_panel;
private JButton calculate_average;
private JLabel length1_label, length2_label, length3_label,output_Label,result_Label;
private JTextField length1, length2, length3;
public TestScorePanel(){
length1_label = new JLabel("Enter length #1");
length2_label = new JLabel("Enter length #2");
length3_label = new JLabel("Enter length #3");
output_Label = new JLabel("Average");
result_Label = new JLabel(" ");//spaces used to hold the space
length1 = new JTextField(5);
length2 = new JTextField(5);
length3 = new JTextField(5);
calculate_average = new JButton("Calculate Average");
calculate_average.addActionListener(new ScoreListener());
p5 = new JPanel();
label_panel = new JPanel(); // left side labels
text_panel = new JPanel(); // right side labels
label_panel.setLayout(new BoxLayout(label_panel, BoxLayout.Y_AXIS));//set vertical layout for panel
text_panel.setLayout(new BoxLayout(text_panel, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));//set
result_Label.setBorder(BorderFactory.createLineBorder(Color.black));
//setMinimumSize donesn's seem to work the way I wanted.
result_Label.setMinimumSize(new Dimension(400,20));
p5.setBorder(BorderFactory.createTitledBorder("Enter Three lengths"));
p5.add(label_panel);
p5.add(Box.createRigidArea(new Dimension (50,0)));
p5.add(text_panel);
add(p5);
label_panel.add(length1_label);
text_panel.add(length1);
label_panel.add(Box.createRigidArea(new Dimension (0, 10)));
text_panel.add(Box.createRigidArea(new Dimension (0, 10)));
label_panel.add(length2_label);
text_panel.add(length2);
label_panel.add(Box.createRigidArea(new Dimension (0, 10)));
text_panel.add(Box.createRigidArea(new Dimension (0, 10)));
label_panel.add(length3_label);
text_panel.add(length3);
label_panel.add(Box.createRigidArea(new Dimension (0, 10)));
text_panel.add(Box.createRigidArea(new Dimension (0, 10)));
label_panel.add(output_Label);
text_panel.add(result_Label);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(Box.createRigidArea(new Dimension (0, 10)));
add(calculate_average);
setPreferredSize(new Dimension(320,220));
}
private class ScoreListener implements ActionListener{
public void actionPerformed(ActionEvent event){
int len1, len2, len3, average;
len1 = Integer.parseInt(length1.getText());
len2 = Integer.parseInt(length2.getText());
len3 = Integer.parseInt(length3.getText());
average= (len1+ len2+ len3)/3;
if (event.getSource() == calculate_average)
result_Label.setText(Integer.toString(average));
}
}
}
答案 0 :(得分:3)
考虑使用不同的布局管理器,它可以让您更好地控制组件的布局方式,例如GridBagLayout
nb:文本字段周围的填充是由MacOS提供的,在这种情况下,我考虑使用不可编辑的文本字段
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Enter Length #1"), gbc);
gbc.gridy++;
add(new JLabel("Enter Length #2"), gbc);
gbc.gridy++;
add(new JLabel("Enter Length #3"), gbc);
gbc.gridy++;
add(new JLabel("Enter Length #4"), gbc);
gbc.gridy++;
add(new JLabel("Average"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
JLabel average = new JLabel(" ");
average.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(average, gbc);
答案 1 :(得分:-1)
设置结果字段的首选大小,如下所示
result_Label.setPreferredSize(length1.getPreferredSize());