📄 jlab1002a.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JLab1002A extends JFrame implements ActionListener
{
private TextField operation; // 结果栏
private Button dot, plus, minus, multi, div, sqrt, equal, dao,M,MS,MR,MC,c1,backs,ce,sign,yu; // 运算符
private Button[] numbers; // 数字
// 构造者
public JLab1002A()
{
setTitle("计算器");
// 初始化变量
// 初始化窗口变量
operation = new TextField(" 0",250);
operation.setEditable(false);
numbers = new Button[10];
for (int i = 0; i < 10; i++)
numbers[i] = new Button("" + i);
backs=new Button("Backplace");
ce=new Button("CE");
c1=new Button("C");
dot = new Button(".");
plus = new Button("+");
minus = new Button("-");
multi = new Button("*");
sign=new Button("+/-");
div = new Button("/");
sqrt = new Button("sqrt");
equal = new Button("=");
dao = new Button("1/x");
M = new Button("M+");
MS=new Button("MS+");
MR=new Button("MR+");
MC=new Button("MC+");
yu=new Button("%");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, numbers[1], 2, 1, 1, 1);
addComponent(layout, numbers[2], 2, 2, 1, 1);
addComponent(layout, numbers[3], 2, 3, 1, 1);
addComponent(layout, numbers[4], 3, 1, 1, 1);
addComponent(layout, numbers[5], 3, 2, 1, 1);
addComponent(layout, numbers[6], 3, 3, 1, 1);
addComponent(layout, numbers[7], 4, 1, 1, 1);
addComponent(layout, numbers[8], 4, 2, 1, 1);
addComponent(layout, numbers[9], 4, 3, 1, 1);
addComponent(layout, div,2,4,1,1);
addComponent(layout, sqrt,2,5,1,1);
addComponent(layout, multi,3,4,1,1);
addComponent(layout, yu,3,5,1,1);
addComponent(layout, minus,4,4,1,1);
addComponent(layout, dao,4,5,1,1);
addComponent(layout, numbers[0],5,1,1,1);
addComponent(layout, sign,5,2,1,1);
addComponent(layout, dot,5,3,1,1);
addComponent(layout, plus,5,4,1,1);
addComponent(layout, equal,5,5,1,1);
addComponent(layout, operation, 0, 0, 6, 1);
addComponent(layout, backs,1,1,2,1);
addComponent(layout, ce,1,3,2,1);
addComponent(layout, c1,1,5,1,1);
addComponent(layout, MC,2,0,1,1);
addComponent(layout, MR,3,0,1,1);
addComponent(layout, MS,4,0,1,1);
addComponent(layout, M,5,0,1,1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
Button btn = (Button)e.getSource();
if(btn==numbers[1]) operation.setText(String.valueOf(1));
if(btn==numbers[2]) operation.setText(String.valueOf(2));
if(btn==numbers[3]) operation.setText(String.valueOf(3));
if(btn==numbers[4]) operation.setText(String.valueOf(4));
if(btn==numbers[5]) operation.setText(String.valueOf(5));
if(btn==numbers[6]) operation.setText(String.valueOf(6));
if(btn==numbers[7]) operation.setText(String.valueOf(7));
if(btn==numbers[8]) operation.setText(String.valueOf(8));
if(btn==numbers[9]) operation.setText(String.valueOf(9));
if(btn==numbers[0]) operation.setText(String.valueOf(0));
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(1, 1, 1, 1);
constraints.weightx = 1;
constraints.weighty = 1;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof Button)
((Button)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
JLab1002A calc = new JLab1002A();
calc.setSize(500,500);
calc.setVisible(true);
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -