1 回答
TA贡献1829条经验 获得超7个赞
您的问题来自未将JFrame 和 JPanel的布局都设置为null,以便setBounds(...)生效。不以这种方式显式设置布局,JPanel 将盲目使用其默认 FlowLayout,而 JFrame(实际上是其 contentPane)将盲目使用其默认 BorderLayout
因此需要进行这些更改:
class PannelDez extends JPanel {
// .....
public PannelDez() {
// Panel
setLayout(null);
// ....
}
// ....
}
class Interface extends JFrame {
public Interface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// PanelAdden
// .....
}
// ..........
}
但话虽如此,我重申我之前说过的话:
虽然空布局和 setBounds() 对 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件的大小,它们是增强或维护的皇家女巫,它们放置在滚动窗格中时完全失败,在所有平台或与原始屏幕分辨率不同的屏幕上查看时,它们看起来很糟糕.
同样,解决这个问题的更好方法是学习和使用布局管理器。例如,您正在创建一个计算器,这很适合使用两个 JPanel,一个用于保存使用 GridLayout 的按钮以创建按钮网格,另一个使用 BorderLayout 来保存上述 JPanel在其中心位置,并将您的 JTextField 保持在顶部位置,BorderLayout.PAGE_START位置。另请注意,最好尽量避免代码重复,例如我们在所有 ActionListener 中看到的。您只需要两个ActionListeners —— 每个数字按钮一个,每个操作一个。
下面的代码创建了一个如上所述的 Swing GUI,一个带有嵌套的 JPanel,外部使用 BorderLayout,内部使用 GridLayout(0, 4),它代表可变行数(0)和 4 列。GUI的图像:
和代码:
导入 java.awt.BorderLayout; 导入 java.awt.Font; 导入 java.awt.GridLayout; 导入 java.awt.event.ActionEvent; 导入 java.awt.event.ActionListener; 导入 javax.swing.*;
@SuppressWarnings("serial")
public class FooGui02 extends JPanel {
private static final Font BUTTON_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 36);
private static final Font ANZEIGE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 24);
private JTextField anzeige = new JTextField(10);
RechnerInt interfaceRechner = new RechnerInt();
public FooGui02() {
anzeige.setFont(ANZEIGE_FONT);
// anzeige.setEnabled(false);
anzeige.setBackground(null);
anzeige.setFocusable(false); // user can't interact with this
// create a grid of 4 columns and variable rows:
JPanel btnPanel = new JPanel(new GridLayout(0, 4));
// create first row
// add 3 blank JLabels
for (int i = 0; i < 3; i++) {
btnPanel.add(new JLabel());
}
btnPanel.add(createOperationButton(Operation.DIVISION));
// second row
btnPanel.add(createNumberButton(7));
btnPanel.add(createNumberButton(8));
btnPanel.add(createNumberButton(9));
btnPanel.add(createOperationButton(Operation.MULTIPLICATION));
// third row
btnPanel.add(createNumberButton(4));
btnPanel.add(createNumberButton(5));
btnPanel.add(createNumberButton(6));
btnPanel.add(createOperationButton(Operation.SUBTRACTION));
// forth row
btnPanel.add(createNumberButton(1));
btnPanel.add(createNumberButton(2));
btnPanel.add(createNumberButton(3));
btnPanel.add(createOperationButton(Operation.ADDITION));
// fifth row
btnPanel.add(new JLabel());
btnPanel.add(createNumberButton(0));
btnPanel.add(createOperationButton(Operation.CLEAR));
btnPanel.add(createOperationButton(Operation.EQUALS));
setLayout(new BorderLayout());
add(anzeige, BorderLayout.PAGE_START);
add(btnPanel);
}
private JButton createOperationButton(Operation operation) {
JButton button = new JButton(operation.getText());
button.addActionListener(new OperationListener(operation));
button.setFont(BUTTON_FONT);
return button;
}
private JButton createNumberButton(int number) {
JButton button = new JButton("" + number);
button.addActionListener(new NumberListener(number));
button.setFont(BUTTON_FONT);
return button;
}
private class OperationListener implements ActionListener {
private Operation operation;
public OperationListener(Operation operation) {
this.operation = operation;
}
@Override
public void actionPerformed(ActionEvent e) {
String text = anzeige.getText();
switch (operation) {
case CLEAR:
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
anzeige.setText("");
break;
case ADDITION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("+");
anzeige.setText("");
break;
case SUBTRACTION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("-");
anzeige.setText("");
break;
case MULTIPLICATION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("*");
anzeige.setText("");
break;
case DIVISION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("/");
anzeige.setText("");
break;
case EQUALS:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("=");
anzeige.setText("" + interfaceRechner.berechneInt());
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
break;
default:
break;
}
}
}
private class NumberListener implements ActionListener {
private int number;
public NumberListener(int number) {
this.number = number;
}
@Override
public void actionPerformed(ActionEvent e) {
String text = anzeige.getText();
text += number;
anzeige.setText(text);
}
}
private static void createAndShowGui() {
FooGui02 mainPanel = new FooGui02();
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
enum Operation {
DIVISION("/"), MULTIPLICATION("*"), SUBTRACTION("-"),
ADDITION("+"), CLEAR("C"), EQUALS("=");
private String text;
private Operation(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
添加回答
举报