📄 compute.java
字号:
package com.company.计算器小程序;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.TextComponent;
public class Compute implements ActionListener{
private JFrame jframe;
private JPanel jp1,jp2;
private JTextField jtf;
private JButton jb1,jb2;
private int i = 0;
private int a = 0;
private int b = 0;
private int c = 0;
private Button b1 = new Button("7"); // 定义16个按钮
private Button b2 = new Button("8");
private Button b3 = new Button("9");
private Button b4 = new Button("/");
private Button b5 = new Button("4");
private Button b6 = new Button("5");
private Button b7 = new Button("6");
private Button b8 = new Button("*");
private Button b9 = new Button("1");
private Button b10 = new Button("2");
private Button b11 = new Button("3");
private Button b12 = new Button("+");
private Button b13 = new Button("0");
private Button b14 = new Button(".");
private Button b15 = new Button("-");
private Button b16 = new Button("=");
public void ComputeGridLayout(){//计算器界面布局
jframe=new JFrame("Java计算器");
Container c=jframe.getContentPane();
jtf=new JTextField();//创建一个文本框,放置到窗口北面
c.add(jtf,BorderLayout.NORTH);
jtf.setHorizontalAlignment(JTextField.RIGHT);//文本框中的信息右对齐显示
jp1=new JPanel(new GridLayout(4,4));//创建一个中间容器,使用GridLayout布局,放置16个按钮组件
jp1.add(b1);
jp1.add(b2);
jp1.add(b3);
jp1.add(b4);
jp1.add(b5);
jp1.add(b6);
jp1.add(b7);
jp1.add(b8);
jp1.add(b9);
jp1.add(b10);
jp1.add(b11);
jp1.add(b12);
jp1.add(b13);
jp1.add(b14);
jp1.add(b15);
jp1.add(b16);
c.add(jp1,BorderLayout.CENTER);//将中间容器放在窗口中部
jp2=new JPanel();//创建另外一个中间容器,使用FlowLayout布局,即中间容器JPanel的默认布局管理器,放置两个按钮
jb1=new JButton("关闭");
jb2=new JButton("清零");
jp2.add(jb1);
jp2.add(jb2);
c.add(jp2,BorderLayout.SOUTH);//将中间容器放在窗口南部
jframe.setSize(400,300);
//jframe.pack();//获得一个刚好适合组件容纳的窗口
jframe.setVisible(true);
jframe.addWindowListener(new WindowAdapter(){//增加匿名事件适配器类
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public Compute() // 监听按钮
{
ComputeGridLayout();
b1.addActionListener(this); // 7
b2.addActionListener(this); //8
b3.addActionListener(this); // 9
b4.addActionListener(this); // /
b5.addActionListener(this); // 4
b6.addActionListener(this); // 5
b7.addActionListener(this); // 6
b8.addActionListener(this); // *
b9.addActionListener(this); // 1
b10.addActionListener(this); // 2
b11.addActionListener(this); // 3
b12.addActionListener(this); // +
b13.addActionListener(this); // 0
b14.addActionListener(this); // .
b15.addActionListener(this); // -
b16.addActionListener(this); // =
jtf.addActionListener(this); // 注意文本域text也要监听
jb1.addActionListener(this);//窗口下面的“关闭”和“清零”按钮监听
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()!= b4&&e.getSource()!= b8&&e.getSource()!= b12&&e.getSource()!= b15&&e.getSource()!= b16&&
e.getSource()!= jb1&&e.getSource()!= jb2) {//对数字按钮处理
jtf.setText(jtf.getText()+e.getActionCommand()); //getActionCommand()返回按钮表面的信息
}
else if (e.getSource() == b4) { //对运算符号b4、b8、b12、b16进行特殊的处理
a = Integer.parseInt(jtf.getText());//用变量a获取当前文本框中的数值,即第二个数
jtf.setText("");//当输入的是运算符号b4、b8、b12、b16时,在文本框中显示空格
i = 1; //用变量i来标记不同的运算符
}
else if (e.getSource() == b8) {
a = Integer.parseInt(jtf.getText()); //Integer.parseInt(jtf.getText()方法将文本框中的字符串类型包装成整型
jtf.setText("");
i = 2;
}
else if (e.getSource() == b12) {
a = Integer.parseInt(jtf.getText());
jtf.setText("");
i = 3;
}
else if (e.getSource() == b15) {
a = Integer.parseInt(jtf.getText());
jtf.setText("");
i = 4;
}
else if(e.getSource()== jb1){//对"关闭"按钮进行处理
System.exit(0);
}
else if(e.getSource()== jb2){//对"清零"按钮进行处理
jtf.setText("" );
}
else if(e.getSource() == b16) { //当按下“=”时,显示运算结果
b = Integer.parseInt(jtf.getText());
jtf.setText(" ");
if (i == 1) { //注意除的时候余数不能为空
if(b!=0){
c = a / b;
jtf.setText(String.valueOf(c)); //getActionCommand()返回按钮表面的信息
}
else jtf.setText("除数不能为零!");
}
else if (i == 2) {
c = a * b;
jtf.setText(String.valueOf(c));
}
else if (i == 3) {
c = a + b;
jtf.setText(String.valueOf(c));//String.valueOf(c)方法将整型转化为字符串类型
}
else if (i == 4) {
c=a-b;
jtf.setText(String.valueOf(c));
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Compute();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -