日期:2014-05-20 浏览次数:20717 次
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class EX4136 extends JFrame implements ActionListener{
private JLabel biaoq1,biaoq2;
private JButton button;
private JTextField wenben;
Choujiang ff=new Choujiang();
public EX4136(){
this.setLayout(null);
biaoq1=new JLabel("请输入您要购买的数字(1-20)");
button=new JButton("开奖");
wenben=new JTextField();
button.addActionListener(this);
biaoq1.setBounds(10,10,200,50);
wenben.setBounds(190,20,80,30);
button.setBounds(290,20,70,30);
this.add(biaoq1);
this.add(wenben);
this.add(button);
this.setSize(400, 300);
this.setTitle("模拟彩票系统");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String m1=ff.getPrize1()+""; // 用""把整型转换成字符串
String m2=ff.getPrize2()+"";
String m3=ff.getPrize3()+"";
String str="谢谢您的购买,下次好运"+"一等奖是"+ff.getPrize1()+",二等奖是"+ff.getPrize2()+",三等奖是"+ff.getPrize3();
String str1="恭喜您,获得一等奖";
String str2="恭喜您,获得二等奖";
String str3="恭喜您,获得三等奖";
if(e.getSource()==button){
if(wenben.getText()==m1){
biaoq2=new JLabel(str1);
}else if(wenben.getText()==m2){
biaoq2=new JLabel(str2);
}else if(wenben.getText()==m3){
biaoq2=new JLabel(str3);
}else{
biaoq2=new JLabel(str);
}
biaoq2.setBounds(200,10,350,100);
this.add(biaoq2);
}
}
public static void main(String[] args){
new EX4136();
}
}
public class Choujiang {
int a[]=new int[3];
public Choujiang(){
for(int i=0;i<3;i++){
a[i]=(int)(Math.random()*20+1);
}
}
public int getPrize1(){
return a[0];
}
public int getPrize2(){
return a[1];
}
public int getPrize3(){
return a[2];
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class EX4136 extends JFrame implements ActionListener{
private JLabel biaoq1,biaoq2;
private JButton button;
private JTextField wenben;
Choujiang ff=new Choujiang();
public EX4136(){
this.setLayout(null);
biaoq1=new JLabel("请输入您要购买的数字(1-20)");
button=new JButton("开奖");
wenben=new JTextField();
button.addActionListener(this);
biaoq1.setBounds(10,10,200,50);
wenben.setBounds(190,20,80,30);
button.setBounds(290,20,70,30);
this.add(biaoq1);
this.add(wenben);
this.add(button);
this.setSize(400, 300);
this.setTitle("模拟彩票系统");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String m1=ff.getPrize1()+""; // 用""把整型转换成字符串
String m2=ff.getPrize2()+"";
String m3=ff.getPrize3()+"";
String str="谢谢您的购买,下次好运"+"一等奖是"+ff.getPrize1()+",二等奖是"+ff.getPrize2()+",三等奖是"+ff.getPrize3();
String str1="恭喜您,获得一等奖";
String str2="恭喜您,获得二等奖";
String str3="恭喜您,获得三等奖";
if(e.getSource() == button){
if(biaoq2 != null){
this.remove(biaoq2);
}
System.out.println("clicked");
System.out.println("wenben.getText():" + wenben.getText());
System.out.println("m1:" + m1);
System.out.println("m2:" + m2);
System.out.println("m3:" + m3);
if(wenben.getText().equals(m1)){
biaoq2=new JLabel(str1);
}else if(wenben.getText().equals(m2)){
biaoq2=new JLabel(str2);
}else if(wenben.getText().equals(m3)){
biaoq2=new JLabel(str3);
}else{
biaoq2=new JLabel(str);