2012-09-27 25 views
1

我想使用Swing應用程序的系統外觀。所以我使用了getSystemLookAndFeelClassName()方法,它工作得很好。在Swing應用程序中擴展系統外觀

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

但是,現在我想改變應用程序的所有JButtons(在我所有的JFramesJDialogsJOptionPanesJFileChoosers等),只有JButtons。所以我想知道如何擴展系統外觀以保留除JButtons(和JPanels,我想要灰色背景)之外的所有組件。

謝謝。

+3

另請參閱[UIManager默認值](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) – Robin

+0

被引[此](http://stackoverflow.com/a/3705571/230513 )。請嘗試着重您的非常廣泛的問題;一個[sscce](http://sscce.org/)會很有幫助。 – trashgod

+0

謝謝你的指點。我會在幾個小時內看一看。對不起,但沒有SSCCE提供。我的問題是一般性的,因爲我真的不知道這樣做。 –

回答

1

最後我延長了系統的外觀和感覺是這樣的:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
UIManager.put("ButtonUI", "com.my.package.MyButtonUI"); 
UIManager.put("Panel.background", Color.green); 

隨着MyButtonUI

public class MyButtonUI extends BasicButtonUI { 

    public static final int BUTTON_HEIGHT = 24; 

    private static final MyButtonUI INSTANCE = new MyButtonUI(); 

    public static ComponentUI createUI(JComponent b) { 
     return INSTANCE; 
    } 

    @Override 
    public void paint(Graphics g, JComponent c) { 
     AbstractButton button = (AbstractButton) c; 
     Graphics2D g2d = (Graphics2D) g; 
     final int buttonWidth = button.getWidth(); 
     if (button.getModel().isRollover()) { 
      // Rollover 
      GradientPaint gp = new GradientPaint(0, 0, Color.green, 0, BUTTON_HEIGHT * 0.6f, Color.red, true); 
      g2d.setPaint(gp); 
     } else if (button.isEnabled()) { 
      // Enabled 
      GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, BUTTON_HEIGHT * 0.6f, Color.gray, true); 
      g2d.setPaint(gp); 
     } else { 
      // Disabled 
      GradientPaint gp = new GradientPaint(0, 0, Color.black, 0, BUTTON_HEIGHT * 0.6f, Color.blue, true); 
      g2d.setPaint(gp); 
     } 
     g2d.fillRect(0, 0, buttonWidth, BUTTON_HEIGHT); 
     super.paint(g, button); 
    } 

    @Override 
    public void update(Graphics g, JComponent c) { 
     AbstractButton button = (AbstractButton) c; 
     if (isInToolBar(button)) { 
      // Toolbar button 
      button.setOpaque(false); 
      super.paint(g, button); 
     } else if (button.isOpaque()) { 
      // Other opaque button 
      button.setRolloverEnabled(true); 
      button.setForeground(Color.white); 
      paint(g, button); 
     } else { 
      // Other non-opaque button 
      super.paint(g, button); 
     } 
    } 

    private boolean isInToolBar(AbstractButton button) { 
     return SwingUtilities.getAncestorOfClass(JToolBar.class, button) != null; 
    } 
} 

注:我higly推薦此鏈接:http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/(羅賓)

謝謝。

0

你也可以製作自己的按鈕類,例如CustomButton擴展了JButton,所有的修改都使用它來代替標準的JButton。這樣,您就可以使用定製和標準按鈕 - 如果你改變主意,想的地方添加一個標準的JButton;)

+0

我可以。但是我怎麼能在'JOptionPane.showMessageDialog()'中使用這個'CustomButtom'? –

+0

如果您使用UIManager,則自定義按鈕將出現在消息對話框中。如果你使用一個CustomButton類,忘記showMessageDialog(),並讓你自己的JDialogs顯示你自己的按鈕和任何你想要的。 – Rempelos

+0

我將如何添加顏色到自定義按鈕和常規的?實際上即時通訊使用一個標籤,但你分配顏色的屬性也是如此,因爲所有的屬性都是Label.foreground你如何設置一個自定義類的屬性 – AngryDuck