2014-09-03 43 views
0

我試圖使所有JLabels在默認情況下都具有不透明的自定義外觀和感覺。我可以設置像前景(Label.foreground),但你如何設置不透明的屬性?使用外觀將所有JLabels設置爲不透明

編輯#1: 我正在創建自定義外觀&感覺延伸MetalLookAndfeel。我重寫了initComponentDefaults(UIDefaults)方法。不過,我開放其他方式,涉及使用看看&的感覺。

編輯#2: 我試過Vince Emigh的建議,但它沒有奏效。看起來,JLabel上的不透明屬性存在一個錯誤 - 請參閱make-jlabel-backround-transparent-again

編輯#3:代碼示例

class Demo { 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       UIManager.put("Label.opaque", Boolean.valueOf(true)); 
       UIManager.put("Label.background", new ColorUIResource(Color.RED)); 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(3); 

       JLabel yoyo = new JLabel("YOYOYOYO"); 
       yoyo.setOpaque(true);// try once with this commented out and note difference 

       JPanel panel = new JPanel(); 
       panel.setSize(200, 200); 
       panel.setBackground(Color.BLUE); 
       panel.add(yoyo); 

       frame.add(panel); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

我也試着設置JPanel的不透明性質 - 同樣的結果。看來使用UIManager設置不透明屬性對任何對象都沒有影響。 :(

是否有另一種方式做到這一點?

+0

看起來和感覺管理您使用的?Synth的? – markspace 2014-09-03 15:31:39

+0

我假設你不能在構造函數中繼承JLabel和'setOpaque(true)'? – remi 2014-09-03 15:44:39

回答

0
UIManager.put("Label.opaque", Boolean.valueOf(true)); 

它像任何其他財產


class Demo { 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       UIManager.put("Label.opaque", Boolean.valueOf(true)); 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(3); 

       JPanel panel = new JPanel(); 
       panel.setSize(200, 200); 
       panel.setBackground(Color.BLUE); 
       panel.add(new JLabel("YOYOYOYO"); 

       frame.add(panel); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

我不得不試試這個。之前我沒有這樣做的原因是,當我查看Metal的L&F屬性列表bel使用[鏈接](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/)我沒有看到「不透明」列出。 – BigMac66 2014-09-03 19:54:37

+0

@ BigMac66我不確定你指的是哪個列表(沒有在鏈接中看到列表),但我在發佈之前對其進行了測試。讓我知道,如果你得到的結果,你正在尋找 – 2014-09-03 19:57:42

+0

不幸的是,它沒有奏效。我正在Windows 7計算機上運行java版本1.7.0_21。順便說一句,在我的鏈接指向的網站上沒有列表 - 我有一個演示應用程序,我發現它可以用來調查各種L&F - 它們並不完全相同!將L&F設置爲Metal並選擇Label,您將看到JLabel的所有默認值。 – BigMac66 2014-09-04 13:38:12

相關問題