职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1635|回复: 1

张孝祥 -- 《Java就业培训教程》书中原码 9章

[复制链接]
weisheng 发表于 2006-11-14 07:24 | 显示全部楼层 |阅读模式
《Java就业培训教程》P316源码
程序清单:TestStopWatch.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
class StopWatch        extends Canvas implements Runnable
{
        private long startTime = 0;
        private long endTime = 0;
        private boolean bStart = false;
        public StopWatch()
        {
                enableEvents(AWTEvent.MOUSE_EVENT_MASK);
                setSize(80,30);
        }
        protected void processMouseEvent(MouseEvent e)
        {
                if(e.getID() == MouseEvent.MOUSE_PRESSED)
                {
/*鼠标按下时,启动计时线程,并让起始时间变量和终止时间变量都等于当前时间*/
                        bStart = true;
                        startTime = endTime  = System.currentTimeMillis();
                        repaint();
                        new Thread(this).start();
                }
                else if(e.getID() == MouseEvent.MOUSE_RELEASED)
                {
                        /*鼠标释放时,终止计时线程,并重绘窗口表面上的内容*/
                        bStart = false;
                        repaint();
                }
                super.processMouseEvent(e);
        }
        public void paint(Graphics g)
        {
/*时间值的小时、分钟、秒、都用两位数字显示,
不足两位的部分前面加0,即\"HH:mm:ss\"这种的格式。*/
                SimpleDateFormat sdf= new SimpleDateFormat(\"HH:mm:ss\");
        /*最刚开始编写这个程序的时候,直接使用elapsedTime.setTime(endTime-startTime);
语句设置elapsedTime时间对象的数字值,从运行结果上发现,即使endTime-startTime等于0,
但elapsedTime显示的时间却不是\"00:00:00\",而是\"08:00:00\"。我们曾经讲过,时间在计算机
内存中也是用一个长整数表示的,在这里,我们又发现,即使这个内存中的长整数等于0时,由于
Date类考虑了本地时区问题,所以,其表示的时间就不一定为\"零点:零分:零秒\"。这里不需要
考虑时区问题,只是借助Date类来帮我们生成\"HH:mm:ss\"这种时间表示格式。明白这个问题后,
我们就不难想像出,可以先求出显示时间为\"00:00:00\"的时间对象在内存中对应的那个长整数,
然后在这个基础上加上计时器所记下的时间值,最后就可以显示出我们想要的结果。*/       
                Date elapsedTime =null;
                try
                {
                elapsedTime= sdf.parse(\"00:00:00\");
                }catch(Exception e){}
                elapsedTime.setTime(endTime - startTime +
elapsedTime.getTime());
                String display =  sdf.format(elapsedTime);
                g.drawRect(0,0,78,28);
                g.fill3DRect(2,2,75,25,true);
                g.setColor(Color.WHITE);
                g.drawString(display,10,20);
        }
        public void run()
        {
                while(bStart)
                {
                        try
                        {
                        Thread.sleep(500);
                        }catch(Exception e){e.printStackTrace();}
                        endTime = System.currentTimeMillis();
                        repaint();
                }
        }
}
public class TestStopWatch
{
        public static void main(String [] args)
        {
                Frame f =new Frame(\"StopWatch\");
                f.add(new StopWatch());
                f.setSize(200,200);
                f.setVisible(true);
                        f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
                                {
                                        System.exit(0);
                                }
                                });
        }
}
《Java就业培训教程》P319源码
程序清单:TestCheckbox.java
import java.awt.*;
import java.awt.event.*;
public class TestCheckbox
{
        Checkbox cb1=new Checkbox(\"你喜欢我吗?\",true);
        CheckboxGroup cbg=new CheckboxGroup();
        Checkbox cb2=new Checkbox(\"喜欢\",cbg,true);
        Checkbox cb3=new Checkbox(\"不喜欢\",cbg,false);
       
        public void init()
        {               
                Frame f=new Frame(\"TestCheckBox\");
                //创建FlowLayout布局管理器,关于布局管理器,本章后面有专门的讲解,
                看不明白//的读者暂时可以不去下面两句代码的作用。
                FlowLayout fl=new FlowLayout();
                f.setLayout(fl);
                 
                f.add(cb1);
                f.add(cb2);
                f.add(cb3);
                cb1.addItemListener(new CbItemListener());
                cb2.addItemListener(new CbItemListener());
                cb3.addItemListener(new CbItemListener());
                f.setBounds(0,0,300,100);
                f.setVisible(true);
                        f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
                                {
                                        System.exit(0);
                                }
                        });
        }
        class CbItemListener implements ItemListener
        {
                public void itemStateChanged(ItemEvent e)
                {
                        Checkbox cb = (Checkbox)e.getItemSelectable();
                        if(cb.getLabel().equals(\"你喜欢我吗?\"))
                        {
                                if(cb.getState() == true)
                                        System.out.println(\"我很高兴\");
                                else
                                        System.out.println(\"我很伤心\");
                        }
                        /*else if(cb.getLabel().equals(\"喜欢\"))
                        {
                                if(e.getStateChange() == ItemEvent.SELECTED)
                                        System.out.println(\"我也喜欢你\");
                                else
                                        System.out.println(\"我也不喜欢你\");
                        }*/
                        else
                        {
                                Checkbox cbx =cbg.getSelectedCheckbox();
                                if(cbx != null)
                                        System.out.println(cbx.getLabel());
                        }
                }
        }
        public static void main(String[] args)
        {
                new TestCheckbox().init();               
        }
}
《Java就业培训教程》P321源码
程序清单:TestChoice.java
import java.awt.*;
import java.awt.event.*;
public class TestChoice
{
        Choice ch=new Choice(); //创建Choice对象
        TestChoice()
        {
                ch.add(\"choice1\"); //用add方法向列表里加入选项
                ch.add(\"choice2\"); //用add方法向列表里加入选项
                ch.add(\"choice3\"); //用add方法向列表里加入选项
                FlowLayout fl=new FlowLayout();
                Frame f=new Frame(\"TestChoice\");
                f.setLayout(fl);
                f.add(ch); //把列表加入到窗口
                f.setBounds(0,0,200,100);
                f.setVisible(true);
                f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
                                {
                                        System.exit(0);
                                }
                        });
        }
        public static void main(String[] args)
        {
                new TestChoice();               
        }
}
《Java就业培训教程》P323源码
程序清单:TestMenuBar.java
import java.awt.*;
import java.awt.event.*;
public class TestMenuBar
{
        MenuBar menubar=new MenuBar(); //创建菜单条对象
        Menu fileM=new Menu(\"File\"); //创建各菜单
        Menu editM=new Menu(\"Edit\"); //创建各菜单
        Menu toolsM=new Menu(\"Tools\"); //创建各菜单
        Menu helpM=new Menu(\"Help\"); //创建各菜单
               
        MenuItem fileMI1=new MenuItem(\"New\"); //创建各菜单项
        MenuItem fileMI2=new MenuItem(\"Open\"); //创建各菜单项
        MenuItem fileMI3=new MenuItem(\"Save\"); //创建各菜单项
        CheckboxMenuItem fileMI5=new CheckboxMenuItem(\"Quit\",true);
         //创建各菜单项
         
            Menu filePrint = new Menu(\"print\");//创建子菜单
            MenuItem printM1 = new MenuItem(\"preview\");
            MenuItem printM2 = new MenuItem(\"setting\");
           
        TestMenuBar()
        {
                FlowLayout fl=new FlowLayout();
               
                Frame f=new Frame(\"TestMenuBar\");
                f.setLayout(fl);
               
                menubar.add(fileM); //将菜单加入菜单条
                menubar.add(editM);
                menubar.add(toolsM);
                menubar.add(helpM);
               
                fileM.add(fileMI1); //将菜单项加入file菜单中
                fileM.add(fileMI2);
                fileM.add(fileMI3);
               
                filePrint.add(printM1);//将菜单项加入print菜单中
                filePrint.add(printM2);
                fileM.add(filePrint);
//将print菜单作为一个菜单项加入file菜单中
               
                fileM.addSeparator(); //将一条分割线加入菜单中
                fileM.add(fileMI5); //将菜单项加入菜单中
                f.setMenuBar(menubar); //把整个菜单系统显示在窗口中
                f.setBounds(0,0,250,200);
                f.setVisible(true);
                f.addWindowListener(new WindowAdapter()
{
                        public void windowClosing(WindowEvent e)
                        {
                                System.exit(0);
                        }
                });
        }
       
        public static void main(String[] args)
        {
                new TestMenuBar();
        }
}
《Java就业培训教程》P327源码
程序清单:TestDialog.java
import java.awt.*;
import java.awt.event.*;
public class TestDialog
{
        TextField tf = new TextField(10);
        Button b1=new Button(\"模态显示\");
        Button b2=new Button(\"非模态显示\");
        Frame f=new Frame(\"TestDialog\");
       

        Button b3=new Button(\"确定\");
        Dialog dlg = new Dialog(f, \"Dialog Title\", true);
        FlowLayout fl=new FlowLayout();
        TestDialog()
        {
        f.setLayout(fl);
        f.add(tf);
        f.add(b1);
        f.add(b2);
        b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                        dlg.setModal(true);
                        dlg.setVisible(true);
                        tf.setText(\"www.it315.org\");
                }
                });
        b2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                        dlg.setModal(false);
                        dlg.setVisible(true);
                        tf.setText(\"www.it315.org\");
                }
                });       
        f.setBounds(0,0,400,200);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                {
                        System.exit(0);
                }
                });

       
        dlg.setLayout(fl);
        dlg.add(b3);
        b3.addActionListener(new ActionListener()
        {
                public void actionPerformed(ActionEvent e)
                {
                        dlg.dispose();
                }
        });

        dlg.setBounds(0,0,200,150);
               
        }
        public static void main(String[] args)
        {
        new TestDialog();               
        }
}
《Java就业培训教程》P330源码
程序清单:TestPane.java
import java.awt.*;
import java.awt.event.*;
public class TestPane
{

        TestPane()
        {
                Frame f=new Frame(\"TestDialog\");
                ScrollPane sp = new ScrollPane();
                TextArea ta = new TextArea(\"\",10,50,TextArea.SCROLLBARS_NONE);
                sp.add(ta);
               
                f.add(sp);
                f.setSize(200,200);
                f.setVisible(true);
                f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                {
                        System.exit(0);
                        }
                });
        }
        public static void main(String[] args)
        {
                new TestPane();               
        }
}
 楼主| weisheng 发表于 2006-11-14 07:24 | 显示全部楼层
《Java就业培训教程》P336源码
程序清单:TestCardLayout.java
import java.awt.*;
import java.awt.event.*;
public class TestCardLayout
{
        CardLayout cl = new CardLayout();
        Panel plCenter = new Panel();
        public static void main(String [] args)
        {
                new TestCardLayout().init();
        }
        public void init()
        {
                Frame f=new Frame("布局管理器");
                Panel plWest = new Panel();
                f.add(plWest,"West");
                f.add(plCenter);
               
                plWest.setLayout(new GridLayout(3,1));
                Button btnPrev = new Button("prev");
                plWest.add(btnPrev);
                Button btnNext = new Button("next");
                plWest.add(btnNext);
                Button btnThree = new Button("three");
                plWest.add(btnThree);
               
                plCenter.setLayout(cl);
                plCenter.add(new Button("One"),"1");
                plCenter.add(new Button("two"),"2");
                plCenter.add(new Button("three"),"3");
                plCenter.add(new Button("four"),"4");
                plCenter.add(new Button("five"),"5");

                class MyActionListener implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                if(e.getActionCommand().equals("prev"))
                                        cl.previous(plCenter);
                                else if(e.getActionCommand().equals("next"))
                                        cl.next(plCenter);
                                else if(e.getActionCommand().equals("three"))
                                        cl.show(plCenter,"3");
                        }
                }
                MyActionListener ma = new MyActionListener();
                btnPrev.addActionListener(ma);
                btnNext.addActionListener(ma);
                btnThree.addActionListener(ma);
                               
                f.setSize(300,300);
                f.setVisible(true);
        }
}
《Java就业培训教程》P343源码
程序清单:Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
    JFrame jf = new JFrame("Calculator");
    JTextField tf = new JTextField();
    public void init()
    {
             Container c = jf.getContentPane();
             tf.setHorizontalAlignment(JTextField.RIGHT);  
        c.add(tf,"North");
        
        JPanel pnl=new JPanel();
        c.add(pnl,"Center");

        pnl.setLayout(new GridLayout(4,4));
        JButton b=new JButton("1");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("2");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("3");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("+");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("4");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("5");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("6");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("-");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("7");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("8");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("9");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("*");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("0");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton(".");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("=");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("\\");
        b.addActionListener(this);
        pnl.add(b);
        
        jf.setSize(200,300);
        jf.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        tf.setText(tf.getText()+e.getActionCommand());
    }
    public static void main(String [] args)
    {
        new Calculator().init();
    }
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-12 06:40 , Processed in 0.142726 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表