首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ProgressBar运用小例子

2012-11-06 
ProgressBar使用小例子?package com.xhm.testimport java.awt.event.ActionEventimport java.awt.event.

ProgressBar使用小例子

?

package com.xhm.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class ProgressBarTest extends JFrame {

??? private JProgressBar bar;
??? private JButton bt;
??? private JLabel lab;
??? private Timer timer;

??? public ProgressBarTest() {
??? ??? init();

??? ??? setTitle("ProgressBarTest");
??? ??? setSize(400, 300);
??? ??? setLocationRelativeTo(null);
??? }

??? private void init() {
??? ??? setLayout(null);

??? ??? bt = new JButton();
??? ??? bt.setSize(100, 30);
??? ??? bt.setLocation(100, 50);
??? ??? bt.setText("Start");
??? ??? bt.addActionListener(new ActionListener() {

??? ??? ??? public void actionPerformed(ActionEvent e) {
??? ??? ??? ??? bt_click(e);
??? ??? ??? }
??? ??? });
??? ??? add(bt);

??? ??? bar = new JProgressBar();
??? ??? bar.setBorder(BorderFactory.createEtchedBorder());
??? ??? bar.setLocation(50, 100);
??? ??? bar.setSize(200, 15);
??? ??? bar.setMinimum(0);
??? ??? bar.setMaximum(100);
??? ??? bar.setStringPainted(true);
??? ??? add(bar);

??? ??? lab = new JLabel();
??? ??? lab.setSize(100, 30);
??? ??? lab.setLocation(100, 150);
??? ??? lab.setText("");
??? ??? add(lab);

??? ??? timer = new Timer(50, bt.getActionListeners()[0]);
??? ??? timer.addActionListener(new ActionListener() {

??? ??? ??? public void actionPerformed(ActionEvent e) {
??? ??? ??? ??? timer_action(e);
??? ??? ??? }
??? ??? });

??? }

??? private void bt_click(ActionEvent e) {
??? ??? try {
??? ??? ??? timer.start();
??? ??? } catch (Exception ee) {
??? ??? ??? ee.printStackTrace();
??? ??? }
??? }

??? private void timer_action(ActionEvent e) {
??? ??? try {
??? ??? ??? int i = bar.getValue();
??? ??? ??? if (i < 100) {
??? ??? ??? ??? i++;
??? ??? ??? ??? bar.setValue(i);
??? ??? ??? ??? lab.setText(bar.getValue() + "%");
??? ??? ??? }else
??? ??? ??? {
??? ??? ??? ??? timer.stop();
??? ??? ??? }
??? ??? } catch (Exception ee) {
??? ??? ??? ee.printStackTrace();
??? ??? }
??? }

??? public static void main(String[] args) {
??? ??? new ProgressBarTest().setVisible(true);
??? }

}

热点排行