zoukankan      html  css  js  c++  java
  • java第六次作业

    一个抽奖程序:用ArrayList类和random类
    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.;
    import java.util.
    ;
    import java.util.List;

    public class jRandom extends JFrame {
    private JFrame frame;
    private JButton startBUtton;

    public jRandom() {
    	makeFrame();
    	frame.pack();
    	frame.setVisible(true);
    	frame.setSize(400, 215);
    	frame.setLocation(550, 350);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void makeFrame() {
    
    	frame = new JFrame(" 抽个奖吧!");
    	ImageIcon imageIcon0 = new ImageIcon("t/1.png");
    	frame.setIconImage(imageIcon0.getImage());
    	Container contentPane = frame.getContentPane();
    	startBUtton = new JButton();
    	ImageIcon imageIcon1 = new ImageIcon("t/3.png");
    	ImageIcon imageIcon2 = new ImageIcon("t/4.png");
    	ImageIcon imageIcon3 = new ImageIcon("t/5.png");
    	startBUtton.setIcon(imageIcon1);// 设置按钮形状
    	startBUtton.setRolloverIcon(imageIcon2);// 设置鼠标滑过按钮的图像
    	startBUtton.setPressedIcon(imageIcon3);// 设置鼠标按下按钮的形状
    	startBUtton.setMargin(new Insets(0, 0, 0, 0)); // 设置按钮边框与边框内容之间的像素数
    	startBUtton.setBorderPainted(false); // 不绘制边框
    
    	// 按钮事件监听
    	startBUtton.addActionListener(new ActionListener() {
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			makechoujiang();
    		}
    	});
    	contentPane.add(startBUtton, BorderLayout.CENTER);
    
    }
    
    private void makechoujiang() {
    	double baseNum = 0.50;
    	String product = "100元,200元,500元,1000元";
    	String num = "30,20,5,3";
    	String[] p = product.split(",");
    	String[] n = num.split(",");
    	Random ran = new Random();
    	double base = ran.nextDouble();
    	if (base > baseNum) {
    		ImageIcon imageIcon = new ImageIcon("t/2.png");
    		JOptionPane.showMessageDialog(frame, "请您继续努力!", "抽奖结果",
    				JOptionPane.PLAIN_MESSAGE, imageIcon);
    		return;
    	}
    
    	List<String> list = new ArrayList<String>();
    	for (int i = 0; i < p.length; i++) {
    		for (int j = 0; j < Integer.parseInt(n[i]); j++) {
    			list.add(p[i]);
    		}
    	}
    	// 打乱奖池数据
    	int l = ran.nextInt(list.size());
    	ImageIcon imageIcon = new ImageIcon("t/1.png");
    	JOptionPane.showMessageDialog(frame, "恭喜您中了" + list.get(l) + "哟!!",
    			"抽奖结果", JOptionPane.PLAIN_MESSAGE, imageIcon);
    
    }
    
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	new jRandom();
    }
    

    }
    程序界面如下:

  • 相关阅读:
    安装HyperV后VirtualBox打开故障
    TortoiseGit或TortoiseSVN在软件或系统更新后图标丢失的一个解决办法
    WSL2 Ubuntu1604 安装 GUI图形库 和 Qt Creator
    时序约束(小梅哥)
    FPGA加速
    吴恩达机器学习笔记
    Neural Network and Deep Learning 笔记【第二章;反向传播算法如何⼯作】
    Tcl学习记录
    Neural Network and Deep Learning 笔记【第一章;手写数字识别】
    操作符重载
  • 原文地址:https://www.cnblogs.com/shemai/p/5468471.html
Copyright © 2011-2022 走看看