gpt4 book ai didi

java - JLabel 和 JButton setPreferredSize 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:40 25 4
gpt4 key购买 nike

我正在构建一个 Java GUI 计算器。 UI 很复杂,我使用布局而不是拖放来完成它。一切正常,除了

1.mode_error_label.setPreferredSize(new Dimension(46, 55)); 2.backButton.setPreferredSize(new Dimension(45, 55));

为什么高度不设置为55?

我错过了什么?

查看图像的左上角和右上角。

我的结果:

/image/5Uey8.jpg

期望的结果:

/image/eoDwk.jpg

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);
}
}

我搜索了它,但没有找到运气。任何帮助,将不胜感激。谢谢

最佳答案

对于具有后退按钮和标签的顶栏,将容器布局设置为边框布局。边框布局应根据其容器调整组件的大小。或者您可以发布显示问题的代码,我们可以尝试解决您的问题。祝你好运。

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);
}

}

关于java - JLabel 和 JButton setPreferredSize 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52821492/

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