我目前正在處理自定義合成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都存在相同的問題)。
如果我手動設置顯示的默認按鈕的尺寸正確,所以我假定存在這樣的問題計算按鈕的大小。
也許有人有類似的問題,或者可以給我一個提示,爲什麼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
佈局問題工作? [大小]不正確(http://stackoverflow.com/q/7229226/230513)?請編輯您的問題以包含一個[sscce](http://sscce.org/),它可以重現您描述的問題。 – trashgod
我已經爲我原來的問題添加了一個簡單的例子。沒有特別的佈局或別的東西。 我只是不認爲它像預期的那樣工作。 – user1845266
注意:缺少圖片和結束標記。 – trashgod