gpt4 book ai didi

java - 在 BoxLayout 中对齐 JLabel

转载 作者:行者123 更新时间:2023-12-04 21:27:58 26 4
gpt4 key购买 nike

我这几天一直在寻找这个问题的答案,但我找不到问题所在。我想要做的是使顶部 JLabel(称为 display)向右对齐,底部 JLabel(称为 notice)向左对齐。似乎两者都不想这样做。从我读过的内容来看,我所拥有的应该可以工作,但事实并非如此。帮忙?

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class Calculator {

private static JButton clear, add, subtract, multiply, divide, equals, point, zero, one, two, three, four, five, six, seven, eight, nine;
private static JLabel display, notice, blank1, blank2, blank3;
private static JPanel mainPanel, buttonPanel, topLabel, bottomLabel;

public static void goGUI() {

JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(600,300));
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.setContentPane(mainPanel);
Border empty = BorderFactory.createEmptyBorder(10,10,10,10);
mainPanel.setBorder(empty);

buttonPanel = new JPanel(new GridLayout(5,4, 5,5));
Border buttonBorder = BorderFactory.createEmptyBorder(10,0,10,0);
buttonPanel.setBorder(buttonBorder);

topLabel = new JPanel();
bottomLabel = new JPanel();

clear = new JButton("C");
add = new JButton("+");
subtract = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equals = new JButton("=");
point = new JButton(".");
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");

// Here I added ActionListeners to all the buttons...

display = new JLabel("0");
display.setAlignmentX(Component.RIGHT_ALIGNMENT);
notice = new JLabel("*Maximum 19 digits - Order of operations not taken into account*");
notice.setAlignmentX(Component.LEFT_ALIGNMENT);
blank1 = new JLabel();
blank2 = new JLabel();
blank3 = new JLabel();

buttonPanel.add(clear);
buttonPanel.add(blank1);
buttonPanel.add(blank2);
buttonPanel.add(blank3);
buttonPanel.add(seven);
buttonPanel.add(eight);
buttonPanel.add(nine);
buttonPanel.add(divide);
buttonPanel.add(four);
buttonPanel.add(five);
buttonPanel.add(six);
buttonPanel.add(multiply);
buttonPanel.add(one);
buttonPanel.add(two);
buttonPanel.add(three);
buttonPanel.add(subtract);
buttonPanel.add(zero);
buttonPanel.add(point);
buttonPanel.add(equals);
buttonPanel.add(add);

topLabel.add(display);
bottomLabel.add(notice);

mainPanel.add(topLabel);
mainPanel.add(buttonPanel);
mainPanel.add(bottomLabel);

frame.pack();
frame.setVisible(true);

} //end goGUI

//ActionListener classes went here...

public static void main(String[] args) {

try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {}

goGUI();

} //end main
} //end Calculator

为了清楚起见,我删除了所有 ActionListener 的东西。但这是我无法修复的布局。

最佳答案

考虑让 topLabel 不使用默认的 FlowLayout,而是使用使其内容填满它的东西,例如 BorderLayout。

topLabel.setLayout(new BorderLayout());

接下来确保设置显示器的水平对齐方式,而不是右对齐:

display.setHorizontalAlignment(SwingConstants.RIGHT);

应为您的通知 JLabel 及其容器进行类似的更改。

关于java - 在 BoxLayout 中对齐 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602237/

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