2014-07-07 49 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