文档库 最新最全的文档下载
当前位置:文档库 › 计算机编程代码

计算机编程代码

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;


public class ddd extends JFrame {

private JPanel contentPane;
private String a1="";
private int data1;
private String a2="";
private int data2;
private int opt;
TextField textField = new TextField();
Button button = new Button("0");
Button button_1 = new Button("1");
Button button_2 = new Button("2");
Button button_3 = new Button("3");
Button button_4 = new Button("4");
Button button_5 = new Button("5");
Button button_6 = new Button("6");
Button button_7 = new Button("7");
Button button_8 = new Button("8");
Button button_9 = new Button("9");
Button button_11 = new Button("-");
Button button_12 = new Button("*");
Button button_13 = new Button("/");
Button button_14 = new Button("=");
private final Button button_15 = new Button("+");

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ddd frame = new ddd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public ddd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});
button.setBounds(64, 74, 42, 23);
contentPane.add(button);


button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});
button_1.setBounds(122, 74, 42, 23);
contentPane.add(button_1);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_2.setBounds(182, 74, 42, 23);
contentPane.add(button_2);
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_3.setBounds(64, 103, 42, 23);
contentPane.add(button_3);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_4.setBounds(122, 103, 42, 23);
contentPane.add(button_4);
button_5.addActionListener(new Actio

nListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_5.setBounds(182, 103, 42, 23);
contentPane.add(button_5);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_6.setBounds(64, 132, 42, 23);
contentPane.add(button_6);
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_7.setBounds(122, 132, 42, 23);
contentPane.add(button_7);
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});


button_8.setBounds(182, 132, 42, 23);
contentPane.add(button_8);





button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(a1.intern()!="")
data1=Integer.parseInt(a1);
a1="";
opt=2;
}
});
button_11.setBounds(241, 103, 42, 23);
contentPane.add(button_11);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(a1.intern()!="")
data1=Integer.parseInt(a1);
a1="";
opt=3;
}
});


button_12.setBounds(241, 132, 42, 23);
contentPane.add(button_12);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(a1.intern()!="")
data1=Integer.parseInt(a1);
a1="";
opt=4;
}
});


button_13.setBounds(241, 161, 42, 23);
contentPane.add(button_13);
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result=0;
data2=Integer.parseInt(a1);
if(opt==2)
result=data1-data2;
if(opt==3)
result=data1*data2;
if(opt==4)
result=data1/data2;
if(opt==5)
result=data1+data2;


textField.setText(Integer.toString(result));
data1=result;
a1="";
a2="";
data2=0;
}
});


button_14.setBounds(182, 161, 42, 23);
contentPane.add(button_14);


textField.setBounds(64, 35, 219, 23);
contentPane.add(textField);

Button button_10 = new Button("9");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a1=a1+e.getActionCommand();
textField.setText(a1);
}
});
button_10.setBounds(64, 161, 42, 23);
contentPane.add(button_10);
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(a1.intern()!="")
data1=Integer.parseInt(a1);
a1="";
opt=5;
}
});
button_15.setBounds(241, 74, 42, 23);

contentPane.add(but

ton_15);

//contentPane.add(button_16);


//contentPane.add(button_17);
setVisible(true);
}
}

相关文档