2014-07-07 51 views
3

我创建了一个JPopupmenu并添加了一个JTextField。当我使用金属或灵气时,一切都可以。问题是当我将LookAndFeel切换到Windows时。我不能按右ALT,因为如果我按下这个键,JPopupmenu会隐藏。LookAndFeel行为差异

我可以使用正确的ALT在Windows LookAndFeel中编写国家标志吗?

import javax.swing.*; 
import java.awt.event.*; 

public class Popup extends JFrame { 

    JPopupMenu popup; 
    JPanel panel; 
    JTextField field; 

    public Popup(){ 
     setSize(500,400); 
     try { 
      //UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (ClassNotFoundException | InstantiationException 
       | IllegalAccessException | UnsupportedLookAndFeelException e1) { 
      e1.printStackTrace(); 
     } 
     SwingUtilities.updateComponentTreeUI(this); 

     popup = new JPopupMenu(); 
     field = new JTextField(10); 
     popup.add(field); 
     JButton button = new JButton("Options"); 
     button.addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent e) { 
       popup.show(e.getComponent(), e.getX(), e.getY()); 
      } 
     });   

     panel = new JPanel(); 
     panel.add(button); 
     add(panel); 
    } 

    public static void main(String[] args){ 
     Popup pop = new Popup(); 
     pop.setVisible(true); 
    } 

} 
+0

是的,这正是我所期望的。你试图达到什么目标?你可以考虑使用JWindow或未装饰的JFrame – MadProgrammer

+0

看起来像这样。 [链接](http://oi58.tinypic.com/b5lceu.jpg) 我想筛选JList中的项目,最后选择一个 – Zbijlud

+2

我会考虑使用未装饰的JFrame,但必须使用WindowFocusListener确定自动隐藏窗口。问题是,许多操作系统都希望显示字段和菜单的快捷键 – MadProgrammer

回答

4

JPopupMenu有一个非常具体的一套操作要求,是的,他们不看之间的变化和感觉,这就是那种点。

你可以做的是创建你自己的弹出使用未装饰的JFrame。这里的诀窍是模仿尽可能多的弹出窗口,例如,当另一个组件获得焦点时自动关闭,使用退出键来关闭弹出窗口的能力......等等...

这只是一个快速举例来提供一个概念证明,我个人还为转义键添加了一个键绑定,某种监听器接口允许搜索窗格请求该弹出框被解散,并且能够自动聚焦某些组件窗口已变得可见,但是这只是我...

Popup

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowFocusListener; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestPopup { 

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

    public TestPopup() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JButton show; 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      show = new JButton("..."); 
      show.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        PopupWindow window = new PopupWindow(); 
        window.show(show, 0, show.getHeight()); 
       } 
      }); 
      add(show); 
     } 

    } 

    public class SearchPane extends JPanel { 

     private JList list; 
     private JTextField search; 

     public SearchPane() { 
      setLayout(new BorderLayout()); 
      list = new JList(); 
      list.setPrototypeCellValue("This is just a test"); 
      list.setVisibleRowCount(20); 
      search = new JTextField(10); 
      add(new JScrollPane(list)); 
      add(search, BorderLayout.SOUTH); 
     } 

    } 

    public class PopupWindow extends JFrame { 

     private SearchPane searchPane; 

     public PopupWindow() { 
      setUndecorated(true); 
      setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
      addWindowFocusListener(new WindowFocusListener() { 

       @Override 
       public void windowGainedFocus(WindowEvent e) { 
       } 

       @Override 
       public void windowLostFocus(WindowEvent e) { 
        dispose(); 
       } 
      }); 
      searchPane = new SearchPane(); 
      add(searchPane); 
      pack(); 
     } 

     public void show(JComponent parent, int x, int y) { 
      Point point = new Point(x, y); 
      SwingUtilities.convertPointToScreen(point, parent); 
      setLocation(point); 
      setVisible(true); 
     } 

    } 

} 
+0

非常感谢!当我看到你的帖子时,我正在研究它。 – Zbijlud

+0

希望它给你一些想法;) – MadProgrammer