📄 computer.txt
字号:
JAVA程序__计算器
Computer.java 执行程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public class Computer implements ActionListener
{
private Frame f = new Frame("计算器");
private Panel p = new Panel();
private Panel p1 = new Panel();
private Panel p2 = new Panel();
private Panel p3 = new Panel();
private AboutDialog d ;
private HelpDialog d1 ;
private Button b[] = new Button[21];
private Button b1[] = new Button[4];
private Button b2[] = new Button[5];
private TextField tf = new TextField();
private TextField tf1 = new TextField(1);
private String s,oper,s2;
private boolean flag = true;
private double result, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
private Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run()
{
s = "";
oper = "";
s2 = "";
//tf.setEditable(false);
tf1.setEditable(false);
MenuBar mb = new MenuBar();
f.setLayout(new BorderLayout(5,5));
f.setBackground(Color.lightGray);
f.setLocation(300,200);
p.setLayout(new BorderLayout(3,3));
p.setBackground(Color.lightGray);
p1.setLayout(new GridLayout(4,5));
p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
p3.setLayout(new GridLayout(4,1));
f.setMenuBar(mb);
Menu m1 = new Menu("编辑(E)");
Menu m2 = new Menu("查看(V)");
Menu m3 = new Menu("帮助(H)");
mb.add(m1);
mb.add(m2);
mb.add(m3);
MenuItem mi1 = new MenuItem("复制(C)",new MenuShortcut(KeyEvent.VK_C));
MenuItem mi2 = new MenuItem("粘贴(V)",new MenuShortcut(KeyEvent.VK_V));
MenuItem mi3 = new MenuItem("退出");
MenuItem mi4 = new MenuItem("科学型(S)");
MenuItem mi5 = new MenuItem("数字分组(T)");
MenuItem mi6 = new MenuItem("关于计算器");
MenuItem mi7 = new MenuItem("帮助主题");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
mi7.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
m2.add(mi4);
m2.addSeparator();
m2.add(mi5);
m3.add(mi6);
m3.addSeparator();
m3.add(mi7);
for(int i=1;i<4;i++)
{
b1 = new Button();
b1.setFont(new Font("仿宋",0,16));
b1.setBackground(Color.lightGray);
b1.setForeground(Color.red);
}
b1[1].setLabel(" 退格 ");
b1[2].setLabel(" CE ");
b1[3].setLabel(" C ");
p2.add(tf1);
for(int i=1;i<4;i++)
{
p2.add(b1);
b1.addActionListener(this);
}
for(int i=1;i<5;i++)
{
b2 = new Button();
b2.setFont(new Font("仿宋",0,16));
b2.setBackground(Color.lightGray);
b2.setForeground(Color.red);
}
b2[1].setLabel("MC");
b2[2].setLabel("MR");
b2[3].setLabel("MS");
b2[4].setLabel("M+");
for(int i=1;i<5;i++)
{
p3.add(b2);
b2.addActionListener(this);
}
for(int i=1;i<21;i++)
{
b = new Button();
b.setFont(new Font("仿宋",0,16));
}
b[1].setLabel("7");
b[2].setLabel("8");
b[3].setLabel("9");
b[4].setLabel("/");
b[5].setLabel("sqrt");
b[6].setLabel("4");
b[7].setLabel("5");
b[8].setLabel("6");
b[9].setLabel("*");
b[10].setLabel("%");
b[11].setLabel("1");
b[12].setLabel("2");
b[13].setLabel("3");
b[14].setLabel("-");
b[15].setLabel("1/x");
b[16].setLabel("0");
b[17].setLabel("+/-");
b[18].setLabel(".");
b[19].setLabel("+");
b[20].setLabel("=");
for(int i=1;i<21;i++)
{
p1.add(b);
b.addActionListener(this);
b.setBackground(Color.lightGray);
b.setForeground(Color.blue);
}
for(int i=4;i<15;i=i+5)
{
b.setBackground(Color.lightGray);
b.setForeground(Color.red);
}
for(int i=17;i<21;i++)
{
b.setBackground(Color.lightGray);
b.setForeground(Color.blue);
}
for(int i=19;i<21;i++)
{
b.setBackground(Color.lightGray);
b.setForeground(Color.red);
}
for(int i=1;i<4;i++)
{
b[i*5].setBackground(Color.lightGray);
b[i*5].setForeground(Color.blue);
}
f.add(tf,"North");
p.add(p1,"Center");
p.add(p2,"North");
p.add(p3,"West");
f.add(p,"Center");
f.pack();
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
}
public void actionPerformed(ActionEvent e)
{
s = e.getActionCommand();
String s1 = tf.getText();
if(s=="0"|s=="1"|s=="2"|s=="3"|s=="4"|s=="5"|s=="6"|s=="7"|s=="8"|s=="9")
{
if(flag)
{
tf.setText(s1 + s);
}
else
{
tf.setText(s);
flag = true;
}
}
else if(s=="+"|s=="-"|s=="*"|s=="/")
{
result = Double.parseDouble(s1);
flag = false;
oper = s;
}
else if(s=="=")
{
tmp1 = Double.parseDouble(tf.getText());
if(oper == "+") result += tmp1;
if(oper == "-") result -= tmp1;
if(oper == "*") result *= tmp1;
if(oper == "/") result /= tmp1;
tf.setText(Double.toString(result));
oper = "";
flag = false;
}
else if(s=="+/-")
{
tmp2 = -1*Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp2));
}
else if(s=="sqrt")
{
tmp3 = Double.parseDouble(tf.getText());
if(tmp3>0)
{
tmp3 = Math.sqrt(tmp3);
tf.setText(Double.toString(tmp3));
flag = false;
}
else{}
}
else if(s=="1/x")
{
tmp4 = 1/Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp4));
flag = false;
}
else if(s=="%")
{
tmp5 = Double.parseDouble(tf.getText())/100;
if(oper == "*")
{
result = result*(result*tmp5);
tf.setText(Double.toString(result));
}
else if(oper == "+")
{
result = result+result*tmp5;
tf.setText(Double.toString(result));
}
else if(oper == "-")
{
result = result-result*tmp5;
tf.setText(Double.toString(result));
}
else if(oper == "/")
{
result = result/(result*tmp5);
tf.setText(Double.toString(result));
}
else
{
tf.setText("0.0");
}
flag = false;
oper = "";
}
else if(s==" 退格 ")
{
String after = s1.substring(0,(s1.length()-1));
tf.setText(after);
}
else if(s==".")
{
if(s1.indexOf('.')==-1)
{
tf.setText(s1+s);
}
else{}
}
else if(s==" CE "|s==" C ")
{
tf.setText("0");
flag = false;
}
else if(s=="退出")
{
System.exit(1);
}
else if(s=="MS")
{
String before = tf.getText();
tmp7 = Double.parseDouble(before);
s2 = Double.toString(tmp7);
tf1.setText(" M");
flag = false;
}
else if(s=="MR")
{
tf.setText(s2);
flag = false;
}
else if(s=="MC")
{
s2 = "";
tf1.setText("");
}
else if(s=="M+")
{
tmp7 = tmp7 + Double.parseDouble(tf.getText());
s2 = Double.toString(tmp7);
flag = false;
}
else if(s=="关于计算器")
{
d = new AboutDialog(f);
d.display();
}
else if(s=="帮助主题")
{
d1 = new HelpDialog(f);
d1.display();
}
if(s=="复制(C)")
{
String text = tf.getText();
StringSelection selection = new StringSelection(text);
cb.setContents(selection, null);
}
if(s=="粘贴(V)" )
{
Transferable contents = cb.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
try{
String text = (String)contents.getTransferData(flavor);
if (!text.equals(null))
{
tf.setText(text);
}
else
{
tf.setText("0.0");
}
}
catch(UnsupportedFlavorException ef)
{
}
catch(IOException ex)
{
}
}
}
public static void main(String args[])
{
Computer c = new Computer();
c.run();
}
}
-----------------------------------------------------------------
Re:【原创】我自己写的JAVA程序__计算器(普通型)
AboutDialog.java 关于计算器程序
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class AboutDialog extends Dialog implements ActionListener{
private Label l;
private Button b;
private TextArea ta;
private Panel p1;
private Panel p2;
private Panel p3;
private Canvas c1, c2, c3, c4;
private String filename = "xxx.txt";
private String title = "关于计算器";
private String content = "";
public AboutDialog(Frame f){
super(f,true);
this.setTitle(title);
l = new Label("About Calculator V 1.00");
l.setForeground(Color.blue);
l.setFont(new Font("Arial",Font.PLAIN,24));
b = new Button(" 确定 ");
b.addActionListener(this); //(new ButtonHandler());
ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setBackground(Color.lightGray);
ta.setEditable(false);
c1 = new Canvas();
c2 = new Canvas();
c3 = new Canvas();
c4 = new Canvas();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p2.setLayout(new BorderLayout(5,5));
p1.setBackground(Color.lightGray);
p2.setBackground(new Color(130,254,240,200));
p3.setBackground(Color.lightGray);
p1.add(l);
p2.add(ta);
p2.add(c1,"North");
p2.add(c2,"South");
p2.add(c3,"West");
p2.add(c4,"East");
p3.add(b);
this.add(p1,"North");
this.add(p2,"Center");
this.add(p3,"South");
this.setLocation(f.getX() + f.getWidth(), f.getY());
this.addWindowListener(new WindowHandler());
}
public void display(){
this.setSize(350,350);
this.readfromFile(filename);
this.setTextArea();
this.setResizable(false);
this.setVisible(true);
}
public void readfromFile(String s){
try{
BufferedReader br = new BufferedReader(new FileReader(s));
String s1 = br.readLine();
String s2 = "";
while(s1 != null){
s2 = s2 + s1 + "\n";
s1 = br.readLine();
}
br.close();
content = s2;
}catch(IOException e){
e.printStackTrace();
}
}
public void setFilename(String s){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -