java自定义抽号

package csdn20230129;

/**
 * @Description
 * @Author wangFei
 * @Date 2023/1/29 17:35
 * @Version 1.0
 **/
//RandomNumber.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;

public class Main extends JFrame{
    private static final long serialVersionUID = 1L;
    static JLabel MAX;
    static JLabel MIN;
    static JLabel NUMBER;
    static JLabel GAP;
    static JLabel result;
    static JTextField max;
    static JTextField min;
    static JTextField number;
    static JTextField gap;
    static JButton Count;
    static JTextArea Result;
    static Panel Left,Right,MainPanel;

    public static void main(String args[]) throws IOException{
        int x = 500;
        int y = 350;
        JFrame F = new JFrame("RandomNumber");
        //INIT
        MAX =new JLabel("最大值:");
        max = new JTextField("100",6);
        MIN =new JLabel("最小值:");
        min = new JTextField("10",6);
        NUMBER = new JLabel("个数:");
        number = new JTextField("1",6);
        GAP = new JLabel("最小间隔:");
        gap = new JTextField("10",6);
        Count = new JButton("抽号");
        result = new JLabel("结果:");
        Result = new JTextArea(15,10);
        Left = new Panel();
        Right = new Panel();
        Left.setLayout(new GridLayout(6,2,10,10));
        Right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        F.setLayout(new FlowLayout(FlowLayout.LEFT));
        //ADD-FIRST(LEFT)
        Left.add(MAX);
        Left.add(max);
        Left.add(MIN);
        Left.add(min);
        Left.add(NUMBER);
        Left.add(number);
        Left.add(GAP);
        Left.add(gap);
        Left.add(Count);
        Count.addActionListener((e) -> {
            System.out.println(RandomInteger(Integer.parseInt(max.getText()),
                    Integer.parseInt(min.getText()),
                    Integer.parseInt(number.getText()),
                    Integer.parseInt(gap.getText())));
            Result.setText(RandomInteger(Integer.parseInt(max.getText()),
                    Integer.parseInt(min.getText()),
                    Integer.parseInt(number.getText()),
                    Integer.parseInt(gap.getText())));

        });
        //ADD-FIRST(RIGHT)
        Result.setEditable(false);
        Right.add(result);
        Right.add(Result);
        //ADD-SECOND
        MainPanel = new Panel();
        MainPanel.add(Left);
        MainPanel.add(Right);
        F.add(MainPanel);
        //END
        F.setSize(x, y);
        //SetLocation
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenw = screenSize.width;
        int screenh = screenSize.height;
        F.setLocation((screenw-x)/2, (screenh-y)/2);
        //SetVisible
        F.setVisible(true);
        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static String RandomInteger(int MaximumValue,int MinimumValue,int Range,int MinimumInterval) {
        int TMP = 0;
        int COUNT = 0;
        String end = "";
        TreeSet<Integer> t = new TreeSet<>();
        Random r = new Random();
        for(; Range > 0 ; Range-- ) {
            COUNT = r.nextInt(MaximumValue - MinimumValue + 1) + MinimumValue;
            if(Math.abs(COUNT - TMP) <= MinimumInterval) {
                while(Math.abs(COUNT - TMP) <= MinimumInterval) {
                    COUNT = r.nextInt(MaximumValue - MinimumValue + 1) + MinimumValue;
                }
            }
            t.add(COUNT);
            TMP = COUNT;
        }
        Iterator<Integer> I = t.iterator();
        while(I.hasNext()) {
            Integer a = I.next();
            end = end + a +" ";
        }
        return end;
    }
    static class Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(max.getText() != null|| min.getText() != null || gap.getText() != null || number.getText() != null){
                    int Max = Integer.valueOf(max.getText());
                    int Min = Integer.valueOf(min.getText());
                    int Gap = Integer.valueOf(gap.getText());
                    int Range = Integer.valueOf(number.getText());
                    Result.setText(RandomInteger(Max,Min,Range,Gap));
                }
            } catch (Exception a) {
                JOptionPane.showMessageDialog(null,a.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    JOptionPane.showMessageDialog(null,e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }
                System.exit(1);
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞LOVE霞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值