-
主窗口:应用程序的主窗口是一个继承自JFrame的类,设置了窗口的标题、大小、布局和关闭操作。
-
抽奖按钮:一个JButton组件用于开始和结束抽奖。点击按钮时,会触发ActionListener的事件。
-
抽奖逻辑:抽奖逻辑位于StartButtonListener内部。当用户点击按钮时,根据当前抽奖状态执行不同的操作。如果抽奖正在进行,会停止抽奖并将结果添加到手机号码列表中。如果抽奖尚未开始,会启动一个计时器,定期生成新的随机抽奖号码,并在按钮上显示。
-
生成抽奖号码:生成抽奖号码的方法是generatePhoneNumber(),它随机生成一个11位的手机号码,以便在按钮上显示。
-
限制抽奖次数:在抽奖过程中,会检查手机号码列表的大小,如果达到5个号码,则会显示消息提示用户所有号码都已抽完。
-
主函数:在main函数中,创建了prizedraw对象并将其设置为可见,启动了整个应用程序
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; public class prizedraw extends JFrame { private JPanel buttonPanel; private JLabel statusLabel; private JButton startButton; private ArrayList<String> phoneNumbers; private Timer timer; public prizedraw() { phoneNumbers = new ArrayList<>(); setupUI(); } private void setupUI() { this.setTitle("抽奖"); this.setSize(400, 200); this.setLayout(new BorderLayout()); buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); this.add(buttonPanel, BorderLayout.CENTER); statusLabel = new JLabel("请开始抽奖"); statusLabel.setFont(new Font("Serif", Font.BOLD, 50)); JPanel centerPanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; centerPanel.add(statusLabel, gbc); this.add(centerPanel, BorderLayout.NORTH); startButton = new JButton("开始"); startButton.setFont(new Font("Serif", Font.BOLD, 24)); startButton.addActionListener(new StartButtonListener()); buttonPanel.add(startButton); } private class StartButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (timer != null && timer.isRunning()) { timer.stop(); phoneNumbers.add("已结束抽奖"); statusLabel.setText("请开始抽奖"); startButton.setText("开始"); } else { if (phoneNumbers.size() >= 5) { JOptionPane.showMessageDialog(null, "所有的号码已经被抽完了!"); return; } timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String phoneNumber = generatePhoneNumber(); statusLabel.setText(phoneNumber); } }); timer.start(); startButton.setText("结束"); } } } private String generatePhoneNumber() { Random random = new Random(); return "1" + (random.nextInt(9) + 3) + String.format("%09d", random.nextInt(1000000000)); } public static void main(String[] args) { prizedraw frame = new prizedraw(); frame.setVisible(true); } }
相信大家都看出问题了,这个程序抽取的号码存在漏洞,万一抽取到的号码相等怎么办呢,我们可以在随机抽取的过程中加入do while进行判断,以确保每一个号码的唯一性,最后我们可以在抽取完成后讲存储的每一个号码通过新的面板展示出来
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class prizedraw extends JFrame { private JPanel buttonPanel; private JLabel statusLabel; private JButton startButton; private Timer timer; public prizedraw() { setupUI(); } private void setupUI() { this.setTitle("抽奖"); this.setSize(400, 200); this.setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); this.add(buttonPanel, BorderLayout.SOUTH); statusLabel = new JLabel("请开始抽奖", SwingConstants.CENTER); statusLabel.setFont(new Font("Serif", Font.BOLD, 30)); this.add(statusLabel, BorderLayout.CENTER); startButton = new JButton("开始"); startButton.setFont(new Font("Serif", Font.BOLD, 24)); startButton.addActionListener(new StartButtonListener()); buttonPanel.add(startButton); } private class StartButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (timer != null) { timer.cancel(); timer = null; startButton.setText("开始"); } else { timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { String phoneNumber = generatePhoneNumber(); statusLabel.setText(phoneNumber); } }, 0, 100); startButton.setText("结束"); } } } private String generatePhoneNumber() { Random random = new Random(); int secondDigit = random.nextInt(7) + 3; int remainingDigits = random.nextInt(1000000000); String phoneNumber = String.format("1%d%09d", secondDigit, remainingDigits); // 隐藏号码中间的四位数字 phoneNumber = phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7); return phoneNumber; } public static void main(String[] args) { prizedraw frame = new prizedraw(); frame.setVisible(true); } }
我是天才
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class prizedraw extends JFrame {
private JPanel buttonPanel;
private JLabel statusLabel;
private JButton startButton;
private Timer timer;
private int clickCount=0;
public prizedraw() {
setupUI();
}
private void setupUI() {
this.setTitle("抽奖");
this.setSize(400, 200);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
this.add(buttonPanel, BorderLayout.SOUTH);
statusLabel = new JLabel("请开始抽奖", SwingConstants.CENTER);
statusLabel.setFont(new Font("Serif", Font.BOLD, 30));
this.add(statusLabel, BorderLayout.CENTER);
startButton = new JButton("开始");
startButton.setFont(new Font("Serif", Font.BOLD, 24));
startButton.addActionListener(new StartButtonListener());
buttonPanel.add(startButton);
}
private class StartButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (timer != null) {
timer.cancel();
timer = null;
startButton.setText("开始");
} else {
clickCount++;
if(clickCount==6) {
JOptionPane.showMessageDialog(null, "所有的号码已经被抽完了!");
}
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
String phoneNumber = generatePhoneNumber();
statusLabel.setText(phoneNumber);
}
}, 0, 10);
startButton.setText("结束");
}
}
}
private String generatePhoneNumber() {
Random random = new Random();
int secondDigit = random.nextInt(7) + 3;
int remainingDigits = random.nextInt(1000000000);
String phoneNumber = String.format("1%d%09d", secondDigit, remainingDigits);
// 隐藏号码中间的四位数字
phoneNumber = phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
return phoneNumber;
}
public static void main(String[] args) {
prizedraw frame = new prizedraw();
frame.setVisible(true);
}
}