2012-08-03 49 views
3

我正在開發使用Java NetBeans編輯器有點GUI應用程序的更改背景顏色。 我已經在JFrame中放入了一個簡單的進度條。 我開發的項目,JDK7雨雲L&F - 進度條

我想從默認的橙色背景顏色更改爲個人之一。我已經嘗試了顏色變化的所有屬性,但是當我運行該程序時顏色仍然相同。

我已經嘗試過使用 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

橙顏色....

編輯 用下面的代碼

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

是否有可能改變顏色與原來的顏色一樣的效果?

+1

我認爲這是您使用的LaF的問題。只是片刻,我正在進行一些測試。從我在這個世界的角落 – davidbuzatto 2012-08-03 20:42:20

+0

@Andrew湯普森是一位SSCCE non_accesible,或者我squid_proxy再有問題 – mKorbel 2012-08-03 20:46:41

+0

我在這裏編輯您的文章,並添加代碼,請回復的變化,如果......,問題可以清楚交代現在 – mKorbel 2012-08-03 21:23:02

回答

5
  1. 也許(ColorFont)問題談論Nimbus Look and Feel

  2. 必須check this thread

進度[禁用+完成] .foregroundPainter 進度[禁用+不確定] .foregroundPainter
進度[無效] .backgroundPainter 進度[無效] .foregroundPainter 進度[有效+完成] .foregroundPainter 進度[有效+不確定] .foregroundPainter 進度[有效+不確定]。 progressPadding 進度[有效] .backgroundPainter 進度[有效] .foregroundPainter

。 4。也許最簡單的你將是從Nimbus default S上的隔空nimbusOrange,但這種變化是無處不在,

滑稽輸出與改變Control Color圖形用戶界面,例如

UIManager.getLookAndFeelDefaults()。把(「nimbusOrange」,(新顏色(127,255,191)));

。 5.如

enter image description here

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); 
    } 
} 
+0

這是Netbeans的一個項目,如何發佈整個項目? – 2012-08-03 21:04:14

+0

乾淨和建立?從菜單:-) – mKorbel 2012-08-03 21:05:35

+0

這裏全碼:http://pastebin.com/ddCFFHeb – 2012-08-03 21:07:40

3

這是我在做應用我目前開發

ColorUIResource colorResource = new ColorUIResource(Color.red.darker().darker()); 
UIManager.put("nimbusOrange",colorResource); 

這只是改變默認的橙色的東西更吸引目光它隨處改變它。