2013-08-12 48 views
0

我目前正在處理自定義合成lnf,但無法獲得根窗格的默認按鈕顯示正確。根窗格默認按鈕不繪製correclty與自定義合成lnf

所有按鈕,定義如下繪製正確:

<style id="Button"> 
    <property key="Button.defaultButtonFollowsFocus" type="boolean" value="true"/> 
    <property key="Button.textShiftOffset" type="integer" value="1"/> 
    <property key="Button.margin" type="insets" value="0 10 0 10"/> 
    <insets top="4" left="4" bottom="4" right="4"/> 

    <state> 
     <imagePainter method="buttonBackground" path="images/Button.Normal.png" sourceInsets="4 4 4 4" paintCenter="true" stretch="true"/> 
    </state> 

    <state value="MOUSE_OVER"> 
     <imagePainter method="buttonBackground" path="images/Button.Normal.MouseOver.png" sourceInsets="4 4 4 4" paintCenter="true" stretch="true"/> 
    </state> 

    <state value="FOCUSED"> 
... 
    </state> 
... 
</style> 
<bind style="Button" type="region" key="Button"/> 

但如果我設置一個按鈕爲根窗格的defaultButton它不正確地繪製。

作爲一個例子,我採取了以下截圖。左邊的那個是普通按鈕,右邊是默認按鈕。如果我將左邊的一個設置爲defaultButton,那麼右邊的一個會正確繪製,並且會出現相同的問題(例如,如果顯示JFileChooseDialog,則所有defaultButton都存在相同的問題)。

left: normal button - right: default button

如果我手動設置顯示的默認按鈕的尺寸正確,所以我假定存在這樣的問題計算按鈕的大小。

也許有人有類似的問題,或者可以給我一個提示,爲什麼defaultButton不是正常的按鈕。

在此先感謝。

編輯:添加了簡單的可運行示例代碼。沒有特別的佈局,也沒有其他特別的東西。默認按鈕只是沒有畫。


DefaultButtonTest.java

public class DefaultButtonTest 
{ 
    public static void main(String[] args) throws InterruptedException, NamingException 
    { 
     MyLookAndFeel.install(); 
     JFrame testFrame = new JFrame("test"); 
     JButton button1 = new JButton("button1"); 
     JButton button2 = new JButton("button2"); 
     JPanel testPanel = new JPanel(); // Applying for example FlowLayout makes no difference 
     testPanel.add(button1); 
     testPanel.add(button2); 
     testFrame.setContentPane(testPanel); 
     testFrame.getRootPane().setDefaultButton(button1); 
     testFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     testFrame.revalidate(); 
     testFrame.pack(); 
     testFrame.setVisible(true); 
    } 
}  


MyLookAndFeel.java

public final class MyLookAndFeel extends SynthLookAndFeel 
{ 
    public final void initialize() 
    { 
     super.initialize(); 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JDialog.setDefaultLookAndFeelDecorated(true); 
    } 

    public final void uninitialize() 
    { 
     super.uninitialize(); 
    } 

    public static boolean install() 
    { 
     try 
     { 
      final long start = System.currentTimeMillis(); 
      MyLookAndFeel laf = new MyLookAndFeel(); 
      laf.load(MyLookAndFeel.class.getResourceAsStream("laf.xml"), MyLookAndFeel.class); 
      UIManager.setLookAndFeel(laf); 
      return true; 
     } 
     catch (final Exception exception) 
     { 
      exception.printStackTrace(); 
      return false; 
     } 
    } 

    public final boolean getSupportsWindowDecorations() 
    { 
     return true; 
    } 

    public final UIDefaults getDefaults() 
    { 
     UIDefaults defaults = super.getDefaults(); 
     defaults.put(SwingUtilities2.AA_TEXT_PROPERTY_KEY, SwingUtilities2.AATextInfo.getAATextInfo(true)); 
     defaults.addResourceBundle("com.sun.swing.internal.plaf.metal.resources.metal"); 
     return defaults; 
    } 
} 


laf.xml

<synth> 
    <!-- **************************************************************************************************************************************** 
    CONTROLS 
    ***************************************************************************************************************************************** --> 
    <style id="Button"> 
    <property key="Button.defaultButtonFollowsFocus" type="boolean" value="true"/> 
    <property key="Button.textShiftOffset" type="integer" value="1"/> 
    <property key="Button.margin" type="insets" value="0 10 0 10"/> 
    <insets top="4" left="4" bottom="4" right="4"/> 

    <state> 
     <imagePainter method="buttonBackground" path="images/Button.Normal.png" sourceInsets="4 4 4 4" paintCenter="true" stretch="true"/> 
    </state> 

    </style> 
    <bind style="Button" type="region" key="Button"/> 
</synth 
+0

佈局問題工作? [大小]不正確(http://stackoverflow.com/q/7229226/230513)?請編輯您的問題以包含一個[sscce](http://sscce.org/),它可以重現您描述的問題。 – trashgod

+0

我已經爲我原來的問題添加了一個簡單的例子。沒有特別的佈局或別的東西。 我只是不認爲它像預期的那樣工作。 – user1845266

+0

注意:缺少圖片和結束標記。 – trashgod

回答

1

我終於找到了解決這個問題,它是樸素簡單。

雖然狀態像'MOUSE OVER''FOCUSED'...從其父母繼承其字體和顏色屬性,他們不需要給予字體和顏色屬性。如果按鈕或單選按鈕定義了諸如「DEFAULT」或「SELECTED」之類的狀態,則它們不會從它們的父級繼承它們,並且需要明確定義字體和顏色(至少對於大小計算,如上所述,如果我手動設置大小字體和顏色顯示正確)。

例如

這並不適用於 '默認' 狀態

<style id="Button"> 
    <property key="Button.textShiftOffset" type="integer" value="1"/> 

    <state> 
     <imagePainter method="buttonBackground" path="images/Button.Normal.png" sourceInsets="4 4 4 4" paintCenter="true" stretch="true"/> 
    </state> 

</style> 
<bind style="Button" type="region" key="Button"/> 


這適用於 '默認' 狀態

<style id="Button"> 
    <property key="Button.textShiftOffset" type="integer" value="1"/> 

    <font name="Dialog" size="16"/> 
    <color type="TEXT_FOREGROUND" value="#000000"/> 

    <state> 
     <imagePainter method="buttonBackground" path="images/Button.Normal.png" sourceInsets="4 4 4 4" paintCenter="true" stretch="true"/> 
    </state> 

</style> 
<bind style="Button" type="region" key="Button"/> 
+0

我希望所有JButton狀態的工作,請參閱JButton API isXxx – mKorbel

+0

這是有道理的,因爲按鈕的首選大小部分基於字體。 – trashgod

1

經驗上,按鈕的首選大小是錯誤的。一個簡單的方法是使用其他按鈕的大小,如建議here

enter image description here

final JButton button2 = new JButton("button2"); 
JButton button1 = new JButton("button1"){ 

    @Override 
    public Dimension getPreferredSize() { 
     return button2.getPreferredSize(); 
    } 
}; 
button1.setBorder(BorderFactory.createLineBorder(Color.red)); 
+0

雖然這會起作用,但它只是解決未知問題的解決方法。 – user1845266