java最后一次实验内容。。。
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.IOException;
public class computer implements ActionListener {
private JFrame frame=new JFrame("简单计算器");
private Panel p=new Panel();
private Panel p1=new Panel();
private Panel p2=new Panel();
private Panel p3=new Panel();
private MenuBar mb=new MenuBar();
private Button button[]=new Button[21];
private Button button1[]=new Button[4];
private Button button2[]=new Button[5];
private JTextField tf=new JTextField();
private TextField tf1=new TextField();
//定义操作数和操作符
private String str1,str2,opr;
private boolean flag=true;
//定义中间缓存变量
private double result, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
private Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
public void com( )
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(5,5));
frame.setBackground(Color.LIGHT_GRAY);
frame.setLocation(300,200);
//设置又对齐
tf.setHorizontalAlignment(SwingConstants.RIGHT);
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));
frame.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.add(mi5);
m3.add(mi6);
m3.add(mi7);
//定义Backspace,CE,C,""
for(int i=1;i<4;i++)
{
button1[i]=new Button();
button1[i].setFont(new Font("仿宋",0,16));
button1[i].setBackground(Color.lightGray);
button1[i].setForeground(Color.red);
}
button1[1].setLabel("Backspace");
button1[2].setLabel("CE");
button1[3].setLabel(" C ");
tf1.setEnabled(false);
p2.add(tf1);
for(int i=1;i<4;i++)
{
p2.add(button1[i]);
button1[i].addActionListener(this);
}
for(int i=1;i<21;i++)
{
button[i]=new Button();
button[i].setFont(new Font("仿宋",0,16));
button[i].setBackground(Color.LIGHT_GRAY);
button[i].setForeground(Color.BLUE);
}
button[1].setLabel("7");
button[2].setLabel("8");
button[3].setLabel("9");
button[4].setLabel("/");
button[5].setLabel("sqrt");
button[6].setLabel("4");
button[7].setLabel("5");
button[8].setLabel("6");
button[9].setLabel("*");
button[10].setLabel("%");
button[11].setLabel("1");
button[12].setLabel("2");
button[13].setLabel("3");
button[14].setLabel("-");
button[15].setLabel("1/x");
button[16].setLabel("0");
button[17].setLabel("+/-");
button[18].setLabel(".");
button[19].setLabel("+");
button[20].setLabel("=");
for(int i=1;i<21;i++)
{
p1.add(button[i]);
button[i].addActionListener(this);
button[i].setBackground(Color.LIGHT_GRAY);
button[i].setForeground(Color.BLUE);
}
for(int i=4;i<21;i=i+5)
{
button[i].setForeground(Color.RED);
}
button[20].setForeground(Color.RED);
for(int i=1;i<5;i++)
{
button2[i]=new Button();
button2[i].setFont(new Font("仿宋",0,16));
button2[i].setBackground(Color.lightGray);
button2[i].setForeground(Color.red);
}
button2[1].setLabel("MC");
button2[2].setLabel("MR");
button2[3].setLabel("MS");
button2[4].setLabel("M+");
for(int i=1;i<5;i++)
{
p3.add(button2[i]);
button2[i].addActionListener(this);
}
frame.getContentPane().add(tf,"North");
p.add(p1,"Center");
p.add(p2,"North");
p.add(p3,"West");
frame.getContentPane().add(p,"Center");
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
str1=e.getActionCommand();
String str3=tf.getText();
if(str1=="1"|str1=="2"|str1=="3"|str1=="4"|str1=="5"|str1=="6"|str1=="7"|str1=="8"|str1=="9"|str1=="0")
{
if(flag)
{
tf.setText(str3+str1);
}
else
{
tf.setText(str1);
flag=true;
}
}
else if(str1=="+"|str1=="-"|str1=="*"|str1=="/")
{
result=Double.parseDouble(tf.getText());
flag=false;
opr=str1;
}
else if(str1=="=")
{
String strtmp;
String tempstr="";
String str0;
int loc;
boolean ok;
tmp1=Double.parseDouble(tf.getText());
if(opr=="+")result+=tmp1;
if(opr=="-")result-=tmp1;
if(opr=="*")result*=tmp1;
if(opr=="/")result/=tmp1;
strtmp=Double.toString(result);
loc=strtmp.indexOf(".");
str0=strtmp.substring(loc+1);
if(Double.parseDouble(str0)==0.0)
{
tf.setText(strtmp.substring(0,loc));
}
else tf.setText(Double.toString(result));
opr=" ";
flag=false;
}
else if(str1=="sqrt")
{
tmp2=Double.parseDouble(tf.getText());
if(tmp2>0)
{
tmp2=Math.sqrt(Double.parseDouble(tf.getText()));
tf.setText(Double.toString(tmp2));
flag=false;
}
}
else if(str1=="%")
{
tmp3=Double.parseDouble(tf.getText())/100;
if(opr == "*")
{
result = result*(result*tmp3);
tf.setText(Double.toString(result));
}
else if(opr == "+")
{
result = result+result*tmp3;
tf.setText(Double.toString(result));
}
else if(opr == "-")
{
result = result-result*tmp3;
tf.setText(Double.toString(result));
}
else if(opr == "/")
{
result = result/(result*tmp3);
tf.setText(Double.toString(result));
}
else
{
tf.setText("0.0");
}
flag = false;
opr = "";
}
else if(str1=="1/x")
{
tmp4=Double.parseDouble(tf.getText());
result=1.0/tmp4;
tf.setText(Double.toString(result));
flag=false;
}
else if(str1=="+/-")
{
tmp5=-1*Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp5));
}
else if(str1==".")
{
if(str3.indexOf('.')==-1)
{
tf.setText(str3+str1);
}
}
else if(str1==" C ")
{
tf.setText("0.0");
flag=false;
}
else if(str1=="CE")
{
tf.setText("0");
flag=false;
}
else if(str1=="Backspace")
{
tf.setText(str3.substring(0,(str3.length()-1)));
}
else if(str1=="MC")
{
str2 ="";
tf1.setText("");
}
else if(str1=="MS")
{
String before = tf.getText();
tmp7 = Double.parseDouble(before);
str2 = Double.toString(tmp7);
tf1.setText(" M");
flag = false;
}
else if(str1=="M+")
{
tmp7 = tmp7 + Double.parseDouble(tf.getText());
str2 = Double.toString(tmp7);
flag = false;
}
else if(str1=="复制(C)")
{
String text = tf.getText();
StringSelection selection = new StringSelection(text);
cb.setContents(selection, null);
}
else if(str1=="粘贴(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)
{
}
}
else if(str1=="数字分组(T)")
{
String str4=tf.getText();
for(int i=1;(3*i)<str4.length();i++)
{
}
}
else if(str1=="关于计算器")
{
aboutcomputer d;
d=new aboutcomputer(frame);
d.display();
}
else if(str1=="帮助主题")
{
helpcomputer d;
d=new helpcomputer(frame);
d.display();
}
else if(str1=="退出")
{
System.exit(1);
}
}
class pressKeyboard extends KeyAdapter
{
private JTextField tf;
public pressKeyboard(JTextField tf)
{
this.tf=tf;
}
public void KeyTyped(KeyEvent e)
{
String s1=tf.getText();
char ch=e.getKeyChar();
if(ch=='1'|ch=='2'|ch=='3'|ch=='4'|ch=='5'|ch=='6'|ch=='7'|ch=='8'|ch=='9'|ch=='0')
{
if(flag)
{
tf.setText(s1+ch);
}
else
{
tf.setText(ch+"");
flag=true;
}
}
else if(ch=='+'|ch=='-'|ch=='*'|ch=='/')
{
result=Double.parseDouble(tf.getText());
flag=false;
opr=ch+"";
}
}
}
public static void main(String[]args)
{
computer computer1=new computer();
computer1.com();
}
}
关于
aboutcomputer.java
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class aboutcomputer extends Dialog implements ActionListener {
private Label l;
private Label ll;
private Button b;
public TextArea ta;
private Panel p1;
private Panel p2;
private Panel p3;
private String title = "关于计算器";
public aboutcomputer(JFrame f){
super(f,true);
this.setTitle(title);
l = new Label("简单计算器");
l.setForeground(Color.blue);
l.setFont(new Font("Arial",Font.PLAIN,24));
ll = new Label("java最后一次实验!");
ll.setForeground(Color.blue);
ll.setFont(new Font("Arial",Font.PLAIN,24));
ll.setBackground(Color.BLUE);
b = new Button(" 确定 ");
b.addActionListener(this); //(new ButtonHandler());
ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setText(ll.getText());
ta.setBackground(Color.lightGray);
ta.setEditable(false);
ta.enable(false);
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);
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());
f.pack();
f.setResizable(false);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
{
dispose();
}
}
public void display(){
this.setSize(350,350);
this.setResizable(false);
this.setVisible(true);
}
private class WindowHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose();
// System.exit(1);
}
}
}
helpcomputer.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class helpcomputer extends aboutcomputer implements ActionListener{
private String str;
private Button bt;
public helpcomputer(JFrame f)
{
super(f);
this.setTitle("简单计算器帮助文档!");
str="呵呵,还没有帮助文档!";
ta.setText(str);
bt=new Button("确定");
}
}