2013-06-20 69 views
9

我已經工作了與JProgressBar一個同時使用WindowsLookAndFeel,但我發現從本機進度條的一些主要區別:的JProgressBar的Windows LAF


這裏是天然的酒吧,在其常規狀態:

enter image description here

雖然這是Java的:

enter image description here

銳利的眼睛可能會看到java的頂級白色更亮,綠色也更亮一些。而且,本地是「填充」,而Java的空白邊框。


現在,本地不確定:

enter image description here

而Java的:

enter image description here

閃爍短得多,而且非常非常快(未顯示)。此外,由於本地可能看起來像一個煥發,Java的看起來很像一個「彗星」(尾巴,只從一側...)。


我的最後一個問題,堵塞棒:

enter image description here

可以這樣用Java來完成?

+0

用紅色屏蔽的條形顏色在這裏解決了幾次,包括JProgressBar中的彩虹顏色,請點擊目標???,只創建與Win7類似的配色方案,注意大多數用戶改變Win主題中的顏色。我==橘子),即將創建不確定,不知是什麼問題,搜索在@trashgod – mKorbel

+0

最好的帖子關於JProgressBar在帖子由@aterai,必須使用GradientPaint而不是純色 – mKorbel

回答

5
  • 這僅僅是最後一個問題的例子:
    • 的Windows 7(綠色的進度條)
    • JDK 1.7.0_25(WindowsLookAndFeel)

TurnProgressBarRedTest

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import java.beans.*; 
import javax.swing.*; 
import javax.swing.plaf.LayerUI; 

public class TurnProgressBarRedTest { 
    public JComponent makeUI() { 
    final BoundedRangeModel model = new DefaultBoundedRangeModel(); 
    final BlockedColorLayerUI layerUI = new BlockedColorLayerUI(); 
    final JPanel p = new JPanel(new GridLayout(4, 1, 12, 12)); 
    p.setBorder(BorderFactory.createEmptyBorder(24,24,24,24)); 

    final JProgressBar pb1 = new JProgressBar(model); 
    pb1.setStringPainted(true); 
    p.add(pb1); 
    final JProgressBar pb2 = new JProgressBar(model); 
    pb2.setStringPainted(true); 
    p.add(pb2); 

    p.add(new JProgressBar(model)); 
    p.add(new JLayer<JProgressBar>(new JProgressBar(model), layerUI)); 

    JPanel box = new JPanel(); 
    box.add(new JButton(new AbstractAction("+10") { 
     private int i = 0; 
     @Override public void actionPerformed(ActionEvent e) { 
     model.setValue(i = (i>=100) ? 0 : i + 10); 
     } 
    })); 
    //http://msdn.microsoft.com/en-us/library/windows/desktop/aa511486.aspx 
    box.add(new JCheckBox(new AbstractAction(
     "<html>Turn the progress bar red<br />"+ 
     " when there is a user recoverable condition<br />"+ 
     " that prevents making further progress.") { 
     @Override public void actionPerformed(ActionEvent e) { 
     boolean b = ((JCheckBox)e.getSource()).isSelected(); 
     pb2.setForeground(b? new Color(255,0,0,100) : pb1.getForeground()); 
     layerUI.isPreventing = b; 
     p.repaint(); 
     } 
    })); 

    JPanel panel = new JPanel(new BorderLayout()); 
    panel.add(p, BorderLayout.NORTH); 
    panel.add(box, BorderLayout.SOUTH); 
    return panel; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(new TurnProgressBarRedTest().makeUI()); 
    f.setSize(320, 240); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
} 
class BlockedColorLayerUI extends LayerUI<JProgressBar> { 
    public boolean isPreventing = false; 
    private BufferedImage bi; 
    private int prevw = -1; 
    private int prevh = -1; 
    @Override public void paint(Graphics g, JComponent c) { 
    if(isPreventing) { 
     JLayer jlayer = (JLayer)c; 
     JProgressBar progress = (JProgressBar)jlayer.getView(); 
     int w = progress.getSize().width; 
     int h = progress.getSize().height; 

     if(bi==null || w!=prevw || h!=prevh) { 
     bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
     } 
     prevw = w; 
     prevh = h; 

     Graphics2D g2 = bi.createGraphics(); 
     super.paint(g2, c); 
     g2.dispose(); 

     Image image = c.createImage(
         new FilteredImageSource(bi.getSource(), 
         new RedGreenChannelSwapFilter())); 
     g.drawImage(image, 0, 0, null); 
    } else { 
     super.paint(g, c); 
    } 
    } 
} 
class RedGreenChannelSwapFilter extends RGBImageFilter { 
    @Override public int filterRGB(int x, int y, int argb) { 
    int r = (int)((argb >> 16) & 0xff); 
    int g = (int)((argb >> 8) & 0xff); 
    int b = (int)((argb  ) & 0xff); 
    return (argb & 0xff000000) | (g<<16) | (r<<8) | (b); 
    } 
} 
+0

這就是它看起來像在Windows 8.1中:http://i.imgur.com/nUYsSkB.png不幸的是不能很好地工作。 – RAnders00