zoukankan      html  css  js  c++  java
  • Java -GUI开发九九乘法表

    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 ActionListener
    34     {
    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
    弱水三千,先干为敬。
  • 相关阅读:
    关于C语言字符串
    进程间通信方式总结
    数据结构(3)-----链表
    Oracle查看用户所在表空间
    oracle linux了解基本命令行
    关于分区技术的索引 index
    oracle闪回表详解
    转:深入学习Oracle分区表及分区索引
    第一章 基本的SQL语句 (SQL基础)
    linux根分区扩容
  • 原文地址:https://www.cnblogs.com/deom/p/7612312.html
Copyright © 2011-2022 走看看