choujiang.rar

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 2
    下载次数
  • 2011-02-21 16:47
    上传日期
自动抽奖程序,将代码中分别属于JChouJiang.java, FirstFrame.java和CJWindow.java三个文件的代码分别存为三个java文件,即可运行。
choujiang.rar
  • choujiang.txt
    7.6KB
内容介绍
//CJWindow.java文件 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.math.*; import java.util.Arrays; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class CJWindow extends JFrame{ String stf="",st; String[] ssf; int ch=0; private final String imgePath = "bj.jpg"; public Dimension dm=Toolkit.getDefaultToolkit().getScreenSize(); Font font1=new Font("楷体_GB2312",Font.BOLD,60); Font font2=new Font("黑体",Font.BOLD,160); Font font3=new Font("黑体",Font.BOLD,24); Font font4=new Font("黑体",Font.BOLD,36); MyThread mth=new MyThread(); JPanel jp; JButton jb1=new JButton("幸运抽奖"); public CJWindow() throws HeadlessException{ super("抽奖"); setBounds(Math.abs(dm.width-1024)/2,Math.abs(dm.height-857)/2,1024,857); init(); readFile(); ButtonCJ bcj=new ButtonCJ(); jb1.addActionListener(bcj); } private void init(){ final Image im=Toolkit.getDefaultToolkit().createImage(imgePath); jp=new JPanel(){ protected void paintChildren(Graphics g){ g.drawImage(im,0,0,this); super.paintChildren(g); } }; jp.setLayout(null); jp.add(jb1); jb1.setBounds(674,40,300,150); jb1.setBackground(Color.red); jb1.setForeground(Color.yellow); jb1.setFont(font1); add(jp); } public void readFile(){ try{ File f=new File("D://jiang.txt"); if(!f.exists()){ System.out.println("D://jiang.txt不存在,系统自动在D盘下建立jiang.txt"); f.createNewFile(); } FileReader fr=new FileReader(f); while((ch=fr.read())!=-1){ stf+=(char)ch; } ssf=stf.split("\n"); } catch(Exception ex){ ex.printStackTrace(); } } private class DJWindow extends JFrame{ JPanel DJ_jp1=new JPanel(); JPanel DJ_jp2=new JPanel(); JPanel DJ_jp3=new JPanel(); JLabel DJ_jl=new JLabel(new ImageIcon("sl.gif")); JLabel DJ_jl1=new JLabel(new ImageIcon("hylw.gif")); JLabel DJ_jl2=new JLabel(new ImageIcon("dfw.gif")); JLabel DJ_jl3=new JLabel(new ImageIcon("sc.gif")); JLabel DJ_jl4=new JLabel(new ImageIcon("fc.gif")); JButton DJ_jb1=new JButton("确认并返回"); JTextArea DJ_jta=new JTextArea(); public DJWindow(){ setTitle("抽奖结果"); setBounds(Math.abs(dm.width-420)/2,Math.abs(dm.height-450)/2,420,500); setLayout(null); DJ_jp1.add(DJ_jl); DJ_jp1.add(DJ_jl1); DJ_jp1.setLayout(null); DJ_jp1.setBounds(60,20,300,150); DJ_jl.setBounds(0,0,150,150); DJ_jl1.setBounds(160,0,150,150); DJ_jp2.add(DJ_jl2); DJ_jp2.add(DJ_jl3); DJ_jp2.add(DJ_jl4); DJ_jp2.setLayout(null); DJ_jp2.setBounds(0,20,420,150); DJ_jl3.setBounds(0,0,130,150); DJ_jl2.setBounds(140,0,130,150); DJ_jl4.setBounds(280,0,130,150); DJ_jp3.add(DJ_jta); DJ_jp3.setLayout(null); DJ_jp3.setBounds(30,200,360,200); DJ_jta.setBounds(50,0,250,180); DJ_jta.setBackground(Color.MAGENTA); DJ_jta.setForeground(Color.yellow); DJ_jta.setFont(font2); DJ_jta.setEditable(false); DJ_jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dispose(); } }); add(DJ_jp1); add(DJ_jp3); add(DJ_jb1); DJ_jb1.setBounds(75,400,250,40); DJ_jb1.setFont(font3); DJ_jb1.setForeground(Color.MAGENTA); DJ_jb1.setEnabled(false); setVisible(true); } public void rmjp1(){ remove(DJ_jp1); } public void addjp2(){ add(DJ_jp2); } public void setjta(String ss){ DJ_jta.setText(ss); } public void enjb1(){ DJ_jb1.setEnabled(true); } public void setjtaFt(){ DJ_jta.setFont(font4); } public void setjtabs(){ DJ_jta.setBounds(0,50,340,45); } } private class ButtonCJ implements ActionListener{ public void actionPerformed(ActionEvent ae1){ st=ae1.getActionCommand(); if(st=="幸运抽奖"){ mth.start(); dispose(); } } } private class MyThread extends Thread{ public void run(){ String rs=""; int rand=0,lgth=0,lh=0,slh=0; DJWindow djw=new DJWindow(); lgth=ssf.length; rand=(int)((Math.random()-0.0001)*lgth); rs=ssf[rand]; lh=rs.length(); slh=(int)(Math.abs(340/2-36*lh/2)/18); for(int j=0;j<slh;j++){ rs=" "+rs; } try{ for(int i=5;i>0;i--){ djw.setjta(" "+i); Thread.sleep(1000); } djw.setjtaFt(); djw.setjtabs(); djw.rmjp1(); djw.addjp2(); djw.setjta(""+rs); djw.enjb1(); djw.repaint(); } catch(InterruptedException e){ e.printStackTrace(); } } } } //FirstFrame.java文件 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.math.*; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; class FirstFrame extends JFrame{ JPanel jp=new JPanel(); JButton jb1=new JButton("修改奖品"); JButton jb2=new JButton("进行抽奖"); JButton jb3=new JButton("修改路径"); JLabel jl1=new JLabel("请选择要进行的操作"); JLabel jl2=new JLabel("放置文件路径"); JTextField jtf=new JTextField(); String str,addr="D:\\jiang.txt"; Font font1=new Font("黑体",Font.BOLD,22); Font font2=new Font("黑体",Font.BOLD,20); Font font3=new Font("Default",Font.PLAIN,14); public FirstFrame(){ super("选择操作方式"); ButtonClick bc=new ButtonClick(); jb1.addActionListener(bc); jb2.addActionListener(bc); jp.setBackground(Color.yellow); jp.setLayout(null); jp.add(jl1); jl1.setBounds(108,32,250,30); jl1.setFont(font1); jl1.setForeground(Color.blue); ButtonGroup bg=new ButtonGroup(); bg.add(jb1); bg.add(jb2); jp.add(jb1); jb1.setBounds(50,90,150,50); jb1.setFont(font2); jb1.setBackground(Color.blue); jb1.setForeground(Color.yellow); jp.add(jb2); jb2.setBounds(220,90,150,50); jb2.setFont(font2); jb2.setBackground(Color.blue); jb2.setForeground(Color.yellow); jp.add(jl2); jl2.setBounds(100,250,100,30); jl2.setForeground(Color.blue); jl2.setFont(font3); jp.add(jtf); jtf.setBounds(210,250,100,30); jtf.setForeground(Color.black); jtf.setFont(font3); jtf.setEditable(false); jtf.setText(" "+addr); add(jp); Dimension dm=Toolkit.getDefaultToolkit().getScreenSize(); setBounds(Math.abs(dm.width-420)/2,Math.abs(dm.height-320)/2,420,320); setVisible(true); } public void setV(boolean bl){ this.setVisible(bl); } private class ButtonClick implements ActionListener{ public void actionPerformed(ActionEvent ae){ try{ str=ae.getActionCommand(); if(str=="修改奖品"){ try{ File f=new File("D://jiang.txt"); if(!f.exists()){ System.out.println("D://jiang.txt文件不存在,系统自动在D盘下建立jiang.txt"); f.createNewFile(); } Runtime rt=Runtime.getRuntime(); rt.exec("cmd /c "+addr); }catch(IOException ioe){ ioe.printStackTrace(); System.exit(0); } } else if(str=="进行抽奖"){ CJWindow cjw=new CJWindow(); cjw.setVisible(true); } }catch(Exception e){ e.printStackTrace(); } } } } //JChouJiang.java文件 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JChouJiang{ public static void main(String agrs[]){ FirstFrame ff=new FirstFrame(); ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
评论
    相关推荐