今天,笔者作为小白,用Java写了一个随机抽号的界面。话不多说,直接上截图和代码。
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RandomGameDemo extends JFrame implements ActionListener {
Container c = getContentPane();// 创建一个主容器
// 创建窗口所需要的组件
JButton jbt = new JButton("OK");
JLabel jla1 = new JLabel("00");
JLabel jla2 = new JLabel("请输入数字范围: ");
JLabel jla3 = new JLabel("~");
JTextField jtf1 = new JTextField(5);
JTextField jtf2 = new JTextField(5);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
// 构造方法
RandomGameDemo() {
super("随机抽号");// 设置窗口标题
// 设置布局管理器
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
// 添加组件
p1.add(jla1);
p2.add(jla2);
p2.add(jtf1);
p2.add(jla3);
p2.add(jtf2);
p3.add(p2);
p3.add(jbt);
// p1.setBackground(Color.magenta);
p2.setBackground(Color.orange);
p3.setBackground(Color.orange);
c.add(p1, BorderLayout.CENTER);
c.add(p3, BorderLayout.SOUTH);
// 注册监听器
jbt.addActionListener(this);
jtf1.addActionListener(this);
jtf2.addActionListener(this);
// 设置字体大小
jla1.setFont(new Font("华文仿宋", 1, 250));
jla2.setFont(new Font("华文仿宋", 1, 25));
jla3.setFont(new Font("华文仿宋", 1, 25));
// 设置窗口属性
setBounds(400, 400, 500, 400);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 实现接口中的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbt || e.getSource() == jtf1 || e.getSource() == jtf2) {
giveRandom();
}
}// end actionPerformed
// 产生随机数的方法
public void giveRandom() {
//用线程设置动态变化效果
new Thread(new Runnable() {
public void run() {
int i = 0;//定义运行次数i
while (true) {
try {
int max=Integer.parseInt(jtf2.getText().trim())>Integer.parseInt(jtf1.getText().trim())?Integer.parseInt(jtf2.getText().trim()) : Integer.parseInt(jtf1.getText().trim()),
min=Integer.parseInt(jtf1.getText().trim())<Integer.parseInt(jtf2.getText().trim())?Integer.parseInt(jtf1.getText().trim()) : Integer.parseInt(jtf2.getText().trim());
// 定义随机数,并在指定范围内给定随机数
int randomnum = new Random().nextInt(max - min + 1)+ min;
//随机数输出格式
if (randomnum >= 1 && randomnum <= 9) {
jl1.setText("0" + randomnum);
setColor();
i++;
} else {
jla1.setText(randomnum + "");
setColor();
i++;
}
//运行10次则退出循环
if (i == 10)
break;
} //end try
catch (NumberFormatException e) {
//错误提示窗口
JOptionPane.showMessageDialog(null, "未输入数字或者输入格式错误","错误",JOptionPane.ERROR_MESSAGE);
break;
}//end catch
//用睡眠方法控制速度
try {
if (i < 9)
Thread.sleep(50);
else
Thread.sleep(500);
} //end try
catch (InterruptedException ee) {
ee.printStackTrace();
}//end catch
}//end while
}//end run
}).start();
}// end giveRandom
///给数字设置颜色
private void setColor() {
int num = Integer.parseInt(jla1.getText());
switch(num/10) {
case 0:jla1.setForeground(Color.red);break;
case 1:jla1.setForeground(Color.gray);break;
case 2:jla1.setForeground(Color.green);break;
case 3:jla1.setForeground(Color.blue);break;
case 4:jla1.setForeground(Color.black);break;
case 5:jla1.setForeground(Color.yellow);break;
case 6:jla1.setForeground(Color.pink);break;
}
}// end setColor
}// end RandomGameDemo
public class RandomGame {
public static void main(String[] args) {
new RandomGameDemo();
}// emd main
}// end RandomGame
写代码的过程中遇到的问题是如何让界面的随机数在变化的过程中出现动态的结果,这就要用到线程问题来解决了。笔者对线程不是很了解,所以也是通过在博客上的学习和借鉴来完成这个代码的。另外笔者的随机抽号界面设计的不足就是有点草率和丑陋哈哈哈,基本功能还是能够实现的,不过可能存在一些细节未注意或者未处理,欢迎读者向我提出建议或者代码中的不足,笔者将不胜感激!