(急)一个JAVA小程序。。高手帮忙改下
用applet从2个文本中读取2个整数,然后比较大小,输出大的数,怎么我这个输入的数好像都没用啊。。。不管输入什么结果都是0.谁帮忙改下代码了。。谢谢importjava...
用applet从2个文本中读取2个整数,然后比较大小,输出大的数,怎么我这个输入的数好像都没用啊。。。不管输入什么结果都是0.谁帮忙改下代码了。。谢谢
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class bbb extends Applet implements ActionListener
{
int a;
int b;
private Button compare=new Button("比较");
private TextField text=new TextField(20);
private TextField a1=new TextField(10);
private TextField b1=new TextField(10);
public void init()
{
try{
setLayout(new FlowLayout());
add(compare);
add(text);
add(a1);
add(b1);
setVisible(true);
compare.addActionListener(this);
a=Integer.parseInt(a1.getText()) ;
b=Integer.parseInt(b1.getText()) ;
}
catch(Exception e){
System.out.println("123123123");
}
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==compare){if(a>=b) text.setText("两个整数中最大值是:"+a);
else text.setText("两个整数中最大值是:"+b);
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class bbb extends Applet implements ActionListener
{
int a;
int b;
private Button compare=new Button("比较");
private TextField text=new TextField(20);
private TextField a1=new TextField(10);
private TextField b1=new TextField(10);
public void init()
{
try{
setLayout(new FlowLayout());
add(compare);
add(text);
add(a1);
add(b1);
setVisible(true);
compare.addActionListener(this);
a=Integer.parseInt(a1.getText()) ;
b=Integer.parseInt(b1.getText()) ;
}
catch(Exception e){
System.out.println("123123123");
}
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==compare){if(a>=b) text.setText("两个整数中最大值是:"+a);
else text.setText("两个整数中最大值是:"+b);
}
}
} 展开
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class bbb extends Applet implements ActionListener {
int a;
int b;
private Button compare = new Button("比较");
private TextField text = new TextField(20);
private TextField a1 = new TextField(10);
private TextField b1 = new TextField(10);
public void init() {
try {
setLayout(new FlowLayout());
add(compare);
add(text);
add(a1);
add(b1);
setVisible(true);
compare.addActionListener(this);
} catch (Exception e) {
System.out.println("123123123");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == compare) {
a=Integer.parseInt(a1.getText());
b=Integer.parseInt(b1.getText());
if(a>=b)
{
text.setText("两个整数中最大值是:"+a);
}
else
text.setText("两个整数中最大值是:"+b);
}
}
}
这样就好了,a=Integer.parseInt(a1.getText()) ;
b=Integer.parseInt(b1.getText()) ;
这两句要定义在事件方法内部,否则找不到它们的。别再把它们弄丢了。
import java.awt.event.*;
import java.applet.Applet;
public class bbb extends Applet implements ActionListener {
int a;
int b;
private Button compare = new Button("比较");
private TextField text = new TextField(20);
private TextField a1 = new TextField(10);
private TextField b1 = new TextField(10);
public void init() {
try {
setLayout(new FlowLayout());
add(compare);
add(text);
add(a1);
add(b1);
setVisible(true);
compare.addActionListener(this);
} catch (Exception e) {
System.out.println("123123123");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == compare) {
a=Integer.parseInt(a1.getText());
b=Integer.parseInt(b1.getText());
if(a>=b)
{
text.setText("两个整数中最大值是:"+a);
}
else
text.setText("两个整数中最大值是:"+b);
}
}
}
这样就好了,a=Integer.parseInt(a1.getText()) ;
b=Integer.parseInt(b1.getText()) ;
这两句要定义在事件方法内部,否则找不到它们的。别再把它们弄丢了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询