简体   繁体   English

JLabel和JButton setPreferredSize无法正常工作

[英]JLabel & JButton setPreferredSize not working properly

I'm building a Java GUI calculator. 我正在构建一个Java GUI计算器。 UI is complex and I'm doing it using Layouts instead of drag and drop. 用户界面很复杂,我正在使用布局而不是拖放来实现。 Everything is working fine except 一切正常,除了

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

Why is height not set to 55? 为什么高度不设置为55?

What am I missing? 我想念什么?

Look at the top left and top right corner of the image. 查看图像的左上角和右上角。

My Result: 我的结果:

https://i.stack.imgur.com/5Uey8.jpg

Desired Result: 所需结果:

https://i.stack.imgur.com/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);
}
}

I searched for it but found no luck. 我搜索了它,但没有发现运气。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

For the top bar where you have the back button and label, set the container layout a border layout. 对于具有后退按钮和标签的顶部栏,将容器布局设置为边框布局。 Border layout should resize the component to its container. 边框布局应将组件调整为适合其容器的大小。 Or you can post a code that shows the problem and we can try to solve your problem. 或者,您可以发布显示问题的代码,我们可以尝试解决您的问题。 Good luck. 祝好运。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM