1 / 5

JAVA 事件傾聽機制

JAVA 事件傾聽機制. 要改成為輸入 6 個號碼 , 用產生器 , 產生出要買幾次樂透後才會出現輸入的 6 個號碼相同. import java.lang.*; import java.awt.*; import java.awt.event.*; // 要用到 MouseAdapter 等 import javax.swing.*; // 要用到 JOptionPane public class j03100401 extends Frame { int[] nums; // 用來存已取得的號碼 Panel P1;

signa
Download Presentation

JAVA 事件傾聽機制

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA事件傾聽機制 • 要改成為輸入6個號碼,用產生器,產生出要買幾次樂透後才會出現輸入的6個號碼相同

  2. import java.lang.*; import java.awt.*; import java.awt.event.*; //要用到 MouseAdapter 等 import javax.swing.*; //要用到 JOptionPane public class j03100401 extends Frame { int[] nums; //用來存已取得的號碼 Panel P1; TextField T1 ,T2,T3,T4,T5,T6; Button B1,B2; public j03100401() { this.setLayout(null); //手動指定各元件的位置 P1 = new Panel(); P1.setBounds(20,35,250,28); GridLayout layout = new GridLayout(1,0); layout.setHgap(15); P1.setLayout(layout); P1.setEnabled(false); //不讓游標進入 T1~T6 this.add(P1);

  3. //T1~T6 都加到 P1 內 T1 = new TextField(); P1.add(T1); T2 = new TextField(); P1.add(T2); T3 = new TextField(); P1.add(T3); T4 = new TextField(); P1.add(T4); T5 = new TextField(); P1.add(T5); T6 = new TextField(); P1.add(T6); B1 = new Button("清除"); B1.setBounds(50,80,60,28); B1.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { T1.setText(""); T2.setText(""); T3.setText(""); T4.setText(""); T5.setText(""); T6.setText(""); } }); this.add(B1);

  4. B2 = new Button("開始選號"); B2.setBounds(130,80,100,28); //=========================================== B2.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { nums = new int[6]; for(int x=0;x<nums.length;x++) { nums[x]=(int)(Math.random()*42) + 1 ; //1~42 其中一個整數 //0.0 <= Math.random() < 1.0 //0.0 <= Math.random()*42 < 42.0 System.out.print(nums[x] + " "); //上行用來測試,可看選到的重複號碼 for(int y=0; y<x; y++) { //和之前選好的號碼比較 if(nums[x]==nums[y]) //如果有重複 x = x-1; //這次的再重新選 } }

  5. System.out.println(); T1.setText(String.valueOf(nums[0])); T2.setText(String.valueOf(nums[1])); T3.setText(String.valueOf(nums[2])); T4.setText(String.valueOf(nums[3])); T5.setText(String.valueOf(nums[4])); T6.setText(String.valueOf(nums[5])); } //void mouseClicked(MouseEvent e) end }); //end of B2.addMouseListener(); this.add(B2); //=========================================== this.setBounds(200,100,290, 130); this.setTitle("樂透號碼產生器"); this.setBackground(Color.PINK); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); //結束系統 } }); this.setVisible(true); } //public j03100401() end public static void main(String[] para) { new j03100401(); } } //public class j03100401 end

More Related