我正在构建Java GUI计算器。用户界面很复杂,我正在使用布局而不是拖放来实现。
除外,一切正常 1。mode_error_label.setPreferredSize(new Dimension(46, 55));
2。backButton.setPreferredSize(new Dimension(45, 55));
为什么高度未设置为55?
我想念什么?
查看图像的左上角和右上角。
我的结果:
所需结果:
public class CalculatorViewController extends JPanel {
private JTextField display1;
private JTextField display2;
private JLabel mode_error_label;
private JButton dotButton;
public CalculatorViewController() {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLACK));
mode_error_label = new JLabel("F", JLabel.CENTER);
mode_error_label.setPreferredSize(new Dimension(46, 55));
mode_error_label.setBackground(Color.YELLOW);
mode_error_label.setOpaque(true);
mode_error_label.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));
JButton backButton = new JButton(Character.toString('\u21DA'));
backButton.setPreferredSize(new Dimension(45, 55));
backButton.setBackground(Color.YELLOW);
backButton.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
backButton.setToolTipText("Backspace (Alt-B)");
display1 = new JTextField(16);
display1.setEditable(false);
display1.setHorizontalAlignment(JTextField.RIGHT);
display1.setBackground(Color.WHITE);
display1.setBorder(BorderFactory.createEmptyBorder());
display2 = new JTextField(16);
display2.setEditable(false);
display2.setHorizontalAlignment(JTextField.RIGHT);
display2.setBackground(Color.WHITE);
display2.setBorder(BorderFactory.createEmptyBorder());
display2.setText("0.0");
Box displayBox = Box.createVerticalBox();
displayBox.add(display1);
displayBox.add(display2);
Box upperBox = Box.createHorizontalBox();
upperBox.add(mode_error_label);
upperBox.add(displayBox);
upperBox.add(backButton);
upperBox.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
JCheckBox modeCheckBox = new JCheckBox("int");
modeCheckBox.setPreferredSize(new Dimension(40, 0));
modeCheckBox.setBackground(Color.green);
JRadioButton _0RadioButton = new JRadioButton(".0", false);
_0RadioButton.setBackground(Color.YELLOW);
JRadioButton _00RadioButton = new JRadioButton(".00", true);
_00RadioButton.setBackground(Color.YELLOW);
JRadioButton sciRadioButton = new JRadioButton("Sci", false);
sciRadioButton.setBackground(Color.YELLOW);
ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(_0RadioButton);
radioButtonGroup.add(_00RadioButton);
radioButtonGroup.add(sciRadioButton);
Box lowerBox = Box.createHorizontalBox();
lowerBox.add(modeCheckBox);
lowerBox.add(Box.createGlue());
lowerBox.add(_0RadioButton);
lowerBox.add(_00RadioButton);
lowerBox.add(sciRadioButton);
lowerBox.setBackground(Color.BLACK);
lowerBox.setOpaque(true);
lowerBox.setBorder(BorderFactory.createMatteBorder(5, 0, 5, 0, Color.black));
JPanel lowerPanel = new JPanel();
lowerPanel.add(lowerBox);
lowerPanel.setBackground(Color.BLACK);
lowerPanel.setOpaque(true);
lowerPanel.setBorder(BorderFactory.createEmptyBorder());
Box superBox = Box.createVerticalBox();
superBox.add(upperBox);
superBox.add(lowerBox);
this.add(superBox, BorderLayout.PAGE_START);
}
}
我搜索了它,但没有发现运气。任何帮助,将不胜感激。谢谢
答案 0 :(得分:0)
对于具有返回按钮和标签的顶部栏,将容器布局设置为边框布局。边框布局应将组件调整为适合其容器的大小。或者,您可以发布显示问题的代码,我们可以尝试解决您的问题。祝你好运。
public class CalculatorViewController extends JPanel {
private JTextField display1;
private JTextField display2;
private JLabel mode_error_label;
private JButton dotButton;
private JPanel topPanel;
public CalculatorViewController() {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLACK));
mode_error_label = new JLabel("F", JLabel.CENTER);
mode_error_label.setPreferredSize(new Dimension(46, 55));
mode_error_label.setBackground(Color.YELLOW);
mode_error_label.setOpaque(true);
mode_error_label.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));
JButton backButton = new JButton(Character.toString('\u21DA'));
backButton.setPreferredSize(new Dimension(45, 55));
backButton.setBackground(Color.YELLOW);
backButton.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
backButton.setToolTipText("Backspace (Alt-B)");
display1 = new JTextField(16);
display1.setEditable(false);
display1.setHorizontalAlignment(JTextField.RIGHT);
display1.setBackground(Color.WHITE);
display1.setBorder(BorderFactory.createEmptyBorder());
display2 = new JTextField(16);
display2.setEditable(false);
display2.setHorizontalAlignment(JTextField.RIGHT);
display2.setBackground(Color.WHITE);
display2.setBorder(BorderFactory.createEmptyBorder());
display2.setText("0.0");
Box displayBox = Box.createVerticalBox();
displayBox.add(display1);
displayBox.add(display2);
Box upperBox = Box.createHorizontalBox();
topPanel.add(mode_error_label, BorderLayout.WEST);
topPanel.add(displayBox, BorderLayout.CENTER);
topPanel.add(backButton, BorderLayout.EAST);
upperBox.add(topPanel);
upperBox.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
JCheckBox modeCheckBox = new JCheckBox("int");
modeCheckBox.setPreferredSize(new Dimension(40, 0));
modeCheckBox.setBackground(Color.green);
JRadioButton _0RadioButton = new JRadioButton(".0", false);
_0RadioButton.setBackground(Color.YELLOW);
JRadioButton _00RadioButton = new JRadioButton(".00", true);
_00RadioButton.setBackground(Color.YELLOW);
JRadioButton sciRadioButton = new JRadioButton("Sci", false);
sciRadioButton.setBackground(Color.YELLOW);
ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(_0RadioButton);
radioButtonGroup.add(_00RadioButton);
radioButtonGroup.add(sciRadioButton);
Box lowerBox = Box.createHorizontalBox();
lowerBox.add(modeCheckBox);
lowerBox.add(Box.createGlue());
lowerBox.add(_0RadioButton);
lowerBox.add(_00RadioButton);
lowerBox.add(sciRadioButton);
lowerBox.setBackground(Color.BLACK);
lowerBox.setOpaque(true);
lowerBox.setBorder(BorderFactory.createMatteBorder(5, 0, 5, 0, Color.black));
JPanel lowerPanel = new JPanel();
lowerPanel.add(lowerBox);
lowerPanel.setBackground(Color.BLACK);
lowerPanel.setOpaque(true);
lowerPanel.setBorder(BorderFactory.createEmptyBorder());
Box superBox = Box.createVerticalBox();
superBox.add(upperBox);
superBox.add(lowerBox);
this.add(superBox, BorderLayout.PAGE_START);
}
}