闲来无事,用swing写个小小的双色球号码生成器。很小很容易。。。。。。
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.Box;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.BorderLayout;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2010</p>
*
* <p>Company: </p>
*
* @author xingshuangyang
* @version 1.0
*/
public class Ssq extends JDialog {
public Ssq() {
try {
this.init();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void init() {
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE );
this.setTitle("福利彩票双色球号码生成器") ;
this.add(center);
this.setBounds(300,200,400,220);
this.setVisible(true) ;
}
JPanel center=new Center() ;
public static void main(String[] args) {
Ssq ssq = new Ssq();
}
}
class Center extends JPanel implements Runnable {
JButton runButton=new JButton("开 始") ;
JButton endButton=new JButton("停 止") ;
JLabel centerLabel=new JLabel();
JLabel downLabel=new JLabel();
Box vbox = Box.createVerticalBox();
Box hbox1 = Box.createHorizontalBox();
Box hbox2 = Box.createHorizontalBox();
Box hbox3 = Box.createHorizontalBox();
// ImageIcon ic1=new ImageIcon("123.jpg");
ImageIcon ic1=new ImageIcon(Center.class.getResource("ssq.gif"));
ImageIcon ic2=new ImageIcon(Center.class.getResource("500.gif"));
Thread thread ;
public Center() {
this.init();
}
public void init() {
// thread = new Thread(this);
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
runButton_actionPerformed(e);
}
});
endButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
endButton_actionPerformed(e);
}
});
//centerLabel.setSize(400,100);
downLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
downLabel_mouseClicked(e);
}
});
this.add(vbox) ;
vbox.add(vbox.createVerticalStrut(10)) ;
vbox.add(hbox1) ;
hbox1.add(runButton);
hbox1.add(hbox1.createHorizontalStrut(30)) ;
hbox1.add(endButton);
vbox.add(vbox.createVerticalStrut(10)) ;
vbox.add(hbox2);
hbox2.add(centerLabel);
centerLabel.setIcon(ic1) ;
vbox.add(vbox.createVerticalStrut(6)) ;
vbox.add(hbox3);
hbox3.add(downLabel);
downLabel.setIcon(ic2);
this.setBackground(Color.BLACK);
this.setSize(400,160) ;
}
public void run() {
try {
while (true) {
Thread.currentThread().sleep(10);
this.update();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int number1,number2,number3,number4,number5,number6,number7;
public void update() {
{
number1=Math.round((float)(Math.random()*33));
number2=Math.round((float)(Math.random()*33));
number3=Math.round((float)(Math.random()*33));
number4=Math.round((float)(Math.random()*33));
number5=Math.round((float)(Math.random()*33));
number6=Math.round((float)(Math.random()*33));
number7=Math.round((float)(Math.random()*16));
}
while((number1==number2)||(number1==number3)||(number1==number4)||(number1==number5)||(number1==number6)
||(number2==number3)||(number2==number4)||(number2==number5)||(number2==number6)
|| (number3==number4)||(number3==number5)||(number3==number6)
||(number4==number5)||(number4==number6)||(number5==number6)
||(number1==0)||(number2==0)||(number3==0)||(number4==0)||(number5==0)||(number6==0)||(number7==0))
{
number1=Math.round((float)(Math.random()*33));
number2=Math.round((float)(Math.random()*33));
number3=Math.round((float)(Math.random()*33));
number4=Math.round((float)(Math.random()*33));
number5=Math.round((float)(Math.random()*33));
number6=Math.round((float)(Math.random()*33));
number7=Math.round((float)(Math.random()*16));
return;
}
this.repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.setFont(new Font("Dialog",Font.BOLD,23));
if(number1<10)
{
g.drawString(" 0"+number1,9,98) ;
}
else
g.drawString(" "+number1,9, 98);
if(number2<10)
{
g.drawString(" 0"+number2,62,98) ;
}
else
g.drawString(" "+number2, 62, 98);
if(number3<10)
{
g.drawString(" 0"+number3,114,98) ;
}
else
g.drawString(" "+number3, 114, 98);
if(number4<10)
{
g.drawString(" 0"+number4,169,98) ;
}
else
g.drawString(" "+number4, 169, 98);
if(number5<10)
{
g.drawString(" 0"+number5,220,98) ;
}
else
g.drawString(" "+number5, 220, 98);
if(number6<10)
{
g.drawString(" 0"+number6,273,98) ;
}
else
g.drawString(" "+number6, 273, 98);
g.setColor(Color.BLUE) ;
g.setFont(new Font("Dialog",Font.BOLD,30));
if(number7<10)
{
g.drawString(" 0"+number7,333,98) ;
}
else
g.drawString(" "+number7, 333, 97);
}
public void runButton_actionPerformed(ActionEvent e) {
// this.init() ;
thread = new Thread(this);
thread.start();
}
public void endButton_actionPerformed(ActionEvent e) {
try
{
thread.stop();
}
catch(Exception ee)
{ }
}
public void downLabel_mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "CopyRight: sun Email:[email protected]", "关于......",
JOptionPane.INFORMATION_MESSAGE );
}
}