import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyCalculator extends Frame implements ActionListener {
String buttonText [] = {"0","1","2","3","4","5","6","7","8","9",".","+","-","×","÷","=","C"};
Panel toolBar = new Panel();
double num1=0,num2=0;
Button button [] = new Button[17];
TextField screen;
String calculate="";
public void lanchFrame(){
setVisible(true);
this.setBounds(200, 300, 200, 300);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a) {
System.exit(0);
}
});
this.setTitle("MyCalculator");
screen = new TextField(20);
screen.addActionListener(this);
add(screen,BorderLayout.NORTH);
toolBar.setFont(new Font(null,Font.BOLD,30));
toolBar.setLayout(new GridLayout(6,3,3,3));
for(int i=0;i<17;i++){
button [i] = new Button(buttonText[i]);
button [i].addActionListener(this);
toolBar.add(button [i]);
}
add(toolBar);
}
public static void main (String[]args){
MyCalculator a = new MyCalculator();
a.lanchFrame();
}public void actionPerformed(ActionEvent e) {
try{
Object definer = e.getSource();
for(int i=0;i<17;i++){
if(definer.equals(button[i])){
if(i<10){
screen.setText(screen.getText()+i);
}
if(i==10){
screen.setText(screen.getText()+".");
}
if((i>10)&&(i<15)){
num1=Double.parseDouble(screen.getText());
calculate=e.getActionCommand();
screen.setText("");
}
if(i==15){
num2=Double.parseDouble(screen.getText());
if(calculate.equals("+")){
screen.setText(""+(num1+num2));
}
if(calculate.equals("-")){
screen.setText(""+(num1-num2));
}
if(calculate.equals("×")){
screen.setText(""+(num1*num2));
}
if(calculate.equals("÷")){
screen.setText(""+(num1/num2));
}
}
if(i==16){
screen.setText("");
calculate="";
num1=0;
num2=0;
}
}
}
}
catch(Exception err){
}
}
无法运行,帮我改改!
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyCalculator extends Frame implements ActionListener {
String buttonText [] = {"0","1","2","3","4","5","6","7","8","9",".","+","-","×","÷","=","C"};
Panel toolBar = new Panel();
double num1=0,num2=0;
Button button [] = new Button[17];
TextField screen;
String calculate="";
public void lanchFrame(){
setVisible(true);
this.setBounds(200, 300, 200, 300);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a) {
System.exit(0);
}
});
this.setTitle("MyCalculator");
screen = new TextField(20);
screen.addActionListener(this);
add(screen,BorderLayout.NORTH);
toolBar.setFont(new Font(null,Font.BOLD,30));
toolBar.setLayout(new GridLayout(6,3,3,3));
for(int i=0;i<17;i++){
button [i] = new Button(buttonText[i]);
button [i].addActionListener(this);
toolBar.add(button [i]);
}
add(toolBar);
}
public static void main (String[]args){
MyCalculator a = new MyCalculator();
a.lanchFrame();
}public void actionPerformed(ActionEvent e) {
try{
Object definer = e.getSource();
for(int i=0;i<17;i++){
if(definer.equals(button[i])){
if(i<10){
screen.setText(screen.getText()+i);
}
if(i==10){
screen.setText(screen.getText()+".");
}
if((i>10)&&(i<15)){
num1=Double.parseDouble(screen.getText());
calculate=e.getActionCommand();
screen.setText("");
}
if(i==15){
num2=Double.parseDouble(screen.getText());
if(calculate.equals("+")){
screen.setText(""+(num1+num2));
}
if(calculate.equals("-")){
screen.setText(""+(num1-num2));
}
if(calculate.equals("×")){
screen.setText(""+(num1*num2));
}
if(calculate.equals("÷")){
screen.setText(""+(num1/num2));
}
}
if(i==16){
screen.setText("");
calculate="";
num1=0;
num2=0;
}
}
}
}
catch(Exception err){
}
}
无法运行,帮我改改!
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class MyCalculator extends Frame implements ActionListener { String buttonText[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
".", "+", "-", "×", "÷", "=", "C"};
Panel toolBar = new Panel();
double num1 = 0, num2 = 0;
Button button[] = new Button[17];
TextField screen;
String calculate = "";
public void lanchFrame() { setVisible(true);
this.setBounds(200, 300, 200, 300);
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent a) {
System.exit(0);
} });
this.setTitle("MyCalculator");
screen = new TextField(20);
screen.addActionListener(this);
add(screen, BorderLayout.NORTH);
toolBar.setFont(new Font(null, Font.BOLD, 30));
toolBar.setLayout(new GridLayout(6, 3, 3, 3)); for (int i = 0; i < 17; i++) {
button[i] = new Button(buttonText[i]);
button[i].addActionListener(this);
toolBar.add(button[i]);
}
add(toolBar); } public static void main(String[] args) {
MyCalculator a = new MyCalculator();
a.lanchFrame();
} public void actionPerformed(ActionEvent e) {
try {
Object definer = e.getSource(); for (int i = 0; i < 17; i++) {
if (definer.equals(button[i])) {
if (i < 10) {
screen.setText(screen.getText() + i);
}
if (i == 10) {
screen.setText(screen.getText() + ".");
}
if ((i > 10) && (i < 15)) {
num1 = Double.parseDouble(screen.getText());
calculate = e.getActionCommand();
screen.setText("");
}
if (i == 15) {
num2 = Double.parseDouble(screen.getText());
if (calculate.equals("+")) {
screen.setText("" + (num1 + num2));
}
if (calculate.equals("-")) {
screen.setText("" + (num1 - num2));
}
if (calculate.equals("×")) {
screen.setText("" + (num1 * num2));
}
if (calculate.equals("÷")) {
screen.setText("" + (num1 / num2));
} }
if (i == 16) {
screen.setText("");
calculate = "";
num1 = 0;
num2 = 0;
}
}
}
} catch (Exception err) { } }
}
可以运行;了