package wtpaly;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Wtplay extends JFrame {
//窗口配置
private JTextArea messageArea;
private JButton send;
//wt配置
private int tq;//天气 晴40-2 阴60-2 雨0-3
private int wd;//温度 高10-3 中15-1 低5-3
private int sd;//湿度 湿 5-4 干10-3
private int fl;//风力 无15-2 微10-3 大0-2
private String[] wt=new String[4];
//产生随机数
private int rad(){
int a=(int)(Math.random()*7);
return a;
}
//产生天气
public String randwt(){
int a=this.rad();//产生天气
if(a<2){
this.tq=2;
this.wt[0]="晴";
}else if(a<4){
this.tq=3;
this.wt[0]="多云";
}else{
this.tq=0;
this.wt[0]="雨";
}
int b=this.rad();//产生温度
if(b<1){
this.wd=1;
this.wt[1]="低温";
}else if(b<4){
this.wd=3;
this.wt[1]="中等";
}else{
this.wd=2;
this.wt[1]="高温";
}
int c=this.rad();//产生湿度
if(c<4){
this.sd=1;
this.wt[2]="潮湿";
}else{
this.sd=2;
this.wt[2]="干燥";
}
int d=this.rad();//产生风力
if(d<1){
this.fl=0;
this.wt[3]="强风";
}else if(d<4){
this.fl=3;
this.wt[3]="无风";
}else{
this.fl=2;
this.wt[3]="微风";
}
String af="天气:"+this.wt[0]+"\n温度:"+this.wt[1]+"\n湿度:"+this.wt[2]+"\n风力:"+this.wt[3]+"\n出行指数:"+(this.tq*20+this.wd*5+this.sd*5+this.fl*5);
return af;
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == send) {
String a=new Wtplay().randwt();
messageArea.setText(a);
}
}
}
public Wtplay() {
// 设置客户端窗口的标题
super("Wtpllay");
// 初始化界面元素
messageArea = new JTextArea();
send = new JButton("明天");
// 设置界面布局
Container c = this.getContentPane();
JScrollPane jsp = new JScrollPane(messageArea);
c.add(jsp);
JPanel jp = new JPanel();
jp.add(send);
c.add(jp, BorderLayout.SOUTH);
// 为连接按钮注册监听器 P74
ButtonListener bl = new ButtonListener();
send.addActionListener(bl);
}
public static void main(String[] args) {
Wtplay Wtplay = new Wtplay();
Wtplay.setSize(500, 300);
Wtplay.setLocation(200, 200);
Wtplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Wtplay.setVisible(true);
String a=Wtplay.randwt();
Wtplay.messageArea.setText(a);
}
}
yxs1112003
相关分类