Topic: 为什么这段代码不能运行呢(输入长度和宽度后自动计算出面积!)?

  Print this page

1.为什么这段代码不能运行呢(输入长度和宽度后自动计算出面积!)? Copy to clipboard
Posted by: yqt
Posted on: 2005-09-05 09:23


public class ceishiApplet extends JApplet implements ActionListener {
JLabel label1,label2,label3;
JTextField text1,text2,text3;
public void init(){
label1=new JLabel("请输入长度");
text1=new JTextField(10);
label2=new JLabel("请输入宽度");
text2=new JTextField(10);
label3=new JLabel("面积");
text3=new JTextField();
getContentPane().add(label1);
getContentPane().add(label2);
getContentPane().add(label3);
getContentPane().add(text1);
getContentPane().add(text2);
getContentPane().add(text3);
text3.setEditable(false);
text2.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根

int c;
if (e.getSource()==text2){
int a, b;
a=Integer.parseInt(text1.getText());
b=Integer.parseInt(text2.getText());
c=a*b;
text3.setText(String.valueOf(c));
}
}

}

编译后无法运行???????????????

2.Re:为什么这段代码不能运行呢(输入长度和宽度后自动计算出面积!)? [Re: yqt] Copy to clipboard
Posted by: haiyangzhixin
Posted on: 2005-09-06 10:55

import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class CeishiApplet extends JApplet implements ActionListener {
JLabel label1,label2,label3;
JTextField text1,text2,text3;
//JPanel p1;
JButton b1;
public void init(){
label1=new JLabel("请输入长度");
text1=new JTextField(10);
label2=new JLabel("请输入宽度");
text2=new JTextField(10);
label3=new JLabel("面积");
text3=new JTextField(5);
b1=new JButton("Button");
JPanel p1=new JPanel();
getContentPane().add(p1);

p1.add(label1);
p1.add(text1);
p1.add(label2);
p1.add(text2);
p1.add(label3);
p1.add(text3);
p1.add(b1);

text3.setEditable(false);
b1.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根

int c;
if (e.getSource()==b1){
int a, b;
a=Integer.parseInt(text1.getText());
b=Integer.parseInt(text2.getText());
c=a*b;
text3.setText(String.valueOfCoffee);
}
}
}

3.Re:为什么这段代码不能运行呢(输入长度和宽度后自动计算出面积!)? [Re: yqt] Copy to clipboard
Posted by: q_yuan
Posted on: 2005-09-06 12:51

你要导入一些相应的包啊!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923