Java语言程序设计课程实验题目 第六次实验

Java语言程序设计课程实验题目

第六次实验

1. 在IDE中输入并观察以下代码,分析该段程序的作用。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class TestJProgressBar extends JFrame implements ActionListener{

    Container ctp = getContentPane();

    JPanel jp = new JPanel();

    JProgressBar tp = new JProgressBar(JProgressBar.HORIZONTAL, 0, 300);

       

    public static void main(String[] args) {

        TestJProgressBar tj = new TestJProgressBar();

        tj.launchFrame();

    }

    public void launchFrame() {

        JButton jb1 = new JButton("Start");

 

        jb1.addActionListener(this);

       

        ctp.add(tp, BorderLayout.NORTH);

       

        ctp.add(jp, BorderLayout.CENTER);

        jp.add(jb1);

       

        jp.setLayout(new FlowLayout());

       

        this.setSize(200,100);

        this.setVisible(true); 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

   

    public void actionPerformed(ActionEvent e) {

            tp.setValue(0);   // clear

            MyRunner mr1 = new MyRunner();

            Thread th1 = new Thread(mr1);

            th1.start();

           

            return;

       

    }

   

    class MyRunner implements Runnable {

       

        public void run() {

            while(tp.getValue() < tp.getMaximum()) {

                tp.setValue(tp.getValue() + 1);

               

                try {  

                    Thread.sleep(20);

                }catch(InterruptedException e) {}

            }

        }

       

    }

}

 

2. 在IDE中输入并观察以下代码,分析该段程序的作用。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestJRadioButton implements ActionListener {

         JLabel jl = new JLabel("Please select your gender");

       ButtonGroup bg   = new ButtonGroup();

       JRadioButton rb1 = new JRadioButton("Male",   false);

       JRadioButton rb2 = new JRadioButton("Female", false);

         public void launchFrame() {

                   JFrame f = new JFrame("Test JRadioButton");

                   Container ct = f.getContentPane();          

                   ct.setLayout(new FlowLayout());

                   ct.add(jl);  ct.add(rb1);  ct.add(rb2);           

                   // add two radio buttons into same button group

                   bg.add(rb1);  bg.add(rb2);             

                   // add Listener for two radio buttons

                   rb1.addActionListener(this);  rb2.addActionListener(this);

         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                   f.setSize(200,100);  f.setVisible(true);

       }

       public static void main(String[] args){

                TestJRadioButton tr = new TestJRadioButton();

                tr.launchFrame();

       }    

       public void actionPerformed(ActionEvent e ) {    

                String rbt = e.getActionCommand();

                System.out.println("You selec

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值