java软件更改背景颜色_Java Swing - 如何更改JProgressBar的背景颜色

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JProgressBar;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

public class Main extends JFrame {

protected int minValue = 0;

protected int maxValue = 100;

protected int counter = 0;

protected JProgressBar progressBar;

public Main() {

setSize(300, 100);

UIManager.put("ProgressBar.selectionBackground", Color.black);

UIManager.put("ProgressBar.selectionForeground", Color.white);

UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

progressBar = new JProgressBar();

progressBar.setMinimum(minValue);

progressBar.setMaximum(maxValue);

progressBar.setStringPainted(true);

JButton start = new JButton("Start");

start.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Thread runner = new Thread() {

public void run() {

counter = minValue;

while (counter <= maxValue) {

Runnable runme = new Runnable() {

public void run() {

progressBar.setValue(counter);

}

};

SwingUtilities.invokeLater(runme);

counter++;

try {

Thread.sleep(100);

} catch (Exception ex) {

}

}

}

};

runner.start();

}

});

getContentPane().add(progressBar, BorderLayout.CENTER);

getContentPane().add(start, BorderLayout.WEST);

WindowListener wndCloser = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

};

addWindowListener(wndCloser);

setVisible(true);

}

public static void main(String[] args) {

new Main();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值