首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

事件处理机制,这段代码应该如何写?

2011-12-13 
事件处理机制,这段代码应该怎么写???!!import java.awt.*import java.awt.event.*import javax.swing.*

事件处理机制,这段代码应该怎么写???!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class siren3
{
  Frame f1=new Frame("人品测试器");
  Label l1,l2,l3,l4,l5,l6,l7,l8;
  public siren3()
  {
  l1=new Label("测试题目",Label.CENTER);
  l2=new Label(" ①有靓女或靓仔在旁边,你会无偷睇:");
  l2.setForeground(Color.red); 
  l3=new Label();
  l3.setText("         A.偷偷睇,B.一定会,C.会控制好自己");
  l3.setForeground(Color.red);
  l4=new Label("     ②行街时你会站在你女朋友那边:");
  l4.setForeground(Color.red);
  l5=new Label();
  l5.setText("    A.右边,B.不留意,C.左边");
  l5.setForeground(Color.red);
  l6=new Label("          你的第一条答案是:");
  l6.setForeground(Color.red);
  TextField t1=new TextField(5);
  l7=new Label();
  l7.setText("          你的第二条答案是:");
  l7.setForeground(Color.red);
  TextField t2=new TextField(5);
  l8=new Label();
  Button sure=new Button("sure");
  l8.setForeground(Color.red);
  {
  f1.setLayout (new FlowLayout());
  f1.add(l1);
  f1.add(l2);
  f1.add(l3);
  f1.add(l4);
  f1.add(l5);
  f1.add(l6);
  f1.add(t1);
  f1.add(l7);
  f1.add(t2);
  f1.add(l8);
  f1.add(sure);
  f1.setBounds(80,80,250,250); 
  f1.setVisible(true);
  sure.addActionListener(new ButtonHandler());
  String a=t1.getText();
  String b=t2.getText();
  System.out.println(a);
  if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("a")==0)
  l8.setText("跟你谈人品简直是废话");
  else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("b")==0)
  l8.setText("上帝保佑你,孩子");
  else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("a")==0)
  l8.setText("你的人品还好");
  else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("c")==0)
  l8.setText("你是个好人");
  else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("a")==0)
  l8.setText("好人的你,今天会走桃花运");
  else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("b")==0)
  l8.setText("靓女们此人不错,要多留意");
  else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("c")==0)
  l8.setText("极品啊,快抢啊,要不他就没了");
  }
  }
  /*{
  //String a = t1.getText();
  //Double n =Double.parseDouble (a);
  //String b = t2.getText();
  //Double m=Double.parseDouble (b);
  if (n=="A"&&m=="A")
  out.SetText("跟你谈人品简直是废话");
  else if(n=="A"&&m=="B");
  out SetText("上帝保佑你,孩子");
  else if(n=="B"&&m=="A");
  out SetText("你的人品还好");
  else if(n=="A"&&m=="C");
  out SetText("你是个好人");
  else if(n=="C"&&m=="A");
  out SetText("好人的你,今天会走桃花运");
  else if(n=="B"&&m=="B");
  out SetText("靓女们此人不错,要多留意");


  else if(n=="C"&&m=="C");
  out SetText("极品啊,快抢啊,要不他就没了");
   
   
   
  }*/  
  public static void main(String args[])
  {
  new siren3();
  }

class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

//这里的代码应该怎么写呢???就是说点击"sure"进行判断后,再用l8.setText("XXXXX")
}


}

}

[解决办法]
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class siren3 

Frame f1=new Frame("人品测试器"); 
Label l1,l2,l3,l4,l5,l6,l7,l8; 
TextField t1,t2;
public siren3() 

l1=new Label("测试题目",Label.CENTER); 
l2=new Label(" ①有靓女或靓仔在旁边,你会无偷睇:"); 
l2.setForeground(Color.red);
l3=new Label(); 
l3.setText("         A.偷偷睇,B.一定会,C.会控制好自己"); 
l3.setForeground(Color.red); 
l4=new Label("     ②行街时你会站在你女朋友那边:"); 
l4.setForeground(Color.red); 
l5=new Label(); 
l5.setText("    A.右边,B.不留意,C.左边"); 
l5.setForeground(Color.red); 
l6=new Label("          你的第一条答案是:"); 
l6.setForeground(Color.red); 
t1=new TextField(5); 
l7=new Label(); 
l7.setText("          你的第二条答案是:"); 
l7.setForeground(Color.red); 
t2=new TextField(5); 
l8=new Label(); 
Button sure=new Button("sure"); 
l8.setForeground(Color.red); 

f1.setLayout (new FlowLayout()); 
f1.add(l1); 
f1.add(l2); 
f1.add(l3); 
f1.add(l4); 
f1.add(l5); 
f1.add(l6); 
f1.add(t1); 
f1.add(l7); 
f1.add(t2); 
f1.add(sure); 
f1.add(l8); 
f1.setBounds(80,80,250,250);
f1.setVisible(true); 
sure.addActionListener(new ButtonHandler(this)); 




public void answer()
{
String a=t1.getText(); 
String b=t2.getText(); 
System.out.println(a); 
if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("a")==0) 
l8.setText("跟你谈人品简直是废话"); 
else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("b")==0) 
l8.setText("上帝保佑你,孩子"); 
else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("a")==0) 
l8.setText("你的人品还好"); 
else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("c")==0) 
l8.setText("你是个好人"); 
else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("a")==0) 
l8.setText("好人的你,今天会走桃花运"); 
else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("b")==0) 
l8.setText("靓女们此人不错,要多留意"); 
else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("c")==0) 
l8.setText("极品啊,快抢啊,要不他就没了"); 
}
public static void main(String args[]) 

new siren3(); 


class ButtonHandler implements ActionListener 



siren3 rp=null;
public ButtonHandler(siren3 rp)
{
this.rp=rp;
}
public void actionPerformed(ActionEvent e) 

rp.answer();
//这里的代码应该怎么写呢???就是说点击"sure"进行判断后,再用l8.setText("XXXXX") 





}
[解决办法]
==================================================================================================

//试下是不是你要的结果!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class siren3
{
Frame f1=new Frame("人品测试器");
Label l1,l2,l3,l4,l5,l6,l7,l8;
TextField t1,t2; 
public siren3()

l1=new Label("测试题目",Label.CENTER);
l2=new Label("①有靓女或靓仔在旁边,你会无偷睇:");
l2.setForeground(Color.red);
l3=new Label();
l3.setText("A.偷偷睇,B.一定会,C.会控制好自己");
l3.setForeground(Color.red);
l4=new Label("②行街时你会站在你女朋友那边:");
l4.setForeground(Color.red);
l5=new Label();
l5.setText(" A.右边,B.不留意,C.左边");
l5.setForeground(Color.red);
l6=new Label(" 你的第一条答案是:");
l6.setForeground(Color.red);
t1=new TextField(5);
l7=new Label();
l7.setText("你的第二条答案是:");
l7.setForeground(Color.red);
t2=new TextField(5);
l8=new Label(" ");
Button sure= new Button("sure");
l8.setForeground(Color.red);
{
f1.setLayout (new FlowLayout());
f1.add(l1);
f1.add(l2);
f1.add(l3);
f1.add(l4);
f1.add(l5);
f1.add(l6);
f1.add(t1);
f1.add(l7);
f1.add(t2);
f1.add(sure);
f1.add(l8);
f1.setBounds(80,80,250,250);
f1.setVisible(true);
sure.addActionListener(new ButtonHandler(this));
f1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}
}

public void answer() 

String a=t1.getText();
String b=t2.getText();
System.out.println(a);
if (a.compareToIgnoreCase("a")==0 && b.compareToIgnoreCase("a")==0)
l8.setText("跟你谈人品简直是废话");
else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("b")==0)
l8.setText("上帝保佑你,孩子");
else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("a")==0)
l8.setText("你的人品还好");
else if (a.compareToIgnoreCase("a")==0&&b.compareToIgnoreCase("c")==0)
l8.setText("你是个好人");
else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("a")==0)
l8.setText("好人的你,今天会走桃花运");
else if (a.compareToIgnoreCase("b")==0&&b.compareToIgnoreCase("b")==0)
l8.setText("靓女们此人不错,要多留意");
else if (a.compareToIgnoreCase("c")==0&&b.compareToIgnoreCase("c")==0)
l8.setText("极品啊,快抢啊,要不他就没了");
}
public static void main(String args[])
{
new siren3();
}

class ButtonHandler implements ActionListener
{
siren3 rp=null; 
public ButtonHandler(siren3 rp) 

this.rp=rp; 

public void actionPerformed(ActionEvent e)
{
rp.answer(); 

}


}

}

==============================================================================================

热点排行