📄 caculator.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public final class Caculator extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField tf1,tf2,tf3;
JButton changeButton,plus,minus,multiply,divide,mod;
double n1,n2,n3;
boolean change=true;
Caculator(){
super("简单计算器");
changeButton=new JButton("换肤");
changeButton.addActionListener(new ButtonListener());
plus=new JButton("+");
minus=new JButton("-");
multiply=new JButton("*");
divide=new JButton("/");
mod=new JButton("mod");
tf1=new JTextField(10);
tf2=new JTextField(10);
tf3=new JTextField(10);
changeSkin();
this.setSize(450,270);
this.setVisible(true);
this.setResizable(false);
addListener();
}
public void changeSkin(){
if(change==true){
setContentPane(skin());
change=false;
}
else{
setContentPane(skin2());
change=true;
}
}
private JPanel skin(){
JPanel contentpanel=(JPanel)getContentPane();
contentpanel.removeAll();
JPanel panel1=new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(tf1);
JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(5,1,0,5));
panel2.add(plus);
panel2.add(minus);
panel2.add(multiply);
panel2.add(divide);
panel2.add(mod);
panel1.add(panel2);
panel1.add(tf2);
JLabel label1=new JLabel("=");
panel1.add(label1);
panel1.add(tf3);
contentpanel.setLayout(new FlowLayout(0));
JPanel panel0=new JPanel();
panel0.add(changeButton);
contentpanel.add(panel0);
contentpanel.add(panel1);
return contentpanel;
}
private JPanel skin2(){
JPanel contentpanel=(JPanel)getContentPane();
contentpanel.removeAll();
JLabel lb1=new JLabel("第一个数:");
JPanel panel1=new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.LEFT,15,0));
panel1.add(lb1);
panel1.add(tf1);
JLabel lb2=new JLabel("运算:");
JPanel panel2=new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.LEFT,28,0));
panel2.add(lb2);
panel2.add(plus);
panel2.add(minus);
panel2.add(multiply);
panel2.add(divide);
panel2.add(mod);
JLabel lb3=new JLabel("第二个数:");
JPanel panel3=new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEFT,15,0));
panel3.add(lb3);
panel3.add(tf2);
JLabel lb4=new JLabel("结果:");
JPanel panel4=new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.LEFT,28,0));
panel4.add(lb4);
panel4.add(tf3);
JPanel content=new JPanel();
content.setLayout(new GridLayout(4,1,0,15));
content.add(panel1);
content.add(panel2);
content.add(panel3);
content.add(panel4);
contentpanel.setLayout(new FlowLayout(0,5,15));
contentpanel.add(changeButton);
contentpanel.add(content);
return contentpanel;
}
public void addListener() {
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try{
int flag=1;
JButton source=new JButton();
source=(JButton)e.getSource();
n1=Double.parseDouble(tf1.getText());
n2=Double.parseDouble(tf2.getText());
if(source==plus) n3=n1+n2;
else if(source==minus) n3=n1-n2;
else if(source==multiply) n3=n1*n2;
else if(source==divide){
if(n2==0){
tf3.setText("无意义");
flag=0;
}
else
n3=n1/n2;
}
else{
if(n2==0){
tf3.setText("无意义");
flag=0;
}
else{
int yu;
yu=((int)n1)%((int)n2);
tf3.setText(Integer.toString(yu));
flag=0;
}
}
if(flag==1) tf3.setText(Double.toString(n3));
}catch(Exception ex){tf3.setText("可能未初始化或不是数字");}
}
}
Listener listener=new Listener();
plus.addActionListener(listener);
minus.addActionListener(listener);
multiply.addActionListener(listener);
divide.addActionListener(listener);
mod.addActionListener(listener);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
changeSkin();
}
}
public static void main(String[] args){
new Caculator();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -