我正在使用JProgressBar
组件以及Nimbus UI默认值。问题是,当我想手动更改每个进度条的Bar颜色时,我通过设置JProgressBar.setUI()
函数使用BasicProgressBarUI
。这使得更麻烦,因为我想只改变条的颜色,似乎我松开jprogressbar的默认外观(Border,backgroundcolor消失)。JProgressBar动态更改条形颜色
因此,当代码初始化时,我可以设置Nimbus ProgressBar的UIDefaults
。有用。
但我想动态改变每个进度条的颜色条。
是否有任何其他方式更改JProgressBar
的条形颜色?
public class ProgressGenerator extends JFrame {
protected int minValue = 0;
protected int maxValue = 100;
protected int counter = 0;
protected JProgressBar progressBar;
public ProgressGenerator() {
super("JProgressBar Demo");
setSize(300, 100);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
progressBar = new JProgressBar();
progressBar.setMinimum(minValue);
progressBar.setMaximum(maxValue);
progressBar.setStringPainted(true);
progressBar.setForeground(Color.GREEN);
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 ProgressGenerator();
}
}
为更好地帮助更快张贴[SSCCE(http://sscce.org/),短,可运行,编译,否则要搜索,谷歌可以返回 – mKorbel
参见[这] (http://stackoverflow.com/questions/7174420/change-colors-for-jprogressbar-with-nimbus)类似的问题/答案,它显示了如何使用'UIDefaults'和'putClientProperty'来改变一个'JProgressBar'颜色 –
目前的问题是当我使用setForeground(Color.GREEN)更改条形颜色时;它会更改SelectionBackground颜色。看来,setBackgroundColor(Color.RED);命令不会执行任何操作。 – mbasol