2013-07-12 32 views
2

基於http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html,目的是創建一個適合JTabbedPane的一個選項卡的按鈕How to add close button to a JTabbedPane Tab?我構建了以下代碼。它應該在java文檔中顯示一個迷你按鈕,但即使如此,我改變了大小的結果是相同的。請問我做錯了什麼?在此先感謝按鈕大小以微型結算

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class buttontest { 

public static void main (String[] args){ 

     buttontest t = new buttontest(); 

    } 

public buttontest(){ 
    JFrame test = new JFrame(); 
    JPanel pnlTab = new JPanel(); 
    JButton btnClose = new JButton("x"); 
    btnClose.putClientProperty("JComponent.sizeVariant", "mini"); 

    pnlTab.add(btnClose); 
    test.add(pnlTab); 
    test.setVisible(true); 
      } 
} 

編輯一個:

try { 
     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (UnsupportedLookAndFeelException e) { 
     // handle exception 
    } catch (ClassNotFoundException e) { 
     // handle exception 
    } catch (InstantiationException e) { 
     // handle exception 
    } catch (IllegalAccessException e) { 
     // handle exception 
    } 
+4

您只能將該屬性與支持該屬性的LAF一起使用。 Nimbus LAF支持該財產。 – camickr

+0

請參閱['TabComponentsDemo'](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg)。 – trashgod

+0

@camickr可以使用編輯一個更新來調用代碼,但是我沒有看到任何重大差異,那真的有用嗎? –

回答

1

我想是這樣的 「X」 按鈕

您可以使用實際的圖標從該圖像。

在從演示代碼ButtonTabComponent類,你可以添加以下語句來TabButton私有類的構造函數:

setIcon(UIManager.getIcon("InternalFrame.closeIcon")); 

然後註釋掉的paintComponent()之類的代碼和圖標被塗成關閉按鈕。

+0

我會結帳以後因爲我已經厭倦了程序;但是我想問一下,因爲這個工作就像一個paintComponent(),它將被繪製爲圖標的像素大小或者像按鈕組件的默認大小一樣?感謝您的支持,儘快我會檢查正確答案,如果適用 –