我正在使用Netbeans Editor开发一个带有Java的GUI应用程序。 我在JFrame中添加了一个简单的进度条。 我正在用JDK7
开发项目我想将背景颜色从默认橙色更改为个人颜色。我已经尝试了所有颜色变化的属性,但是当我运行程序时颜色仍然相同。
我已经尝试过使用了 ProgressBar1.setBackground(new java.awt.Color(0,204,255));
和
UIManager.put("ProgressBar.background", Color.YELLOW);
UIManager.put("ProgressBar.foreground", Color.MAGENTA);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
相同的结果.....背景总是橙色
这是我的测试项目的代码
public class Frame extends javax.swing.JFrame
{
public Frame()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jProgressBar1 = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jProgressBar1.setBackground(new java.awt.Color(0, 204, 255));
jProgressBar1.setValue(75);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[])
{
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration
}
这是启动代码时的结果 progress bar http://img1.uploadscreenshot.com/images/orig/8/21508073887-orig.jpg
橙色......
EDIT 使用以下代码
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled].backgroundPainter", new FillPainter(Color.MAGENTA));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled].backgroundPainter", new FillPainter(Color.MAGENTA));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Indeterminate].progressPadding", new FillPainter(Color.ORANGE));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled].foregroundPainter", new FillPainter(Color.GREEN));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled].foregroundPainter", new FillPainter(Color.GREEN));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].foregroundPainter", new FillPainter(Color.GREEN));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled+Finished].foregroundPainter", new FillPainter(Color.GREEN));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled+Indeterminate].foregroundPainter", new FillPainter(Color.GREEN));
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Indeterminate].foregroundPainter", new FillPainter(Color.GREEN)); `
这一行之后
javax.swing.UIManager.setLookAndFeel(info.getClassName());
我已成功更改背景颜色。 但现在颜色是“普通的”,没有像橙色那样的渐变。
progress bar http://img1.uploadscreenshot.com/images/orig/8/21508411677-orig.jpg
是否可以使用与原始颜色相同的效果来改变颜色?
答案 0 :(得分:5)
也许(Color
和Font
)问题谈论Nimbus Look and Feel
进度[禁用+完成] .foregroundPainter 进度[禁用+不确定] .foregroundPainter
进度[无效] .backgroundPainter 进度[无效] .foregroundPainter 进度[有效+完成] .foregroundPainter 进度[有效+不确定] .foregroundPainter 进度[有效+不确定] .progressPadding 进度[有效] .backgroundPainter 进度[有效] .foregroundPainter
。
4.也许最简单的方法是从nimbusOrange
的顶部更改Nimbus default
,但这种变化无处不在,
带有更改Control Color
的GUI的有趣输出,例如
UIManager.getLookAndFeelDefaults()。put(“nimbusOrange”,(new Color(127,255,191)));
。 5.例如
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import javax.swing.*;
public class MyPopupWithNimbus {
public MyPopupWithNimbus() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JList list = new JList();
panel.add(list);
JProgressBar progress = new JProgressBar();
progress.setStringPainted(true);
progress.setString("60 pct");
progress.setValue(60);
frame.add(panel);
frame.add(progress, BorderLayout.SOUTH);
JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("A"));
menu.add(new JMenuItem("B"));
menu.add(new JMenuItem("C"));
JMenu jmenu = new JMenu("D");
jmenu.add(new JMenuItem("E"));
menu.add(jmenu);
frame.setVisible(true);
menu.show(frame, 50, 50);
}
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
UIManager.getLookAndFeelDefaults().put("nimbusOrange", (new Color(127, 255, 191)));
UIManager.getLookAndFeelDefaults().put("PopupMenu[Enabled].backgroundPainter",
new FillPainter(Color.ORANGE));
}
}
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyPopupWithNimbus aa = new MyPopupWithNimbus();
}
});
}
}
class FillPainter implements Painter<JComponent> {
private final Color color;
FillPainter(Color c) {
color = c;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}
答案 1 :(得分:4)
这是我在目前开发的应用程序中所做的
ColorUIResource colorResource = new ColorUIResource(Color.red.darker().darker());
UIManager.put("nimbusOrange",colorResource);
这简单地改变了默认的橙色,使其更具吸引力,并且随处可见。