欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Swing-12 Swing实例10进度条

程序员文章站 2022-03-30 15:57:22
...
  • [ 声明] 学习笔记和代码均参考自徐彤老师(ITFlight)的视频

进度条
Java Swing-12 Swing实例10进度条
Java Swing-12 Swing实例10进度条
Java Swing-12 Swing实例10进度条
Java Swing-12 Swing实例10进度条

package demo10;

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

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

/*
 * 时钟控制进度条移动
 * */

public class Demo1 extends JFrame implements ActionListener{
	
	Timer t=new Timer(100,this);
	JProgressBar pos=new JProgressBar(JProgressBar.HORIZONTAL);
	
	Demo1(){
		setSize(600,600);
		
		pos.setMinimum(0);
		pos.setMaximum(100);
		pos.setValue(10);
		pos.setStringPainted(true);
		
		add(pos,BorderLayout.NORTH);
		
		t.start();
		
		setVisible(true);
	}
	int x=10;
	public void actionPerformed(ActionEvent e) {
		x=x+10;
		pos.setValue(x);
		
	}
	public static void main(String[] args) {
		new Demo1();
	}
	
}
package demo10;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

/*
 * 点击step按钮,进度条移动
 * */

public class Demo2 extends JFrame implements ActionListener{
	
	JButton button=new JButton("step");
	JProgressBar pos=new JProgressBar(JProgressBar.HORIZONTAL);
	
	Demo2(){
		setSize(600,600);
		
		pos.setMinimum(0);
		pos.setMaximum(100);
		pos.setValue(10);
		pos.setStringPainted(true);
		
		add(pos,BorderLayout.NORTH);
		add(button,BorderLayout.SOUTH);
		
		
		button.addActionListener(this);
		
		setVisible(true);
	}
	int x=10;
	public void actionPerformed(ActionEvent e) {
		x=x+10;
		pos.setValue(x);
		
	}
	public static void main(String[] args) {
		new Demo2();
	}
	
}
package demo10;

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

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

/*
 * 两个时钟分别控制水平和垂直进度条移动
 * */

public class Demo3 extends JFrame implements ActionListener{
	
	Timer t=new Timer(100,this);
	Timer t2=new Timer(100,this);
	JProgressBar pos=new JProgressBar(JProgressBar.HORIZONTAL);
	JProgressBar pos2=new JProgressBar(JProgressBar.VERTICAL);
	
	Demo3(){
		setSize(600,600);
		
		pos.setMinimum(0);
		pos.setMaximum(100);
		pos.setValue(10);
		
		pos2.setMinimum(0);
		pos2.setMaximum(100);
		pos2.setValue(10);
		
		pos.setStringPainted(true);
		pos2.setStringPainted(true);
		
		add(pos,BorderLayout.NORTH);
		add(pos2,BorderLayout.CENTER);
		
		t.start();
		t2.start();
		
		setVisible(true);
	}
	int x=10;
	int y=0;
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==t) {
			x=x+1;
			pos.setValue(x);
		}
		if(e.getSource()==t2) {
			y=y+2;
			pos2.setValue(y);
		}
		
	}
	public static void main(String[] args) {
		new Demo3();
	}
	
}
package demo10;

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

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

/*
 * 两个时钟分别控制水平和垂直进度条移动
 * */

public class Demo4 extends JFrame implements ActionListener{
	
	Timer t=new Timer(100,this);
	Timer t2=new Timer(100,this);
	JProgressBar pos=new JProgressBar(JProgressBar.HORIZONTAL); //水平进度条
	JProgressBar pos2=new JProgressBar(JProgressBar.VERTICAL);//垂直进度条
	
	Demo4(){
		setSize(600,600);
		
		pos.setMinimum(0); //最大值
		pos.setMaximum(100);//最小值
		pos.setValue(10);//当前值
		
		pos2.setMinimum(0);
		pos2.setMaximum(100);
		pos2.setValue(10);
		
		pos.setBackground(Color.red);//背景和前景色
		pos.setForeground(Color.green);
		pos2.setBackground(Color.red);
		pos2.setForeground(Color.green);
		
		
		pos.setStringPainted(true);//显示百分比
		pos2.setStringPainted(true);
		
		add(pos,BorderLayout.NORTH);
		add(pos2,BorderLayout.CENTER);
		
		t.start();
		t2.start();
		
		setVisible(true);
	}
	int x=10;
	int y=0;
	int a=1,b=1;
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==t) {
			x=x+a
			if(x>100||x<0) {
				a*=-1;
			}
			pos.setValue(x);
		}
		if(e.getSource()==t2) {
			y=y+b;
			if(y>100||y<0) {
				b*=-1;
			}
			pos2.setValue(y);
		}
		
	}
	public static void main(String[] args) {
		new Demo4();
	}
	
}