Java抽签小程序

Java抽签小程序

package Lotter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.RootPaneContainer;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.text.html.HTML;

import java.awt.AWTException;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ForkJoinPool;
import java.util.Scanner;

public class lottery22 extends JFrame {
	private static final String URL = "Name1.txt";
	private JPanel jp = new JPanel();

	private JTextArea jta = new JTextArea();

	// 添加背景
	public void image() {
		Frame frame = new JFrame("背景图设置");
		((JFrame) frame).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ImageIcon img = new ImageIcon("src/Lotter/lottery.jpg");
		JLabel imgLabel = new JLabel(img);
		this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
		imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
		JPanel jP = (JPanel) this.getContentPane();
		jP.setOpaque(false);

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 调用构造方法
		lottery22 i = new lottery22();

		// 调用背景
		i.image();
		try {
			i.start();
		} catch (Throwable e) {
			e.printStackTrace();
		}

	}

	// 创建窗体
	public lottery22() {
		this.setTitle("抽奖");
		this.setBounds(300, 200, 1216, 856);
		this.setSize(1216, 856);
		this.setLocation(300, 200);
		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		this.setVisible(true);
		// 主窗体的按钮
		JButton jb = new JButton("开始抽签");
		jb.setFont(new Font(null, Font.PLAIN, 20));
		jb.setBounds(100, 600, 200, 60);
		Panel panel1 = new Panel();
		panel1.add(jb);

		jb.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				BufferedReader br = null;
				try {
					br = new BufferedReader(new FileReader(URL));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				ArrayList<String> array = new ArrayList<String>();
				String line = null;
				try {
					while ((line = br.readLine()) != null) {
						array.add(line);
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					br.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				// 随机产生一个索引
				Random r = new Random();
				int index = r.nextInt(array.size());
				// 根据该索引获取一个值
				String name = array.get(index);
				/*
				 * if(jb ==e.getSource()){ textarea(); }
				 */
				JOptionPane.showMessageDialog(null, name, "信息消息提示框", JOptionPane.INFORMATION_MESSAGE);

			}

		});
		this.add(jb);

	}
	/*
	 * public void textarea(){
	 * 
	 * jp.setLayout(null);
	 * 
	 * jta.setBounds(20,20,150,100);
	 * 
	 * jp.add(jta);
	 * 
	 * jta.setLineWrap(true);
	 * 
	 * this.add(jp);
	 * 
	 * this.setTitle("结果");
	 * 
	 * this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	 * 
	 * this.setResizable(false);
	 * 
	 * this.setBounds(100, 100, 200, 200);
	 * 
	 * this.setVisible(true);
	 * 
	 * 
	 * }
	 */

	// 主程序
	public void start() throws AWTException, Throwable {
		// 注册窗口
		JFrame jf = new JFrame("注册");
		jf.setSize(300, 300);
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

		JPanel panel = new JPanel();
		jf.setVisible(true);
		// 创建文本框
		final JTextField text = new JTextField(8);
		text.setFont(new Font(null, Font.PLAIN, 20));
		Panel panel1 = new Panel();
		panel1.add(text);

		// 创建按钮

		JButton btn1 = new JButton("关闭");
		btn1.setFont(new Font(null, Font.PLAIN, 20));

		btn1.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jf.dispatchEvent(new WindowEvent(jf, WindowEvent.WINDOW_CLOSING));

			}
		});
		panel1.add(btn1);
		JButton btn = new JButton("提交");
		btn.setFont(new Font(null, Font.PLAIN, 20));

		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// if (btn == e.getSource()) {
				// 文件写入
				/**
				 * File file = new File("Name.txt"); if (file.exists() != true)
				 * { try { file.createNewFile(); } catch (Exception e) {
				 * e.printStackTrace(); }
				 */

				int number = 1;
				Thread ht = new Thread() {
					public void run() {
						try {
							FileOutputStream os = new FileOutputStream("Name1.txt", true);
							PrintWriter pW = new PrintWriter(os);
							// BufferedWriter bW = new BufferedWriter(os);
							while (true) {
								String string = text.getText();
								// pW.write("\r\n");
								pW.println(string);
								// pW.append(string);
								pW.close();
							}

							// if (btn1 == e.getSource()) {

							// os.close();
							// }
						} catch (Exception a) {
							a.printStackTrace();
						}
						;
					}
				};
				ht.start();

			}

		});
		panel1.add(btn);
		jf.setContentPane(panel1);
	};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值