看看这里,横多粒子
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html

解决方案 »

  1.   

    这是什么环境的?
    我只有jdk1.4
      

  2.   

    例程12-1
    // LabelDemo.java
    import java.awt.*;
    import java.applet.*;
    public class LabelDemo extends Applet
    {
    Label lblOneLabel=new Label("标签一");
    Label lblSecondLabel=new Label("标签二");
    Label lblThirdLabel=new Label("标签三");
    public void init()
    {
    //向Applet容器中加入第一个Label
    add(lblOneLabel);
    //向Applet容器中加入第二个Label
    add(lblSecondLabel);
    //向Applet容器中加入第三个Label
    add(lblThirdLabel);
    }
    }例程12-2
    // ButtonDemo.java
    import java.awt.*;
    import java.applet.*;public class ButtonDemo extends Applet
    {
    Button btnOneButton=new Button ("按钮一");
    Button btnSecondButton=new Button ("按钮二");
    Button btnThirdButton=new Button ("按钮三");

    public void init()
    {
    //向Applet容器中加入第一个Button
    add(btnOneButton);
    //向Applet容器中加入第二个Button
    add(btnSecondButton);
    //向Applet容器中加入第三个Button
    add(btnThirdButton);
    }
    }例程12-3
    // CheckboxDemo.java
    import java.awt.*;
    import java.applet.*;public class CheckboxDemo extends Applet
    {
    Checkbox chkOneCheckbox=new Checkbox ("复选框一");
    Checkbox chkSecondCheckbox=new Checkbox ("复选框二");
    Checkbox chkThirdCheckbox=new Checkbox ("复选框三");

    public void init()
    {
    //向Applet容器中加入第一个Checkbox
    add(chkOneCheckbox);
    //向Applet容器中加入第二个Checkbox
    add(chkSecondCheckbox);
    //向Applet容器中加入第三个Checkbox
    add(chkThirdCheckbox);
    }
    }例程12-4
    // TextFieldDemo.java
    import java.awt.*;
    import java.applet.*;public class TextFieldDemo extends Applet
    {
    Label lblUserName=new Label("用户姓名:");
    TextField txtUserName=new TextField (12);

    Label lblUserPasswd=new Label("用户密码:");
    TextField txtUserPasswd=new TextField (12);

    Button btnLogin=new Button("登录");
    Button btnReset =new Button("重置");


    public void init()
    {
    //设置密码的回显字符为*
    txtUserPasswd.setEchoChar('*');

    add(lblUserName);
    add(txtUserName);

    add(lblUserPasswd);
    add(txtUserPasswd);

    add(btnLogin);
    add(btnReset);
    }
    }例程12-5
    // TextAreaDemo.java
    import java.awt.*;
    import java.applet.*;public class TextAreaDemo extends Applet
    {
    TextArea txtOneTextArea=new TextArea ();
    TextArea txtSecondTextArea=new TextArea ("多行文本框");

    public void init()
    {
    //向Applet容器中加入第一个TextArea
    add(txtOneTextArea);
    //向Applet容器中加入第二个TextArea
    add(txtSecondTextArea); }
    }例程12-6
    //ListDemo.java
    import java.awt.*;
    import java.applet.*;public class ListDemo extends Applet
    {

    public void init()
    {
    List lstYear= new List(4,false);
    lstYear.add ("1990");
    lstYear.add ("1991");
    lstYear.add ("1992");
    lstYear.add ("1993");
    //把1998加入到列表的第三项中
    lstYear.add ("1998",2);
    lstYear.add ("1994");
    lstYear.add ("1995");
    lstYear.add ("1996");
    lstYear.add ("1997");
    //把lstYear加入到容器中并显示出来
    add(lstYear);
    }
    }例程12-7
    //MenuDemo.java
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class MenuDemo extends Frame
    {
    public MenuDemo(String strTitle)
    {
    //设置框架窗体标题
    super(strTitle);
    //创建菜单条并加入到框架窗体中
    MenuBar mnuMenuBar=new MenuBar();
    this.setMenuBar( mnuMenuBar );
    //创建File菜单和相应的菜单项
    Menu mnuFile= new Menu("文件");
    MenuItem mnuFileNew=new MenuItem ("新建");
    MenuItem mnuFileOpen=new MenuItem ("打开…");
    MenuItem mnuFileSave=new MenuItem ("保存");
    MenuItem mnuFileSaveAs=new MenuItem ("另存为…");
    MenuItem mnuFilePageSetting=new MenuItem ("页面设置…");
    MenuItem mnuFilePrint=new MenuItem ("打印");
    MenuItem mnuFileQuit=new MenuItem ("退出");
    //把菜单项加入到File菜单中
    mnuFile.add(mnuFileNew);
    mnuFile.add(mnuFileSave);
    mnuFile.add(mnuFileSaveAs);
    mnuFile.addSeparator();//添加分割条
    mnuFile.add(mnuFilePageSetting);
    mnuFile.add(mnuFilePrint);
    mnuFile.add(mnuFileQuit);
    //创建Edit菜单和相关菜单项并加入到Edit菜单中
    Menu mnuEdit=new Menu("编辑");
    mnuEdit.add( new MenuItem("剪切") );
    mnuEdit.add( new MenuItem("复制") );
    mnuEdit.add( new MenuItem("粘贴") );
    //创建Search菜单和相关菜单项并加入到Search菜单中
    Menu mnuSearch=new Menu("搜索");
    mnuSearch.add( new MenuItem("查找...") );
    mnuSearch.add( new MenuItem("查找下一个") );
    mnuSearch.add( new MenuItem("替换...") );
    //创建Help菜单和相关菜单项并加入到Help菜单中
    Menu mnuHelp=new Menu("帮助");
    mnuHelp.add( new MenuItem("关于帮助") );
    mnuHelp.add( new MenuItem("帮助主题") );
    //把所有菜单加入到菜单条中
    mnuMenuBar.add( mnuFile );
    mnuMenuBar.add( mnuEdit );
    mnuMenuBar.add( mnuSearch );
    mnuMenuBar.add( mnuHelp );
    }
    //设置框架窗体的大小为宽400,高为400
    public Dimension getPreferredSize()
    {
    return new Dimension(400,400);
    }
    //程序的入口方法
    public static void main( String[] args )
    {
    //创建框架窗体
    MenuDemo frmMenuDemo=new MenuDemo("这是个使用菜单的例子");
    //设置框架窗体的事件监听(关闭窗体事件)
    frmMenuDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    //显示框架窗体
    frmMenuDemo.pack();
    frmMenuDemo.show();
    }
    }
      

  3.   

    例程12-11
    //FrameDemo.java
    import java.awt.*;
    import java.awt.event.*;public class FrameDemo extends Frame 
    {
        Label lblDisplayLabe = new Label();    //Construct the frame
        public FrameDemo(String strTitle)
        {
            //设置框架窗体标题
    super(strTitle);

            lblDisplayLabe.setText("这是个Frame的例子");
            this.add(lblDisplayLabe);
        }    //程序的入口方法
    public static void main( String[] args )
    {
    //创建框架窗体
    FrameDemo frmFrameDemo=new FrameDemo("这是个使用Frame的例子"); //设置框架窗体的事件监听(关闭窗体事件)
    frmFrameDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    //正常退出Java虚拟机
    System.exit(0);
    }
    }); //显示框架窗体
    frmFrameDemo.pack();
    frmFrameDemo.show();
    }
    }例程12-12
    //DialogDemo.java
    import java.awt.*;
    import java.awt.event.*;
    public class DialogDemo extends Frame 
    {
        //定义两个Button组件
        Button btnDisplayDialog = new Button("显示对话框");
        Button btnCloseDialog = new Button("关闭对话框");
        //定义Dialog组件,并且设置其为非模式对话框
        Dialog dlgDialog=new Dialog(this,"对话框标题",false);
        //frame的构造方法
        public DialogDemo(String strTitle)
        {
            //设置框架窗体标题
    super(strTitle);
    //设置对话框的大小
    dlgDialog.setSize(300,300);
    //把两个Button组件加入到Frame框架窗口中
            this.add(btnDisplayDialog,BorderLayout.WEST);//把该按钮显示窗口的左侧
            this.add(btnCloseDialog,BorderLayout.EAST);//把该按钮显示窗口的右侧
            //添加两个Button的事件监听
            btnDisplayDialog.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) 
                {
                    //如果对话框已经显示在屏幕上,则直接返回
                    if( dlgDialog.isShowing()) return;
                    //显示对话框
                    dlgDialog.show();
                }
            });
            btnCloseDialog.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) 
                {
                    //如果对话框已经显示在屏幕上,则关闭对话框
                    if( dlgDialog.isShowing())
                     dlgDialog.dispose();
                }
            });     
        }
        //程序的入口方法
    public static void main( String[] args )
    {
    //创建框架窗体
    DialogDemo frmDialogDemo=new DialogDemo("这是个使用Dialog的例子");
    //设置框架窗体的事件监听(关闭窗体事件)
    frmDialogDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    //关闭框架窗口
    System.exit(0);
    }
    });
    //显示框架窗体
    frmDialogDemo.pack();
    frmDialogDemo.show();
    }
    }例程12-13
    //FileDialogDemo.java
    import java.awt.*;
    import java.awt.event.*;
    public class FileDialogDemo extends Frame
    {
    FileDialog dlgFileDialog;

    //Frame的构造方法
    public FileDialogDemo(String title)
    {
    //设置框架窗体的标题
    super(title);
    //创建Button
    Button btnLoadFile= new Button("打开文件");
    //添加Button的事件监听
    btnLoadFile.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    if( dlgFileDialog == null )
                     dlgFileDialog=new FileDialog(FileDialogDemo.this);
                    //设置文件对话框为打开模式
                    dlgFileDialog.setMode(FileDialog.LOAD);
                    //如果对话框已经显示在屏幕上,则直接返回
                    if( dlgFileDialog.isShowing()) return;
                    //显示对话框
                    dlgFileDialog.show();
                    String strFileName=dlgFileDialog.getFile();
                    if( strFileName == null )
                     System.out.println("你取消了文件的选择");
                    else
                     System.out.println("你选择的文件名称:"+ strFileName );
                }
    });
    //创建Button
    Button btnSaveFile= new Button("保存文件");
    //添加Button的事件监听
    btnSaveFile.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    if( dlgFileDialog == null )
                     dlgFileDialog=new FileDialog(FileDialogDemo.this);
                    //设置文件对话框为保存模式
                    dlgFileDialog.setMode(FileDialog.SAVE);
                    //如果对话框已经显示在屏幕上,则直接返回
                    if( dlgFileDialog.isShowing()) return;
                    //显示对话框
                    dlgFileDialog.show();
                    String strFileName=dlgFileDialog.getFile();
                    if( strFileName == null )
                     System.out.println("你取消了文件的选择");
                    else
                     System.out.println("你选择的文件名称:"+ strFileName );
                }
    });
    //设置流式布局管理器
    this.setLayout( new FlowLayout());
    //把两个Button加入到Frame中,用FlowLayout布局管理器对其进行管理
    this.add(btnLoadFile);
    this.add(btnSaveFile);
    }
    public static void main(String[] args)
    {
    //创建框架窗体
    FileDialogDemo frmFileDialogDemo=new FileDialogDemo("文件对话框例子");
    //设置框架窗体的事件监听(关闭窗体事件)
    frmFileDialogDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    //显示框架窗体
    frmFileDialogDemo.pack();
    frmFileDialogDemo.show();
    }
    }例程12-14
    //FlowLayoutDemo.java
    import java.awt.*;
    import java.awt.event.*;
    public class FlowLayoutDemo extends Frame
    {
    public FlowLayoutDemo()
    {
    //设置框架窗口的布局管理器为Flowlayout
    this.setLayout( new FlowLayout());
    //向框架窗口添加8个Button
    this.add( new Button("1") );
    this.add( new Button("2") );
    this.add( new Button("3") );
    this.add( new Button("4") );
    this.add( new Button("5") );
    this.add( new Button("6") );
    this.add( new Button("7") );
    this.add( new Button("8") );
    }
    public static void main( String[] args )
    {
    FlowLayoutDemo frmFlowLayout=new FlowLayoutDemo();
    //设置框架窗体的事件监听(关闭窗体事件)
    frmFlowLayout.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    //显示框架窗体
    frmFlowLayout.pack();
    frmFlowLayout.show();
    }
    }例程12-15
    //BorderLayoutDemo.java
    import java.awt.*;
    import java.awt.event.*;
    public class BorderLayoutDemo extends Frame
    {
    public BorderLayoutDemo()
    {
    //设置框架窗口的布局管理器为BorderLayoutDemo
    this.setLayout( new BorderLayout());
    //上面一行代码可以不要,Frame默认是BorderLayout布局管理器 //向框架窗口添加5个Button
    this.add( new Button("南"), BorderLayout.SOUTH);
    this.add( new Button("北") ,BorderLayout.NORTH);
    this.add( new Button("中间") ,BorderLayout.CENTER);
    this.add( new Button("西") ,BorderLayout.WEST);
    this.add( new Button("东") ,BorderLayout.EAST);
    }
    public static void main( String[] args )
    {
    BorderLayoutDemo frmBorderLayout=new BorderLayoutDemo();

    //设置框架窗体的事件监听(关闭窗体事件)
    frmBorderLayout.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    //显示框架窗体
    frmBorderLayout.pack();
    frmBorderLayout.show();
    }
    //设置框架窗体的大小为300×300
    public Dimension getPreferredSize()
    {
    return new Dimension(300,300);
    }
    }
      

  4.   

    例程12-16
    //CardLayoutDemo.java
    import java.awt.*;
    import java.awt.event.*;public class CardLayoutDemo extends Frame
    {
    //包含四个功能按钮的Panel的定义和创建
    Panel pnlCommandArea=new Panel();
    //显示功能Panel的定义和创建
    Panel pnlDisplayArea=new Panel();
    //CardLayout布局管理器的创建
    CardLayout cardlayout1=new CardLayout();
    //四个功能按钮的定义和创建
    Button btnFirst=new Button("第一个");
    Button btnPrevious=new Button("前一个");
    Button btnNext=new Button("后一个");
    Button btnLast=new Button("最后一个");
    //框架窗体的构造方法
    public CardLayoutDemo()
    {
    //设置Frame的布局管理器为BorderLayout
    this.setLayout(new BorderLayout());
    //把两个Panel加入到布局管理器中
    this.add( pnlCommandArea, BorderLayout.NORTH);
    this.add( pnlDisplayArea, BorderLayout.CENTER);   //=======================================================
    // 显示功能区域
    //=======================================================
    //把显示功能区域Panel的布局管理器设置为CardLayout
    pnlDisplayArea.setLayout(cardlayout1);
    //创建第一个显示Panel
    Panel pnlFirst=new Panel();
    pnlFirst.setBackground(Color.blue);
    pnlFirst.setForeground(Color.white);
    pnlDisplayArea.add("first",pnlFirst);
    pnlFirst.add(new Label("This is the first Panel") );
    //创建第二个显示Panel
    Panel pnlSecond=new Panel();
    pnlSecond.setBackground(Color.red);
    pnlSecond.setForeground(Color.blue);
    pnlDisplayArea.add("second",pnlSecond);
    pnlSecond.add(new Label("This is the second Panel") );
    //创建第三个显示Panel
    Panel pnlThird=new Panel();
    pnlThird.setBackground(Color.black);
    pnlThird.setForeground(Color.white);
    pnlDisplayArea.add("third",pnlThird);
    pnlThird.add(new Label("This is the third Panel") );
    //创建第四个显示Panel
    Panel pnlFourth=new Panel();
    pnlFourth.setBackground(Color.green);
    pnlFourth.setForeground(Color.black);
    pnlDisplayArea.add("fourth",pnlFourth);
    pnlFourth.add(new Label("This is the fourth Panel") );
    //======================================================
    // 创建和显示功能按钮区域 //=====================================================
    //为四个功能按钮设置事件监听器
    btnFirst.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    processAction(e);
                }
    });
    btnPrevious.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    processAction(e);
                }
    });
    btnNext.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    processAction(e);
                }
    });
    btnLast.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
                {
                    processAction(e);
                }
    });
    //把四个功能按钮加入到Panel
    pnlCommandArea.add( btnFirst );
    pnlCommandArea.add( btnPrevious );
    pnlCommandArea.add( btnNext );
    pnlCommandArea.add( btnLast );
    }
    //程序的入口方法
    public static void main( String[] args )
    {
    //创建框架窗体的实例
    CardLayoutDemo frmCardLayout=new CardLayoutDemo();

    //设置框架窗体的事件监听(关闭窗体事件)
    frmCardLayout.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    //正常退出Java虚拟机
    System.exit(0);
    }
    });
    //显示框架窗体
    frmCardLayout.pack();
    frmCardLayout.show();
    }
    //设置框架窗体的大小为300×300
    public Dimension getPreferredSize()
    {
    return new Dimension(300,300);
    }
    //处理按钮的事件
    private void processAction(ActionEvent e)
    {
    //获取事件源(用户选择是哪个按钮)
    Button btnEvent=(Button)e.getSource();
    //判断是用户选择哪个按钮并调用相应的方法
    if( btnEvent.equals(btnFirst))
    cardlayout1.first( pnlDisplayArea );
    else if( btnEvent.equals(btnLast))
    cardlayout1.last( pnlDisplayArea );
    else if( btnEvent.equals(btnPrevious))
    cardlayout1.previous( pnlDisplayArea );
    else if( btnEvent.equals(btnNext))
    cardlayout1.next( pnlDisplayArea );
    }
    }
    以上是我拷贝的,希望对你有用
      

  5.   

    我给一个简单点的例子:温度计(摄氏华氏的转换)
    ( -- 引自<JAVA - First Contact>一书,16章,例2)import java.awt.* ;
    import java.awt.event.* ;public class Chapter16n2 extends Frame
    implements WindowListener, ActionListener
        {
        Temperature temp1 ;    private TextField celsiusField,
    fahrField ;
        private Canvas2 canvas ;
        private Button toF, toC, quit ;    /**
         *
         * constructor
         *
         */    public Chapter16n2()
    {
            temp1 = new Temperature() ; // set up basic window
    setTitle("Chapter16n2") ;
            setBackground(Color.green) ;
            setSize(400, 600) ;
            addWindowListener(this) ; // set up area for the thermometer
            canvas = new Canvas2(this) ;
    add("Center", canvas) ; // set up the panel of buttons and text fields // set up the first line of items
    Panel p1 = new Panel() ;
    p1.setLayout(new FlowLayout()) ;
    p1.add(new Label("Celsius")) ;
            celsiusField = new TextField("", 8) ;
    p1.add(celsiusField) ;
            toF = new Button("Convert to F") ;
    p1.add(toF) ;
            toF.addActionListener(this) ; // set up the second line of items
    Panel p2 = new Panel() ;
    p2.setLayout(new FlowLayout()) ;
    p2.add(new Label("Fahrenheit")) ;
            fahrField = new TextField("", 8) ;
    p2.add(fahrField) ;
    toC = new Button("Convert to C") ;
    p2.add(toC) ;
            toC.addActionListener(this) ; // set up the third line of items
    Panel p3 = new Panel() ;
    p3.setLayout(new FlowLayout()) ;
    quit = new Button("Quit") ;
    p3.add(quit) ;
            quit.addActionListener(this) ; Panel p = new Panel() ;
    p.setLayout(new BorderLayout()) ;
    p.add("North", p1) ;
    p.add("Center", p2) ;
    p.add("South", p3) ;
    add("South", p) ;
    } // end of constructor method    /**
         *
         * main
         *
         */    public static void main(String[] args)
            {
            Chapter16n2 f = new Chapter16n2() ;
            f.setVisible(true) ;
            } // end of main method    /**
         *
         * actionPerformed
         *
         */    public void actionPerformed(ActionEvent event)
            {
    // deal with "Quit" button
    if (event.getSource() == quit)
        {
        dispose();
        System.exit(0);
        }
    // deal with "Convert to Celsius" button
    else if (event.getSource() == toC)
        {
        String f1 = fahrField.getText() ;     double f2 = 0.0 ;
        boolean isValid ;
        try {
    f2 = Double.valueOf(f1).doubleValue() ;
    isValid = true ;
    }
                catch (NumberFormatException ex)
    {
    isValid = false ;
    }            if (isValid)
    {
            temp1.setFahr(f2) ;
            double c2 = temp1.getCelsius() ;
            c2 = ((double) Math.round(c2 * 100)) / 100 ;
            String c1 = Double.toString(c2) ;
            celsiusField.setText(c1) ;
    canvas.repaint() ;
    }
        else
    {
            fahrField.setText("") ;
    }
        }
    // deal with "Convert to Fahrenheit" button
    else if (event.getSource() == toF)
        {
        String c1 = celsiusField.getText() ;     double c2 = 0.0 ;
        boolean isValid ;
        try {
    c2 = Double.valueOf(c1).doubleValue() ;
    isValid = true ;
    }
                catch (NumberFormatException ex)
    {
    isValid = false ;
    }
                if (isValid)
    {
            temp1.setCelsius(c2) ;
            double f2 = temp1.getFahr() ;
            f2 = ((double) Math.round(f2 * 100)) / 100 ;
            String f1 = Double.toString(f2) ;
            fahrField.setText(f1) ;
    canvas.repaint() ;
    }
        else
    {
            celsiusField.setText("") ;
    }
        }
            } // end of method actionPerformed
    [待续]
      

  6.   

    [续上页]
        /**
         *
         * windowClosing
         *
         */    public void windowClosing(WindowEvent event)
            {
            dispose();
            System.exit(0);
            } // end of method windowClosing    public void windowOpened(WindowEvent event) {}
        public void windowIconified(WindowEvent event) {}
        public void windowDeiconified(WindowEvent event) {}
        public void windowClosed(WindowEvent event) {}
        public void windowActivated(WindowEvent event) {}
        public void windowDeactivated(WindowEvent event) {}
        } // end of class Chapter16n2class Canvas2 extends Canvas
        {
        Chapter16n2 parent ;    /**
         *
         * constructor
         *
         */    public Canvas2(Chapter16n2 f)
            {
            parent = f ;
            } // end of constructor method    /**
         *
         * paint
         *
         */    public void paint(Graphics g)
            {
    // set up some dimensions for the drawing
    Dimension d = getSize() ;
    int cx = d.width / 2,
        cy = d.height / 2 + 150,
        bulbInnerRadius = 40,
        bulbOuterRadius = 50,
        bulbInnerAngle = 20,
        bulbOuterAngle = 20,
        innerTubeRadius = 10,
        outerTubeRadius = 20,
        tubeLength = 300 ; // set up some dimensions for the text labels
    Font f1 = new Font("Helvetica", Font.BOLD, 14) ;
    FontMetrics fm1 = g.getFontMetrics(f1) ;
    int w0 = fm1.stringWidth("212F"),
        h0 = (fm1.getAscent() / 2) ; // draw surrounding line
    g.setColor(Color.black) ;
    g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ; // draw bulb
    g.setColor(Color.black) ;
    g.drawArc(cx - bulbOuterRadius,
      cy - bulbOuterRadius,
      bulbOuterRadius * 2,
      bulbOuterRadius * 2,
      90 + bulbOuterAngle,
      360 - (2 * bulbOuterAngle)) ;
    g.drawArc(cx - bulbInnerRadius,
      cy - bulbInnerRadius,
      bulbInnerRadius * 2,
      bulbInnerRadius * 2,
      90 + bulbInnerAngle,
      360 - (2 * bulbInnerAngle)) ;
    g.setColor(Color.red) ;
    g.fillOval(cx - bulbInnerRadius + 1,
       cy - bulbInnerRadius + 1,
       2 * (bulbInnerRadius - 1),
       2 * (bulbInnerRadius - 1)) ; // draw tube
    g.setColor(Color.black) ;
    g.drawLine(cx - outerTubeRadius,
       cy - bulbOuterRadius,
       cx - outerTubeRadius,
       cy - bulbOuterRadius - tubeLength) ;
    g.drawLine(cx - innerTubeRadius,
       cy - bulbInnerRadius,
       cx - innerTubeRadius,
       cy - bulbOuterRadius - tubeLength) ;
    g.drawLine(cx + innerTubeRadius,
       cy - bulbInnerRadius,
       cx + innerTubeRadius,
       cy - bulbOuterRadius - tubeLength) ;
    g.drawLine(cx + outerTubeRadius,
       cy - bulbOuterRadius,
       cx + outerTubeRadius,
       cy - bulbOuterRadius - tubeLength) ; // draw cap
    g.setColor(Color.black) ;
    g.drawArc(cx - outerTubeRadius,
      cy - bulbOuterRadius - tubeLength - outerTubeRadius,
      2 * outerTubeRadius,
      2 * outerTubeRadius,
      0,
      180) ;
    g.drawArc(cx - innerTubeRadius,
      cy - bulbOuterRadius - tubeLength - innerTubeRadius,
      2 * innerTubeRadius,
      2 * innerTubeRadius,
      0,
      180) ; // draw s
    g.setColor(Color.black) ;
    g.drawLine(cx - 40,
               cy - bulbOuterRadius - 20,
       cx - 30,
               cy - bulbOuterRadius - 20) ;
    g.drawLine(cx + 40,
               cy - bulbOuterRadius - 20,
       cx + 30,
               cy - bulbOuterRadius - 20) ;
    g.drawLine(cx - 40,
               cy - bulbOuterRadius - tubeLength + 20,
       cx - 30,
               cy - bulbOuterRadius - tubeLength + 20) ;
    g.drawLine(cx + 40,
               cy - bulbOuterRadius - tubeLength + 20,
       cx + 30,
               cy - bulbOuterRadius - tubeLength + 20) ;
    g.drawString("0C",
                 cx - 50 - w0,
         cy - bulbOuterRadius - 20 + h0) ;
    g.drawString("32C",
                 cx + 50,
         cy - bulbOuterRadius - 20 + h0) ;
    g.drawString("100C",
                 cx - 50 - w0,
         cy - bulbOuterRadius - tubeLength + 20 + h0) ;
    g.drawString("212C",
                 cx + 50,
         cy - bulbOuterRadius - tubeLength + 20 + h0) ; // draw mercury
    double tempC = parent.temp1.getCelsius() ;
    int mercuryLength ;
    if (tempC < - 11)
    mercuryLength = 0 ;
    else if (tempC < 120)
    mercuryLength = (int) (30 + (2.6 * tempC)) ;
    else
    mercuryLength = tubeLength ;
    g.setColor(Color.red) ;
    g.fillRect(cx - innerTubeRadius + 1,
       cy - bulbInnerRadius - mercuryLength,
       2 * innerTubeRadius,
       mercuryLength) ;
    } // end of method paint
        } // end of class Canvas2
      

  7.   

    我的例子是一个application,而不是applet,所以用起来方便点,无需考虑html,直接可运行(用javac编译后,就可用java解释执行).
      

  8.   

    我觉得你也最好安装一个jbuilder,用向导创建窗体,再观察窗体的代码,然后再看看书了解界面设计时要用到那些类库,再对照代码,然后自己在其它编辑环境中独立写一个。。可能对你更有帮助。
      

  9.   

    to  zhlx1977(新) 
    解释一下可以吗?这一段addWindowListener的参数形势和C++有很大差别,你能说说参数属于什么类型吗?
    frmFrameDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    //正常退出Java虚拟机
    System.exit(0);
    }
    });
    new WindowAdapter(){...}中,{}里面的函数与 class WindowAdapter有什么关系?
      

  10.   

    to  zhlx1977(新) 
    C:\>javac Chapter16n2.java
    Chapter16n2.java:7: cannot resolve symbol
    symbol  : class Temperature
    location: class Chapter16n2
        Temperature temp1 ;
        ^
    Chapter16n2.java:22: cannot resolve symbol
    symbol  : class Temperature
    location: class Chapter16n2
            temp1 = new Temperature() ;
                        ^
    2 errors
      

  11.   

    to  : zzwu(未名) 
          undefined   name "Temperature"????
      

  12.   

    javac Chapter16n2.java  >>javac *.java  
    其中*为保存的文件名(必须与类名相同)
      

  13.   

    to  zhlx1977(新) 
    C:\>javac Chapter16n2.java  >>javac aaa.java
    error: cannot read: aaa.java
    Chapter16n2.java:7: cannot resolve symbol
    symbol  : class Temperature
    location: class Chapter16n2
        Temperature temp1 ;
        ^
    Chapter16n2.java:22: cannot resolve symbol
    symbol  : class Temperature
    location: class Chapter16n2
            temp1 = new Temperature() ;
                        ^
    3 errors
      

  14.   

    to  zhlx1977(新) 
    Chapter16n2就是类名。
      

  15.   

    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
    很多例子是错误的
      

  16.   

    谢谢zhlx1977(新) 。
    哪位高手可以解释一下这一段addWindowListener的参数形势和C++有很大差别,能说说参数属于什么类型:
    frmFrameDemo.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    //正常退出Java虚拟机
    System.exit(0);
    }
    });
    new WindowAdapter(){...}中,{}里面的函数与 class WindowAdapter有什么关系?
      

  17.   

    因为java的时间街口有很多方法,所以这里有很多适配器类来“弱化”这些方法,主要是为了简化编码。这里的语句实际是定义一个内部类继承WindowAdapter并构造一个实例传递给addWindowListener方法。public void windowClosing只是WindowAdapter其中的方法之一。这个适配器实现所有的WindowListener方法,但都是空方法,所以继承的时候子类只需实现需要的方法。可以看一下java.awt.event.WindowAdapter的源代码。