我正在创建一个简单的计算器GUI。此外,我有一个问题,调整帮助和退出按钮大小的数字按钮的大小。我知道我对GridLayout
做错了,但不确定它是什么。代码没有问题。
这就是它的样子:
public static final int WIDTH = 350;
public static final int HEIGHT = 500;
public static final int NUMBER_OF_CHAR = 4;
private JTextField single;
private JTextField equation;
boolean setBlank = false;
Double result;
String resultStr;
String verifyAction = "";
public Calculator()
{
super("Calculator");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,0,0,0));
JPanel top = new JPanel();
top.setLayout(new GridLayout(1,2));
JPanel mid = new JPanel();
mid.setLayout(new GridLayout(2,1));
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(4,4));
//TOP
JButton exit = new JButton("Exit");
exit.addActionListener(this);
top.add(exit);
JButton help = new JButton("Help");
help.addActionListener(this);
top.add(help);
//MID
single = new JTextField(NUMBER_OF_CHAR);
mid.add(single);
equation = new JTextField(NUMBER_OF_CHAR);
mid.add(equation);
//BOTTOM
String [] key = {"7","8","9","+","4","5","6","","1","2","3","/","0",".","clear","="};
for(int i = 0 ; i < key.length;i++)
{
JButton button = new JButton(key[i]);
button.addActionListener(this);
bottom.add(button);
}
add(top,BorderLayout.NORTH);
add(mid,BorderLayout.CENTER);
add(bottom,BorderLayout.SOUTH);
}
答案 0 :(得分:2)
这是一个主要问题:setSize(WIDTH, HEIGHT);
。不要这样做,因为这会导致您不想重新调整大小的组件。而是在添加所有组件之后,通过在顶部窗口JFrame上调用pack()
来调整所有组件的大小为其自己的首选大小。再次,这将让GUI大小本身。
不要将GridLayout用于JPanel,而是将其保留为BorderLayout,并使用GUI组件字体。例如
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
public static final int WIDTH = 350;
public static final int HEIGHT = 500;
public static final int NUMBER_OF_CHAR = 4;
public static final Font MY_FONT = new Font(Font.DIALOG, Font.BOLD, 24);
private JTextField single;
private JTextField equation;
boolean setBlank = false;
Double result;
String resultStr;
String verifyAction = "";
public Calculator() {
super("Calculator");
// setSize(WIDTH, HEIGHT); // !! No ***************
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setLayout(new GridLayout(3, 0, 0, 0)); // !! No ***************
JPanel top = new JPanel();
top.setLayout(new GridLayout(1, 2));
JPanel mid = new JPanel();
mid.setLayout(new GridLayout(2, 1));
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(4, 4));
// TOP
JButton exit = new JButton("Exit");
exit.setFont(MY_FONT);
exit.addActionListener(this);
top.add(exit);
JButton help = new JButton("Help");
help.setFont(MY_FONT);
help.addActionListener(this);
top.add(help);
// MID
single = new JTextField(NUMBER_OF_CHAR);
single.setFont(MY_FONT);
mid.add(single);
equation = new JTextField(NUMBER_OF_CHAR);
equation.setFont(MY_FONT);
mid.add(equation);
// BOTTOM
String[] key = { "7", "8", "9", "+", "4", "5", "6", "", "1", "2", "3", "/", "0", ".", "clear", "=" };
for (int i = 0; i < key.length; i++) {
JButton button = new JButton(key[i]);
button.setFont(MY_FONT);
button.addActionListener(this);
bottom.add(button);
}
add(top, BorderLayout.NORTH);
add(mid, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Calculator().setVisible(true);
;
});
}
}