1 Star 0 Fork 118

唐睿/0421_tangrui_calculator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
计算器第三次提交 5.37 KB
一键复制 编辑 原始数据 按行查看 历史
唐睿 提交于 2021-06-08 23:19 . add 计算器第三次提交.
package java2020spring;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private static final String COMMANDS = null;
private static final Icon[] COMMAND = null;
private static final String M = null;
public static void main(String[] args) {
Calculator calculator=new Calculator();
calculator.setVisible(true);
}
public Calculator() throws HeadlessException {
super("Caculator");
resultText.setAlignmentX(RIGHT_ALIGNMENT);
this.setTitle(Const.TITLE);
this.setSize(Const.FRAME_W,Const.FRAME_H);
this.setLayout(new BorderLayout());
this.setLocation(Const.FRAME_X,Const.FRAME_Y);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
resultText.setBounds(20,18,255,115);
resultText.setAlignmentX(RIGHT_ALIGNMENT);
resultText.setEditable(false);
History.setBounds(290,40,250,370);
History.setAlignmentX(LEFT_ALIGNMENT);
History.setEditable(false);
label.setBounds(400,15,100,20);
j1.setBounds(20,18,320,115);
j1.setLayout(new GridLayout());
j2.setBounds(380,40,250,380);
j2.setLayout(new GridLayout());
resultText.setLineWrap(true);
resultText.setWrapStyleWord(true);
History.setLineWrap(true);
History.setWrapStyleWord(true);
g1.setViewportView(resultText);
g1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
g2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
j1.add(g1);
j2.add(g2);
this.add(j1);
this.add(j2);
this.setLayout(null);
this.add(label);
int x=20,y=150;
for (int i=0;i<KEYS.length;i++)
{
keys[i]=new JButton();
keys[i].setText(KEYS[i]);
keys[i].setBounds(x,y,60,40);
if(x<250) {
x+=65;
}
else {
x=20;
y+=45;
}
this.add(keys[i]);
}
for (int i=0;i<KEYS.length;i++)
{
keys[i].addActionListener(this);
}
this.setResizable(false);
this.setBounds(500,200,670,480);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void init() {
resultText.setBackground(Color.WHITE);
JPanel calckeysPanel=new JPanel();
resultText.setBackground(Color.WHITE);
calckeysPanel.setLayout(new GridLayout(5,5,3,3));
for(int i=0;i<keys.length;i++) {
keys[i]=new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.BLACK);
keys[i].addActionListener(this);
this.setBackground(Color.LIGHT_GRAY);
this.pack();
}
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
private String[] KEYS= {"7","8","9","+","sqrt","4","5","6","-","%","1","2","3",
"*","1/x","+/-","0","=","/",".","(",")","e","x*x","Bac","C","MC",
"MR","MS","M+"};
private JButton keys[]=new JButton[KEYS.length];
private JTextArea resultText=new JTextArea("0");
private JTextArea History=new JTextArea();
private JPanel j1=new JPanel();
private JPanel j2=new JPanel();
private JScrollPane g1=new JScrollPane(resultText);
private JScrollPane g2=new JScrollPane(History);
private JLabel label=new JLabel("历史记录");
private String a="";
private JButton[] commands2;
private Array[] ms;
private JButton[] ms2;
public void actionPerformed(ActionEvent e) {
String label=e.getActionCommand();
if(label=="=")
{
resultText.setText(this.a);
History.setText(History.getText()+resultText.getText());
if(label=="=")
{
String s[]=houzhui(this.a);
String result=Result(s);
this.a=result+"";
resultText.setText(this.a);
History.setText(History.getText()+"="+resultText.getText()+"\n");
}
}
else if(label=="AC")
{
this.a="";
resultText.setText("0");
}
else if(label=="sqrt")
{
String n=k(this.a);
resultText.setText("sqqrt"+"("+this.a+")"+"="+n);
History.setText(History.getText()+"sqrt"+"("+this.a+")"+"=");
this.a=n;
}
else if(label=="x*x")
{
String m=p(this.a);
resultText.setText(this.a+"^2"+"="+m);
History.setText(History.getText()+this.a+"^2"+"=");
this.a=m;
}
else if(label=="e"||label=="pi")
{
if(label=="e")
{
String m=String.valueOf(2.7182818);
this.a=this.a+m;
resultText.setText(this.a);
}
if(label=="pi")
{
String m=String.valueOf(3.1415926);
this.a=this.a+m;
resultText.setText(this.a);
}
}
else
{
this.a=this.a+label;
resultText.setText(this.a);
}
}
private String p(String a2) {
// TODO 自动生成的方法存根
return null;
}
private String k(String a2) {
// TODO 自动生成的方法存根
return null;
}
private String Result(String[] s) {
// TODO 自动生成的方法存根
return null;
}
private String[] houzhui(String a2) {
// TODO 自动生成的方法存根
return null;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tang-rui2021/java2021spring.git
[email protected]:tang-rui2021/java2021spring.git
tang-rui2021
java2021spring
0421_tangrui_calculator
master

搜索帮助