Java简易计算器
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Font;
import java.lang.Math;
import javax.swing.SwingConstants;
public class jisuanqi extends JFrame implements ActionListener {
double s1=0,s2=0;//定义两个变量存储数字
String fuhao;//存储运算符
JButton button[],back,CE,C,add,About,Sqrt,addorsub,multiply,sub,divide,point,percent,daoshu,equ;
JLabel screen;//显示屏
//用windowbuilder创建的窗体默认在构造函数中直接新建对象,要实现监听功能,需将对象在jisuanqi类中声明
public jisuanqi(String s) {
super(s);
getContentPane().setBackground(new Color(143, 188, 143));
getContentPane().setLayout(null);//添加absolute layout
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 293);
back= new JButton("Back");
back.setFont(new Font("宋体", Font.PLAIN, 20));
back.setForeground(Color.WHITE);
back.setBackground(SystemColor.infoText);
back.setBounds(25, 40, 93, 30);
back.addActionListener(this);
getContentPane().add(back);
CE = new JButton("CE");
CE.setFont(new Font("宋体", Font.PLAIN, 20));
CE.setBounds(132, 40, 93, 30);
CE.setForeground(Color.WHITE);
CE.setBackground(SystemColor.infoText);
CE.addActionListener(this);
getContentPane().add(CE);
C = new JButton("C");
C.setFont(new Font("宋体", Font.PLAIN, 20));
C.setBounds(239, 40, 93, 30);
C.setForeground(Color.WHITE);
C.setBackground(SystemColor.infoText);
C.addActionListener(this);
getContentPane().add(C);
About = new JButton("About");
About.setFont(new Font("宋体", Font.PLAIN, 20));
About.setBounds(346, 40, 93, 30);
About.setForeground(Color.WHITE);
About.setBackground(SystemColor.infoText);
About.addActionListener(this);
getContentPane().add(About);
JPanel panel = new JPanel();
panel.setBackground(SystemColor.inactiveCaption);
panel.setBounds(10, 81, 238, 169);
panel.setLayout(null);
getContentPane().add(panel);
button=new JButton[10];
for(int i=0;i<10;i++){
button[i]=new JButton(String.valueOf(9-i));
button[i].setBounds(14+(9-i)%3*74, 13+(9-i)/3*40, 60, 27);
panel.add(button[i]);
button[i].setForeground(Color.WHITE);
button[i].setBackground(SystemColor.infoText);
button[i].setFont(new Font("宋体", Font.PLAIN, 20));
button[i].addActionListener(this);
}
addorsub=new JButton("+/-");
addorsub.setFont(new Font("宋体", Font.PLAIN, 16));
addorsub.setBounds(88,133,60,27);
addorsub.setForeground(Color.WHITE);
addorsub.setBackground(SystemColor.infoText);
addorsub.addActionListener(this);
panel.add(addorsub);
point=new JButton(".");
point.setFont(new Font("宋体", Font.PLAIN, 20));
point.setBounds(162,133,60,27);
point.setForeground(Color.WHITE);
point.setBackground(SystemColor.infoText);
point.addActionListener(this);
panel.add(point);
JPanel panel2 = new JPanel();
panel2.setBackground(SystemColor.inactiveCaption);
panel2.setBounds(269, 81, 182, 169);
panel2.setLayout(null);
getContentPane().add(panel2);
divide = new JButton("/");
divide.setFont(new Font("宋体", Font.PLAIN, 20));
divide.setForeground(SystemColor.text);
divide.setBackground(SystemColor.infoText);
divide.setBounds(14, 13, 74, 27);
divide.addActionListener(this);
panel2.add(divide);
Sqrt = new JButton("sqrt");
Sqrt.setFont(new Font("宋体", Font.PLAIN, 20));
Sqrt.setBackground(SystemColor.infoText);
Sqrt.setForeground(SystemColor.text);
Sqrt.setBounds(94, 13, 74, 27);
Sqrt.addActionListener(this);
panel2.add(Sqrt);
multiply = new JButton("*");
multiply.setFont(new Font("宋体", Font.PLAIN, 20));
multiply.setBackground(SystemColor.infoText);
multiply.setForeground(SystemColor.text);
multiply.setBounds(14, 53, 74, 27);
multiply.addActionListener(this);
panel2.add(multiply);
percent = new JButton("%");
percent.setFont(new Font("宋体", Font.PLAIN, 20));
percent.setBackground(SystemColor.infoText);
percent.setForeground(SystemColor.text);
percent.setBounds(94, 53, 74, 27);
percent.addActionListener(this);
panel2.add(percent);
sub = new JButton("-");
sub.setFont(new Font("宋体", Font.PLAIN, 20));
sub.setBackground(SystemColor.infoText);
sub.setForeground(SystemColor.text);
sub.setBounds(14, 91, 74, 27);
sub.addActionListener(this);
panel2.add(sub);
daoshu = new JButton("1/x");
daoshu.setFont(new Font("宋体", Font.PLAIN, 20));
daoshu.setBackground(SystemColor.infoText);
daoshu.setForeground(SystemColor.text);
daoshu.setBounds(94, 93, 74, 27);
daoshu.addActionListener(this);
panel2.add(daoshu);
add = new JButton("+");
add.setFont(new Font("宋体", Font.PLAIN, 20));
add.setBackground(SystemColor.infoText);
add.setForeground(SystemColor.text);
add.setBounds(14, 131, 74, 27);
add.addActionListener(this);
panel2.add(add);
equ = new JButton("=");
equ.setFont(new Font("宋体", Font.PLAIN, 20));
equ.setBackground(SystemColor.infoText);
equ.setForeground(SystemColor.text);
equ.setBounds(94, 133, 74, 27);
equ.addActionListener(this);
panel2.add(equ);
JPanel panel_2 = new JPanel();
panel_2.setBackground(SystemColor.control);
panel_2.setBounds(10, 5, 441, 30);
panel_2.setLayout(null);
getContentPane().add(panel_2);
screen = new JLabel("");
screen.setHorizontalAlignment(SwingConstants.RIGHT);
screen.setFont(new Font("宋体", Font.PLAIN, 20));
screen.setBounds(0, 0, 441, 30);
panel_2.add(screen);
Toolkit kit=Toolkit.getDefaultToolkit();//设置窗体居中显示
Dimension screenSize=kit.getScreenSize();
int screenWidth=screenSize.width/2;
int screenHeight=screenSize.height/2;
int windowWidth=getWidth();
int windowHeight=getHeight();
setLocation(screenWidth-windowWidth/2,screenHeight-windowHeight/2);
setVisible(true);//窗口的可见性设置
}
@Override
public void actionPerformed(ActionEvent e) {//计算器功能实现
// TODO Auto-generated method stub
if(e.getSource()==back) {
String ss=screen.getText();
screen.setText(ss==""?"":ss.substring(0,ss.length()-1));
}
else if(e.getSource()==CE) {
screen.setText("");
}
else if(e.getSource()==C) {
screen.setText("");
s1=0;
s2=0;
}
else if(e.getSource()==About) {//不知道About有什么功能,既然题目要求写了,只能用下面的方法处理了~
screen.setText("此功能正在开发中,敬请期待……");
}
else if(e.getSource()==addorsub) {
String ss=screen.getText();
if(ss.contains("-"))
screen.setText(ss.substring(1,ss.length()));
else
screen.setText("-"+screen.getText());
}
else if(e.getSource()==point) {
String ss=screen.getText();
screen.setText(ss+".");
}
else if(e.getSource()==divide) {
s1=Double.parseDouble((screen.getText()));
screen.setText("");
fuhao="/";
}
else if(e.getSource()==add) {
s1=Double.parseDouble((screen.getText()));
screen.setText("");
fuhao="+";
}
else if(e.getSource()==sub) {
s1=Double.parseDouble((screen.getText()));
screen.setText("");
fuhao="-";
}
else if(e.getSource()==multiply) {
s1=Double.parseDouble((screen.getText()));
screen.setText("");
fuhao="*";
}
else if(e.getSource()==Sqrt) {
s1=Double.parseDouble((screen.getText()));
screen.setText("");
screen.setText(Math.sqrt(s1)+"");
}
else if(e.getSource()==daoshu) {
s1=Double.parseDouble((screen.getText()));
if(s1==0) {
screen.setText("Error:0没有倒数");
}
else {
screen.setText(1/s1+"");
}
}
else if(e.getSource()==percent) {
s1=Double.parseDouble((screen.getText()));
screen.setText(s1*100+"%");
}
else if(e.getSource()==equ) {
s2=Double.parseDouble((screen.getText()));
switch (fuhao) {
case "+":{s1=s1+s2;break;}
case "-":{s1=s1-s2;break;}
case "*":{s1=s1*s2;break;}
case "/":{s1=s1/s2;break;}
}
screen.setText(s1+"");
if(s2==0) {
screen.setText("Error:0不能做除数");
}
}
else {
for(int i=0;i<10;i++) {
if(e.getSource()==button[i]) {
String ss=screen.getText();
screen.setText(ss+button[i].getText());
}
}
}
}
}
public class MainClass {
public static void main(String[] args) {
jisuanqi jsq=new jisuanqi("简易计算器");//新建名为简易计算器的窗口
}
}
效果图: