关于一个java applet小程序,能编译却运行不出来~~
那位高手跟看下~~~老是提示没有初始化。下面是代码!!在嵌入html里面也运行不出来~importjava.applet.*;importjava.awt.*;impor...
那位高手跟看下~~~老是提示没有初始化。下面是代码!!在嵌入html里面也运行不出来~
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Auto extends Applet
{
TextField in1 = new TextField(10);
TextField in2 = new TextField(10);
Button btn = new Button("求和");
Label out = new Label("用于显示结果");
public void init()
{
setLayout(new FlowLayout());
add( in1 );
add( in2 );
add( btn );
add( out );
btn.addActionListener(new BtnActionAdapter());
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a = in1.getText();
String b = in2.getText();
double c = Double.parseDouble(a);
double d = Double.parseDouble(b);
double f=c+d;
out.setText("两个数的和是:"+f);
}
}
} 展开
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Auto extends Applet
{
TextField in1 = new TextField(10);
TextField in2 = new TextField(10);
Button btn = new Button("求和");
Label out = new Label("用于显示结果");
public void init()
{
setLayout(new FlowLayout());
add( in1 );
add( in2 );
add( btn );
add( out );
btn.addActionListener(new BtnActionAdapter());
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a = in1.getText();
String b = in2.getText();
double c = Double.parseDouble(a);
double d = Double.parseDouble(b);
double f=c+d;
out.setText("两个数的和是:"+f);
}
}
} 展开
展开全部
老大,这个用Applet不好显示,我把你的程序改了,用窗体显示,因为你Applet就没画出来怎么显示啊,这个窗体不但可以运行,还可以打成jar包,多方便,说实话,java的applet已经被扔到杂角处了,就是跟不上时代了,我们了解即可。下面是改后的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Auto extends JFrame
{
TextField in1 = new TextField(10);
TextField in2 = new TextField(10);
Button btn = new Button("求和");
Label out = new Label("用于显示结果");
public Auto()
{
setLayout(new FlowLayout());
setSize(400,400);
setLocation(100,100);
add( in1 );
add( in2 );
add( btn );
add( out );
btn.addActionListener(new BtnActionAdapter());
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a = in1.getText();
String b = in2.getText();
double c = Double.parseDouble(a);
double d = Double.parseDouble(b);
double f=c+d;
out.setText("两个数的和是:"+f);
}
}
public static void main(String args[]){
Auto au=new Auto();
au.setVisible(true);
}
}
文件保存为Auto.java
能跑的,你试试,不会的找我,我教你
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Auto extends JFrame
{
TextField in1 = new TextField(10);
TextField in2 = new TextField(10);
Button btn = new Button("求和");
Label out = new Label("用于显示结果");
public Auto()
{
setLayout(new FlowLayout());
setSize(400,400);
setLocation(100,100);
add( in1 );
add( in2 );
add( btn );
add( out );
btn.addActionListener(new BtnActionAdapter());
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a = in1.getText();
String b = in2.getText();
double c = Double.parseDouble(a);
double d = Double.parseDouble(b);
double f=c+d;
out.setText("两个数的和是:"+f);
}
}
public static void main(String args[]){
Auto au=new Auto();
au.setVisible(true);
}
}
文件保存为Auto.java
能跑的,你试试,不会的找我,我教你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询