求java高手,总是出现“需要为class,inte易做图ce或enum” 急急急
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
clsss JSQ extends JFrame implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
double x = 0;
double y = 0;
int f = 0;
String s= "";
JTextField tf1,tf2;
JButton b1,b2,b3,b4,b5,b6;
JButton b[] = new JButton[11];
public JSQ(JFrame f1){
Container c = getCOntentPane;
c.setLayout(null);
f1.add(c);
p1.setBounds(5,25,500,100);
p2.setBounds(5,125,500,300);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(4,5));
tf1 = new JTextField(30);
tf2 = new JTextField(30);
tf1.setEditable(false);
tf2.setEditable(false);
c.add(p1);
c.add(p2);
p1.add(tf1);
p1.add(tf2);
for(int i=0; i<10; i++){
String s1=""+i;
b[i] = new Button(s1);
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10] = new JButton(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("=");
b6 = new JButton("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
s = s+ e.getActionCommand();
for(int i=0; i<11; i++){
if(e.getSource() == b[i]){
tf1.setText(tf1.getText() + e.getActionCommand());
}
if(e.getSource() == b6){
tf1.setText("");
tf2.setText("");
x=0;y=0;f=0;s="";
}
if(e.getSource() == b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;f=1;
}
if(e.getSource() == b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y-x));y=y-x;}
f = 2;
}
if(e.getSource() == b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y*x));y=y*x;}
f = 3;
}
if(e.getSource() == b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y/x));y=y/x;}
f = 4;
}
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y+x));
if(f == 2) tf1.setText(""+(y-x));
if(f == 3) tf1.setText(""+(y*x));
if(f == 4) tf1.setText(""+(y/x));
y=0;
tf2.setText(s + tf1.getText());
}
}
}
}
class JF0 extends JFrame{
public JF0(String s){
super(s);
setBounds(200,200,500,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class T2 extends Applet{
JF0 d= new JF0("计算器");
JSQ f = new JSQ(d);
}
改对后说明理由,加分
追问:谢谢,我已经改好了,但是只是加减能用,乘的结果都是0.0,除的结果都是NaN,请帮忙看看哪里出问题了,不胜感激
答案:1.
clsss JSQ extends JFrame implements ActionListener
这行的第一个单词打错了,改为class
2.
JSQ的构造器里的第一行写错:写为
Container c = getContentPane();
3.
JSQ的构造器里的for循环里new的button错了,改为:
b[i] = new JButton(s1);
再试试,请细心,还有多看出错提示,如有必要,将一些以前碰到的出错提示以及解决办法记下来。
其他:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
class JSQ extends JFrame implements ActionListener{//class写错了
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
double x = 0;
double y = 0;
int f = 0;
String s= "";
JTextField tf1,tf2;
JButton b1,b2,b3,b4,b5,b6;
JButton b[] = new JButton[11];
public JSQ(JFrame f1){
Container c = this.getContentPane();//getCOntentPane;写错了应是this.getContentPane();
c.setLayout(null);
f1.add(c);
p1.setBounds(5,25,500,100);
p2.setBounds(5,125,500,300);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(4,5));
tf1 = new JTextField(30);
tf2 = new JTextField(30);
tf1.setEditable(false);
tf2.setEditable(false);
c.add(p1);
c.add(p2);
p1.add(tf1);
p1.add(tf2);
for(int i=0; i<10; i++){
String s1=""+i;
b[i] = new JButton(s1);//应该是JButton
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10] = new JButton(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("=");
b6 = new JButton("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
s = s+ e.getActionCommand();
for(int i=0; i<11; i++){
if(e.getSource() == b[i]){
tf1.setText(tf1.getText() + e.getActionCommand());
}
if(e.getSource() == b6){
tf1.setText("");
tf2.setText("");
x=0;y=0;f=0;s="";
}
if(e.getSource() == b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;f=1;
}
if(e.getSource() == b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y-x));y=y-x;}
f = 2;
}
if(e.getSource() == b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y*x));y=y*x;}
f = 3;
}
if(e.getSource() == b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y/x));y=y/x;}
f = 4;
}
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y+x));
if(f == 2) tf1.setText(""+(y-x));
if(f == 3) tf1.setText(""+(y*x));
if(f == 4) tf1.setText(""+(y/x));
y=0;
tf2.setText(s + tf1.getText());
}
}
}
}
class JF0 extends JFrame{
public JF0(String s){
super(s);
setBounds(200,200,500,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class T2 extends Applet{
JF0 d= new JF0("计算器");
JSQ f = new JSQ(d);
} 从你贴出来的程序看,这个程序没问题,可以运行
上一个:java , 用单元格计算圆环的面积, 使用FOR 循环的~求高手啊!!有伪代码,求怎么用java写~
下一个:我买了java web开发实战宝典,但找不到mysql communnity servers5.0,谁能帮我,还有