我用 Java 编写了这个计算器程序,我想为其添加更多功能。我希望程序做的是使用 JFrame 重新调整 JButtons / JPanels 的大小。这意味着当用户展开 JPanel 或全屏时,我希望按钮能够随 JFrame 适当调整大小。
public class Calculator implements ActionListener {
JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[14];
JButton addButton,subButton,mulButton,divButton;
JButton decButton,equButton,delButton,clrButton,negButton;
JButton perButton,absButton,piButton,sqrButton,srootButton;
JPanel panel;
Font myFont = new Font ("Helvetica",Font.BOLD,30);
double num1,num2,result = 0;
char operator;
int height = 620;
int width = 520;
Calculator(){
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setLayout(null);
textfield = new JTextField();
textfield.setBounds(50, 25, 400, 60);
textfield.setFont(myFont);
textfield.setEditable(false);
textfield.setBorder(null);
addButton = new JButton(add);
addButton.setBackground(functionColor);
subButton = new JButton(sub);
subButton.setBackground(functionColor);
mulButton = new JButton(mul);
mulButton.setBackground(functionColor);
divButton = new JButton(div);
divButton.setBackground(functionColor);
equButton = new JButton(equ);
equButton.setBackground(functionColor);
decButton = new JButton(".");
decButton.setBackground (numberColor);
decButton.setForeground (Color.WHITE);
clrButton = new JButton("AC");
clrButton.setBackground (top);
clrButton.setForeground (Color.BLACK);
negButton = new JButton("+/-");
negButton.setBackground (top);
negButton.setForeground (Color.BLACK);
delButton = new JButton (delete);
delButton.setBackground (top);
delButton.setForeground (Color.BLACK);
perButton = new JButton ("%");
perButton.setBackground (top);
perButton.setForeground (Color.BLACK);
absButton = new JButton ("|x|");
absButton.setBackground (top);
absButton.setForeground (Color.BLACK);
piButton = new JButton ("𝝅");
piButton.setBackground (top);
piButton.setForeground (Color.BLACK);
sqrButton = new JButton ("x²");
sqrButton.setBackground (top);
sqrButton.setForeground (Color.BLACK);
srootButton = new JButton ("√x");
srootButton.setBackground (top);
srootButton.setForeground (Color.BLACK);
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;
functionButtons[9] = perButton;
functionButtons[10]= piButton;
functionButtons[11]= absButton;
functionButtons[12]= sqrButton;
functionButtons[13]= srootButton;
for(int i = 0; i < 14; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}
JPanel secPan = new JPanel();
secPan.setBounds(50,160,400,50);
secPan.setLayout(new GridLayout(1,4,10,10));
secPan.add(piButton);
secPan.add(perButton);
secPan.add(sqrButton);
secPan.add(srootButton);
frame.add(secPan);
frame.setVisible(true);
JPanel topPan = new JPanel();
topPan.setBounds(50,100,400,50);
topPan.setLayout(new GridLayout(1,4,10,10));
topPan.add(negButton);
topPan.add(absButton);
topPan.add(clrButton);
topPan.add(delButton);
frame.add(topPan);
frame.setVisible(true);
panel = new JPanel();
panel.setBounds(50, 220, 400, 300);
panel.setLayout(new GridLayout(4,4,10,10));
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[0]);
panel.add(decButton);
panel.add(equButton);
panel.add(divButton);
for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i].setBackground(numberColor);
numberButtons[i].setForeground(Color.WHITE);
}
frame.add(panel);
frame.add(textfield);
frame.setVisible(true);
}
}
首先花时间阅读和探索在容器内布置组件
请注意,以下不是“最简单”的解决方案,而是最灵活的解决方案之一。您还可以结合使用
BorderLayout
和 GridLayout
来实现类似的效果。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new CalculatorPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CalculatorPane extends JPanel {
public enum CalculatorAction {
DIVIDE, MULTIPLY, SUBTRACT, ADD, EQUALS;
public String getText() {
switch (this) {
case DIVIDE: return "/";
case MULTIPLY: return "x";
case SUBTRACT: return "-";
case ADD: return "+";
case EQUALS: return "=";
}
return null;
}
}
public CalculatorPane() {
setBackground(Color.GRAY);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
int[][] values = new int[][] {
{ 7, 8, 9 },
{ 4, 5, 6 },
{ 1, 2, 3 },
};
for (int row = 0; row < values.length; row++) {
gbc.gridx = 0;
for (int col = 0; col < values[row].length; col++) {
add(makeNumericButton(values[row][col]), gbc);
gbc.gridx++;
}
gbc.gridy++;
}
gbc.gridx = 0;
gbc.gridwidth = 3;
add(makeNumericButton(0), gbc);
gbc.gridx = 3;
gbc.gridy = 1;
add(makeActionButton(CalculatorAction.DIVIDE), gbc);
gbc.gridy++;
add(makeActionButton(CalculatorAction.MULTIPLY), gbc);
gbc.gridy++;
add(makeActionButton(CalculatorAction.SUBTRACT), gbc);
gbc.gridy++;
add(makeActionButton(CalculatorAction.ADD), gbc);
gbc.gridy++;
add(makeActionButton(CalculatorAction.EQUALS), gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField field = new JTextField(5);
field.setBackground(Color.DARK_GRAY);
field.setForeground(Color.WHITE);
field.setBorder(null);
field.setHorizontalAlignment(JTextField.TRAILING);
field.setFont(field.getFont().deriveFont(48f));
add(field, gbc);
}
protected JButton makeButton(Action action) {
JButton button = new JButton(action);
button.setContentAreaFilled(false);
button.setOpaque(true);
button.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(16, 16, 16, 16)));
return button;
}
protected JButton makeNumericButton(int value) {
JButton button = makeButton(new NumericAction(value));
button.setBackground(Color.DARK_GRAY);
button.setForeground(Color.WHITE);
button.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(16, 16, 16, 16)));
return button;
}
protected JButton makeActionButton(CalculatorAction value) {
JButton button = makeButton(new OperationAction(value));
button.setBackground(Color.ORANGE);
button.setForeground(Color.WHITE);
button.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(16, 16, 16, 16)));
return button;
}
protected class NumericAction extends AbstractAction {
private int value;
public NumericAction(int value) {
this.value = value;
putValue(NAME, Integer.toString(value));
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
protected class OperationAction extends AbstractAction {
private CalculatorAction value;
public OperationAction(CalculatorAction value) {
this.value = value;
putValue(NAME, value.getText());
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
}