Java -GUI開發九九乘法表

來源:互聯網
上載者:User

標籤:font   def   乘法   command   osi   closed   ada   oca   關閉   

Java GUI開發九九乘法表

(1)實現目標:

利用java內建的awt包,基礎控制項開發一個九九乘法表,點擊可以顯示對應的乘法口訣。

(2)控制項選擇:

點擊——Button

顯示——TextField

 

(3)介面設計:總體設計布局

 (4)主表單編寫

      將程式在螢幕中央區顯示

  

1 /*視窗置中顯示*/2             int Win_width=fr.getWidth();3             int Win_Height=fr.getHeight();4             Toolkit kit = Toolkit.getDefaultToolkit(); // 定義工具包5             Dimension screenSize = kit.getScreenSize(); // 擷取螢幕的尺寸6             int screenWidth = screenSize.width; // 擷取螢幕的寬7             int screenHeight = screenSize.height; // 擷取螢幕的高8             fr.setLocation(screenWidth / 2 - Win_width / 2, screenHeight / 2 - Win_Height / 2);

   編寫關閉程式

              /*退出程式*/        fr.addWindowListener(new java.awt.event.WindowAdapter()        {            public void windowClosing(java.awt.event.WindowEvent e)            {                System.exit(0);            }        });            

 (5)FrameInOut表單內容和主要演算法

 1         FrameInOut() 2         { 3             super("99乘法表");//程式名稱 4             tx =new TextField(20); 5             tx.setEditable(false); 6             tx.setBackground(Color.orange); 7             tx.setFont(f); 8             tx.setBounds(490, 100, 230, 60); 9             add(tx);10             11             int i=0;12             int j=0;            13             for(i=1;i<=9;i++)14             {       15                 for(j=1;j<=i;j++)16                 {17                 Button btn =new Button(String.valueOf(j)+" × "+String.valueOf(i)+" = "+String.valueOf(i*j));18                 btn.setBounds((j-1)*80+10+(j-1)*10, 40+(i-1)*40, 80, 25); 19                 //btn.setBackground(Color.cyan);20                 add(btn);21                 btn.addActionListener(this);22                 }23                 24             }25                 26             setLayout(null);27             setSize(820,410);28             setVisible(true);29             setBackground(Color.orange);30         }31         32         33         public void actionPerformed(ActionEvent e)34         {35             String s=e.getActionCommand();36             tx.setText(s);37         }

(6)全部代碼

 1 package p_3142; 2     import java.awt.*; 3     import java.awt.event.*; 4     import java.io.IOException; 5  6     public class p3142 { 7  8         public static void main(String args[]) 9         {10             11             Frame fr=new FrameInOut();12             /*視窗置中顯示*/13             int Win_width=fr.getWidth();14             int Win_Height=fr.getHeight();15             Toolkit kit = Toolkit.getDefaultToolkit(); // 定義工具包16             Dimension screenSize = kit.getScreenSize(); // 擷取螢幕的尺寸17             int screenWidth = screenSize.width; // 擷取螢幕的寬18             int screenHeight = screenSize.height; // 擷取螢幕的高19             fr.setLocation(screenWidth / 2 - Win_width / 2, screenHeight / 2 - Win_Height / 2);20             21             /*退出程式*/22             fr.addWindowListener(new java.awt.event.WindowAdapter()23             {24                 public void windowClosing(java.awt.event.WindowEvent e)25                 {26                     System.exit(0);27                 }28             });29             30         }31     }32 33     class FrameInOut extends Frame implements ActionListener34     {35         TextField tx;36         Label lab;37         Font f=new Font("宋體", Font.BOLD, 40);38         39         FrameInOut()40         {41             super("99乘法表");//程式名稱42             tx =new TextField(20);43             tx.setEditable(false);44             tx.setBackground(Color.orange);45             tx.setFont(f);46             tx.setBounds(490, 100, 230, 60);47             add(tx);48             49             int i=0;50             int j=0;            51             for(i=1;i<=9;i++)52             {       53                 for(j=1;j<=i;j++)54                 {55                 Button btn =new Button(String.valueOf(j)+" × "+String.valueOf(i)+" = "+String.valueOf(i*j));56                 btn.setBounds((j-1)*80+10+(j-1)*10, 40+(i-1)*40, 80, 25); 57                 //btn.setBackground(Color.cyan);58                 add(btn);59                 btn.addActionListener(this);60                 }61                 62             }63                 64             setLayout(null);65             setSize(820,410);66             setVisible(true);67             setBackground(Color.orange);68         }69         70         71         public void actionPerformed(ActionEvent e)72         {73             String s=e.getActionCommand();74             tx.setText(s);75         }76         77     }78       
View Code

 

Java -GUI開發九九乘法表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.