1. 程式人生 > >Java筆記丨09-1 兩個例子

Java筆記丨09-1 兩個例子

一個自動出題並判分的小程式和一個顯示99個圓圈的小程式

·AutoScore.java自動出題並判分

import java.awt.*;

import javax.swing.*;

public class AutoScore extends JFrame

{

    public AutoScore(){

       init();//初始化

       setSize( 400,350 );//設定大小

       setDefaultCloseOperation(EXIT_ON_CLOSE);

       setVisible( true );//顯示

    }



    public void init()

    {

       //{{INIT_CONTROLS

       setLayout(null);                              //沒有佈局

       setSize(400,350);                             //設定大小

       btnNew.setLabel("出題");                      //設定文字

       getContentPane().add(btnNew);                 //文字新增到介面

       btnNew.setBackground(java.awt.Color.lightGray);//設定顏色

       btnNew.setBounds(36,96,98,26);                //設位置寬度高度

       btnJudge.setLabel("判分");                   

       getContentPane().add(btnJudge);

       btnJudge.setBackground(java.awt.Color.lightGray);

       btnJudge.setBounds(216,96,94,25);

       lblA.setText("text");

       getContentPane().add(lblA);

       lblA.setFont(new Font("Dialog", Font.PLAIN, 24));

       lblA.setBounds(36,24,36,36);

       lblOp.setText("text");

       getContentPane().add(lblOp);

       lblOp.setFont(new Font("Dialog", Font.PLAIN, 24));

       lblOp.setBounds(72,24,36,36);

       lblB.setText("text");

       getContentPane().add(lblB);

       lblB.setFont(new Font("Dialog", Font.PLAIN, 24));

       lblB.setBounds(108,24,33,36);

       label5.setText("=");

       getContentPane().add(label5);

       label5.setFont(new Font("Dialog", Font.PLAIN, 24));

       label5.setBounds(168,24,34,36);

       getContentPane().add(txtAnswer);

       txtAnswer.setFont(new Font("Dialog", Font.PLAIN, 24));

       txtAnswer.setBounds(216,24,85,42);

       listDisp.setFont(new Font("Dialog", Font.PLAIN, 16));

       getContentPane().add(listDisp);

       listDisp.setBounds(36,144,272,106);

       //}}

   

       //{{REGISTER_LISTENERS 按鈕與函式關聯起來——新增事件的註冊

       SymAction lSymAction = new SymAction();

       btnNew.addActionListener(lSymAction);

       btnJudge.addActionListener(lSymAction);

       //}}

    }

   

    //{{DECLARE_CONTROLS 介面元素

    java.awt.Button btnNew = new java.awt.Button();//按鈕

    java.awt.Button btnJudge = new java.awt.Button();

    java.awt.Label lblA = new java.awt.Label();//標籤用來顯示題目

    java.awt.Label lblOp = new java.awt.Label();

    java.awt.Label lblB = new java.awt.Label();

    java.awt.Label label5 = new java.awt.Label();

    java.awt.TextField txtAnswer = new java.awt.TextField();//文字框,輸入

    java.awt.List listDisp = new java.awt.List(0);//顯示答案正確與錯誤

    //}}



    class SymAction implements java.awt.event.ActionListener

    {

       public void actionPerformed(java.awt.event.ActionEvent event)

       {

           Object object = event.getSource();

           if (object == btnNew)//出題

              btnNew_ActionPerformed(event);

           else if (object == btnJudge)//判斷

              btnJudge_ActionPerformed(event);

       }

    }



    void btnNew_ActionPerformed(java.awt.event.ActionEvent event)

    {

       // to do: code goes here.

       a = (int)(Math.random()*9+1);   //隨機產生兩個數

       b = (int)(Math.random()*9+1);

       int c = (int)(Math.random()*4);

       switch( c )//隨機產生符號

       {

           case 0: op="+"; result=a+b; break;

           case 1: op="-"; result=a-b; break;

           case 2: op="*"; result=a*b;break;

           case 3: op="/"; result=a/b;break;  //這裡只考慮整數除法

       }

       lblA.setText(""+a);

       lblB.setText(""+b);

       lblOp.setText(""+op);

       txtAnswer.setText("");//將剛剛隨機產生的新增進去,即出題

       

            

    }

   

    int a=0,b=0;  //兩個數字a和b

    String op=""; //存符號

    double result=0;//存正確的結果



    void btnJudge_ActionPerformed(java.awt.event.ActionEvent event)

    {

       String str = txtAnswer.getText();//得到使用者的輸入

       double d = Double.valueOf(str).doubleValue();//字串轉成實數

       String disp = "" + a + op + b+"="+ str +" ";

       if( d == result ) disp += "☆";

       else disp += "╳";

      

       listDisp.add( disp );//把資訊加入到列表框裡

    }



    public static void main(String [] args)

    {

       AutoScore f = new AutoScore();

    }



}

·Circle99Frame.java

import java.awt.*;

import javax.swing.*;



public class Circle99Frame extends JFrame

{

    public static void main(String[]argv)

    {

       JFrame frame = new Circle99Frame();

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉操作

       frame.setSize( 600, 600 );//設定大小

       frame.setVisible(true);//顯示

    }

    public void paint(Graphics g)//

    {  

       g.drawString("circle 99", 20, 20);//畫圖的位置(橫向和縱向的位置)

      

       int x0 = getSize().width /2;//根據大小和寬度設定中心位置

       int y0 = getSize().height /2;

      

       for( int r=0 ; r<getSize().height/2; r+=10 )

       {

           g.setColor( getRandomColor() );//加上隨機的顏色

           g.drawOval( x0-r,y0-r/2, r*2, r*2 );//去掉這裡的y0後面的/2則為同心圓

       }

      

    }

    Color getRandomColor()//紅綠藍三個顏色隨機

    {

       return new Color(

           (int)( Math.random() * 256 ),

           (int)( Math.random() * 256 ),

           (int)( Math.random() * 256 )

       );

    }

}