Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame中使用AWTUtilities的JPopupMenu。使用synth L&;设置window不透明(window,false);F不出现_Java_Swing_Jpopupmenu_Synth - Fatal编程技术网

Java JFrame中使用AWTUtilities的JPopupMenu。使用synth L&;设置window不透明(window,false);F不出现

Java JFrame中使用AWTUtilities的JPopupMenu。使用synth L&;设置window不透明(window,false);F不出现,java,swing,jpopupmenu,synth,Java,Swing,Jpopupmenu,Synth,这件事让我很困惑。基本上,我正在使用自定义Synth L&F开发一个使用透明形状窗口的多窗口应用程序。应用程序的部分调用父框架中的JFrame/JDialog组件。在这些组件中,我有弹出菜单和组合框,问题是一些使用应用程序的人在调用时体验到弹出菜单不出现。没有异常,代码执行良好,包括弹出菜单“show”方法 除了MacOSX上似乎没有什么问题之外,我一直试图将这一点落实到操作系统的细节上,但没有多少乐趣。有些windows用户,如我自己,没有遇到任何问题,其他人则会 此外,我还找到了设置窗口不透

这件事让我很困惑。基本上,我正在使用自定义Synth L&F开发一个使用透明形状窗口的多窗口应用程序。应用程序的部分调用父框架中的
JFrame
/
JDialog
组件。在这些组件中,我有弹出菜单和组合框,问题是一些使用应用程序的人在调用时体验到弹出菜单不出现。没有异常,代码执行良好,包括弹出菜单“show”方法

除了MacOSX上似乎没有什么问题之外,我一直试图将这一点落实到操作系统的细节上,但没有多少乐趣。有些windows用户,如我自己,没有遇到任何问题,其他人则会

此外,我还找到了设置窗口不透明度的违规代码行:

AWTUtilities.setWindowOpaque(window, false) 
如果我删除此LOC,则弹出窗口显示良好。此外,将此LOC替换为:

window.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
产生同样的问题。另一件事是,如果我使用默认的L&F,弹出窗口呈现ok

只是为了确认
JFrame
JDialog
组件的问题是相同的,我只是想知道是否有其他人遇到过这个问题,或者可以为我指出可能的原因

干杯

要复制的测试源:

import com.sun.awt.AWTUtilities;

import javax.swing.*;
import javax.swing.plaf.synth.SynthLookAndFeel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestFrame extends JFrame{

    public TestFrame(){
        super.setTitle("Test Frame");

        JButton btnDialog = new JButton("Open Dialog");
        btnDialog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TestDialog dialog = new TestDialog(TestFrame.this, true);
                dialog.setVisible(true);
            }
        });

        super.add(btnDialog, BorderLayout.CENTER);
        super.pack();
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setVisible(true);
    }

    public static void main(String[] args){
        initLookAndFeel();
        new TestFrame();
    }

    public static void initLookAndFeel() {
        SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();
        try {
            lookAndFeel.load(TestFrame.class.getResourceAsStream("/testskin.xml"), TestFrame.class);
            UIManager.setLookAndFeel(lookAndFeel);
        }
        catch (Exception e) {
           e.printStackTrace();
        }
    }

    public static class TestDialog extends JDialog{

        public TestDialog(Frame owner, boolean modal) {
            super(owner, modal);

            JComboBox petList = new JComboBox(new String[] { "Bird", "Cat", "Dog", "Rabbit", "Pig" });
            super.add(petList, BorderLayout.CENTER);

            super.setUndecorated(true);
            AWTUtilities.setWindowOpaque(this, false);
            super.pack();
        }
    }
}
以及testskin.xml:

<synth>

    <style id="backingStyle">
        <opaque value="true"/>
        <font name="Dialog" size="14"/>
    </style>
    <bind style="backingStyle" type="region" key=".*"/>

    <style id="ComboBox List Renderer">
        <opaque value="true"/>
        <state value="ENABLED">
            <color type="TEXT_FOREGROUND" value="#000000"/>
        </state>
        <state value="DISABLED">
            <color type="TEXT_FOREGROUND" value="#999999"/>
        </state>
        <state value="SELECTED">
            <color type="TEXT_FOREGROUND" value="#CC6600"/>
            <color type="TEXT_BACKGROUND" value="#FFEEDD"/>
        </state>
    </style>
    <bind style="ComboBox List Renderer" type="name" key="ComboBox.listRenderer" />

    <style id="Combo Box">
        <property key="ComboBox.showPopupOnNavigation" type="boolean" value="true"/>
        <state>
            <color value="#D8D987" type="BACKGROUND"/>
        </state>
    </style>
    <bind style="Combo Box" type="region" key="ComboBox" />

</synth>
使组合框弹出菜单“渲染”正常,此外,还向所有样式添加默认背景(在style=“backingStyle”下),例如:

一切就绪。调用'show'方法后手动调用repaint、updateUI、revalidate将使弹出菜单render ok。对于combox元素,设置自定义UI并使用扩展javax.swing.plaf.basic.BasicComboPopup的类覆盖“createPopup”方法,该类在显示时调用repaint/updateUI/revalidate,例如:

 public class ComboPopup extends BasicComboPopup {

    public ComboPopup( JComboBox combo ) {
        super(combo);
    }

    @Override
    public void show(Component invoker, int x, int y) {
        super.show(invoker, x, y);
        this.updateUI();
    }
}
将使combox菜单呈现ok。然而,我还没有为弹出窗口的submneu(JMenu)项找到解决方法,因为弹出窗口是在私有方法中创建的。这似乎是一个错误,但如果我做错了什么,有人能告诉我:)

干杯


乔纳森

我回答了一个类似的问题。希望它能对其他找到此线程的人有所帮助,我也将把我的解决方法放在这里

基本上,当你需要一个重量级的弹出窗口——一个不适合目标窗口的弹出窗口时,你的问题就会出现。解决方法是在显示任何弹出窗口后调用重新绘制。在启动应用程序时,只需调用以下代码

PopupFactory.setSharedInstance(new PopupFactory() 
{
    @Override
    public Popup getPopup(Component owner, final Component contents, int x, int y) throws IllegalArgumentException
    {
        Popup popup = super.getPopup(owner, contents, x, y);
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                contents.repaint();
            }
        });
        return popup;
    }
});

请发布演示问题的文章。包括JFrame、JComboBox和JPopup的xml文件。。。
AWTUtilities.setWindowOpaque(window, false) 
 public class ComboPopup extends BasicComboPopup {

    public ComboPopup( JComboBox combo ) {
        super(combo);
    }

    @Override
    public void show(Component invoker, int x, int y) {
        super.show(invoker, x, y);
        this.updateUI();
    }
}
PopupFactory.setSharedInstance(new PopupFactory() 
{
    @Override
    public Popup getPopup(Component owner, final Component contents, int x, int y) throws IllegalArgumentException
    {
        Popup popup = super.getPopup(owner, contents, x, y);
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                contents.repaint();
            }
        });
        return popup;
    }
});