[公告] 痞客邦「應用市集」新 App 上架-iFontCloud Professional[公告] 痞客邦後台發表文章提供插入多張圖片新功能[公告]痞客邦新服務上線 部落客商店聚集就在《痞市集》[公告] 部落格「快捷功能BAR」改版介紹[公告] 痞客邦「快捷功能BAR」6月4日改版通知
這個程式是之前分享抽籤程式的進化版
可以來做什麼呢?
1.做樂透的亂數取數....嘿嘿
2.做老師上課沒籤桶的取代
3.你中獎了嘛?有活動懶的抽籤嘛?
.....

RndFrame.java
package rndApp;


import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;

public class RndFrame extends JFrame {

 private static final long serialVersionUID = 1L;

 private JPanel jContentPane = null;

 private JScrollPane jScrollPane = null;

 private JTextArea jTextArea = null;

 private JList jList = null;

 private JTextField rndText = null;

 private JButton jButton = null;

 private JButton jButton1 = null;

 private JLabel jLabel = null;

 private JCheckBox jCheckBox = null;

 private JFormattedTextField jFormattedTextField = null;

 //最大數
 private int maxNum = 50;
 //是否重新取數
 private boolean reRnd = false;
 //目前取到的數count
 private int count = 0 ;
 //存放值
 private int[] Num = null;
 //亂數種子
 private java.util.Random r = new java.util.Random(); // @jve:decl-index=0:

 private JLabel jLabel1 = null;

 private JLabel jLabel2 = null;

 private JLabel label_max = null;

 private JLabel jLabel4 = null;

 private JLabel label_rernd = null;

 private JButton jButton2 = null;
 /**
 * This is the default constructor
 */
 public RndFrame() {
 super();
 initialize();
 }

 
 private void setMaxNum(int maxNum)
 {
 this.maxNum = maxNum;
 }
 
 private void setReRnd(boolean b)
 {
 this.reRnd = b ;
 }
 
 private void initNum()
 {
 count = 0 ;
 Num = new int[maxNum];
 for(int i = 0 ; i < maxNum ; i ++)
 {
 Num[i] = i+1 ;
 }
 }
 
 public void setRndText()
 {
 if(maxNum-count>0)
 {
 int rnd = r.nextInt(maxNum-count);
 String str = String.valueOf(Num[rnd]);
 this.getRndText().setText(str);
 this.getJTextArea().append(str+"\n");
 
 if(reRnd==false)
 {
 //如果是不能重覆的話,那把值跟最後一個最交換,
 //然後設定count++不再取到它
 int tmp = Num[rnd];
 Num[rnd] = Num[maxNum-1-count];
 Num[maxNum-1-count] = tmp;
 count++;
 }
 
 }
 }
 
 public void setReset()
 {
 this.setMaxNum(Integer.parseInt(this.getJFormattedTextField().getText()));
 
 initNum();
 this.setReRnd(this.jCheckBox.isSelected());
 //元件回復狀況
 this.getJTextArea().setText("");
 this.label_max.setText(this.getJFormattedTextField().getText());
 this.label_rernd.setText(this.jCheckBox.isSelected()==true?"是":"否");
 }
 /**
 * This method initializes this
 *
 * @return void
 */
 private void initialize() {
 initNum();
 this.setSize(400, 300);
 this.setName("jFrame");
 this.setResizable(false);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setContentPane(getJContentPane());
 this.setTitle("抽籤程式");
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 this.setLocation( screenSize.width/2 - this.getWidth()/2, screenSize.height/2 - this.getHeight()/2 );
 this.setVisible(true);
 }

 /**
 * This method initializes jContentPane
 *
 * @return javax.swing.JPanel
 */
 private JPanel getJContentPane() {
 if (jContentPane == null) {
 label_rernd = new JLabel();
 label_rernd.setBounds(new Rectangle(58, 50, 45, 18));
 label_rernd.setHorizontalTextPosition(SwingConstants.TRAILING);
 label_rernd.setHorizontalAlignment(SwingConstants.CENTER);
 label_rernd.setText("否");
 jLabel4 = new JLabel();
 jLabel4.setBounds(new Rectangle(3, 50, 55, 18));
 jLabel4.setText("重覆取數");
 label_max = new JLabel();
 label_max.setBounds(new Rectangle(58, 30, 44, 16));
 label_max.setHorizontalAlignment(SwingConstants.CENTER);
 label_max.setText("50");
 jLabel2 = new JLabel();
 jLabel2.setBounds(new Rectangle(2, 30, 60, 16));
 jLabel2.setText("最大人數");
 jLabel1 = new JLabel();
 jLabel1.setBounds(new Rectangle(2, 10, 68, 16));
 jLabel1.setText("目前設定值");
 jLabel = new JLabel();
 jLabel.setBounds(new Rectangle(18, 123, 127, 33));
 jLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
 jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
 jLabel.setText("最大人數:");
 jContentPane = new JPanel();
 jContentPane.setLayout(null);
 //jContentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 jContentPane.add(getJScrollPane(), null);
 jContentPane.add(getJList(), null);
 jContentPane.add(getRndText(), null);
 jContentPane.add(getJButton(), null);
 jContentPane.add(getJButton1(), null);
 jContentPane.add(jLabel, null);
 jContentPane.add(getJCheckBox(), null);
 jContentPane.add(getJFormattedTextField(), null);
 jContentPane.add(jLabel1, null);
 jContentPane.add(jLabel2, null);
 jContentPane.add(label_max, null);
 jContentPane.add(jLabel4, null);
 jContentPane.add(label_rernd, null);
 jContentPane.add(getJButton2(), null);
 }
 return jContentPane;
 }

 /**
 * This method initializes jScrollPane
 *
 * @return javax.swing.JScrollPane
 */
 private JScrollPane getJScrollPane() {
 if (jScrollPane == null) {
 jScrollPane = new JScrollPane();
 jScrollPane.setBounds(new Rectangle(308, 2, 82, 264));
 jScrollPane.setViewportView(getJTextArea());
 }
 return jScrollPane;
 }

 /**
 * This method initializes jTextArea
 *
 * @return javax.swing.JTextArea
 */
 private JTextArea getJTextArea() {
 if (jTextArea == null) {
 jTextArea = new JTextArea();
 }
 return jTextArea;
 }

 /**
 * This method initializes jList
 *
 * @return javax.swing.JList
 */
 private JList getJList() {
 if (jList == null) {
 jList = new JList();
 jList.setBounds(new Rectangle(5, 101, 302, 8));
 }
 return jList;
 }

 /**
 * This method initializes rndText
 *
 * @return javax.swing.JTextField
 */
 private JTextField getRndText() {
 if (rndText == null) {
 rndText = new JTextField();
 rndText.setBounds(new Rectangle(103, 8, 112, 88));
 rndText.setFont(new Font("Dialog", Font.PLAIN, 66));
 rndText.setHorizontalAlignment(JTextField.CENTER);
 rndText.setText("");
 }
 return rndText;
 }

 /**
 * This method initializes jButton
 *
 * @return javax.swing.JButton
 */
 private JButton getJButton() {
 if (jButton == null) {
 jButton = new JButton();
 jButton.setBounds(new Rectangle(215, 64, 90, 32));
 jButton.setText("取數");
 jButton.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent e) {
 RndFrame frame = ((RndFrame)((JButton)e.getSource()).getTopLevelAncestor());
 frame.setRndText();
 }
 });
 }
 return jButton;
 }

 /**
 * This method initializes jButton1
 *
 * @return javax.swing.JButton
 */
 private JButton getJButton1() {
 if (jButton1 == null) {
 jButton1 = new JButton();
 jButton1.setBounds(new Rectangle(57, 193, 90, 35));
 jButton1.setText("設定");
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent e) {
 RndFrame frame = ((RndFrame)((JButton)e.getSource()).getTopLevelAncestor());
 frame.setReset();
 }
 });
 }
 return jButton1;
 }

 /**
 * This method initializes jCheckBox
 *
 * @return javax.swing.JCheckBox
 */
 private JCheckBox getJCheckBox() {
 if (jCheckBox == null) {
 jCheckBox = new JCheckBox();
 jCheckBox.setBounds(new Rectangle(18, 159, 237, 30));
 jCheckBox.setFont(new Font("Dialog", Font.BOLD, 24));
 jCheckBox.setText("是否可以重覆取數");
 }
 return jCheckBox;
 }

 /**
 * This method initializes jFormattedTextField
 *
 * @return javax.swing.JFormattedTextField
 */
 private JFormattedTextField getJFormattedTextField() {
 if (jFormattedTextField == null) {
 javax.swing.text.NumberFormatter mf2 = null;
 mf2 = new javax.swing.text.NumberFormatter();
 mf2.setMaximum(new Integer(500));
 mf2.setMinimum(new Integer(0));
 jFormattedTextField = new JFormattedTextField(mf2);
 jFormattedTextField.setBounds(new Rectangle(151, 123, 119, 33));
 jFormattedTextField.setHorizontalAlignment(JTextField.CENTER);
 jFormattedTextField.setText("50");
 jFormattedTextField.setFont(new Font("Dialog", Font.PLAIN, 24));
 }
 return jFormattedTextField;
 }
 /**
 * This method initializes jButton2
 *
 * @return javax.swing.JButton
 */
 private JButton getJButton2() {
 if (jButton2 == null) {
 jButton2 = new JButton();
 jButton2.setBounds(new Rectangle(229, 237, 76, 24));
 jButton2.setText("關於");
 jButton2.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent e) {
 RndFrame frame = ((RndFrame)((JButton)e.getSource()).getTopLevelAncestor());
 new AboutMe(frame,"2008/03/19","1.2","這是一個簡單的亂數取數程式!\n就像是抽籤的程式!");
 }
 });
 }
 return jButton2;
 }


 public static void main(String[] args)
 {
 new RndFrame();
 }
} // @jve:decl-index=0:visual-constraint="10,10"

一樣是用visual Editor拉的畫面
程式在這裡
按我下載
畫面如下:

Posted by catyku at 痞客邦 PIXNET 留言(9) 引用(0) 人氣()


open trackbacks list Trackbacks (0)

留言列表 (9)

Post Comment
  • silentpower
  • 感恩你的抽籤程式1.2版,真的好用,謝謝你的分享~
  • josephchg
  • 您好:我今天發現您的抽籤程式,覺得很好用,所以在我的部落格中推薦給朋友們,希望您不會介意,若您有任何意見,請再跟我說,謝謝您!
  • ^^!謝謝你會喜歡

    catyku replied in 2009/12/18 07:06

  • 阿兔
  • 那個抽籤程式一下載到電腦裡就說不能用.....
  • 怎樣的不能用?理論上有裝jdk按二下就能執行了

    catyku replied in 2010/02/13 20:38

  • banny
  • 謝謝你的分享,真的很好用,
    剛好讓我解決了一些選擇上的煩惱…謝謝
  • smile4672
  • 不能下載欸

    它說我家沒有這個檔案名

    就是

    怎樣開啟

    XLS的檔案阿?
  • xls是用office excel開的
    請問你說怎樣不能下載呢?
    我有試過沒問題?

    catyku replied in 2010/07/11 19:29

  • neroli
  • 謝謝妳的抽籤程式,真好用哩。
    我在win2000上也可以正常使用喔~ 感謝~~
  • abccc1234567
  • 好用+1

    幫推~
  • 訪客
  • 謝謝您的抽籤程式,我會分享在教學上,如果您有意見麻煩您告訴我嚕,謝謝您了
  • a963321006
  • 您好:
    請問EXCEL上能不能做出類似功能。謝謝!

You haven’t logged in yet, please use guest status to leave message. You can also log in with above service account and leave message

other options