2013-12-15 155 views
0

在程序中使用多個showInputDialogs。 當這些輸入中的一個彈出時,它會凍結背景中的所有其他窗口,直到它接收到輸入,是否有辦法使其不凍結其他窗口?showInputDialog凍結其他窗口

回答

3

如果通過「凍結」您的意思是用戶無法訪問其他窗口,那麼關鍵是使新對話框成爲非模態對話框。您可以從JOptionPane中提取JDialog,然後選擇以非模式方式顯示它。 JOptionPane API將告訴你如何。搜索「直接使用」部分:

編輯:安德魯州也是! 1+


與代碼玩....

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

import javax.swing.*; 

public class Foo { 

    public static void main(String[] args) { 
     final JTextField textfield = new JTextField(10); 
     textfield.setFocusable(false); 
     final JPanel panel = new JPanel(); 
     panel.add(textfield); 

     panel.add(new JButton(new AbstractAction("Push Me") { 

     private JOptionPane optionPane; 
     private JDialog dialog; 
     private JTextField optionTextField = new JTextField(10); 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      if (dialog == null) { 
       JPanel optionPanel = new JPanel(new BorderLayout()); 
       optionPanel.add(new JLabel("Enter some stuff"), 
        BorderLayout.PAGE_START); 
       optionPanel.add(optionTextField, BorderLayout.CENTER); 
       optionPane = new JOptionPane(optionPanel, 
        JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 
       dialog = optionPane.createDialog(panel, "Get More Info"); 
       dialog.setModal(false); 
       dialog.addComponentListener(new ComponentAdapter() { 
        @Override 
        public void componentHidden(ComponentEvent arg0) { 
        Integer value = (Integer) optionPane.getValue(); 
        if (value == null) { 
         return; 
        } 
        if (value == JOptionPane.OK_OPTION) { 
         textfield.setText(optionTextField.getText()); 
        } 
        } 
       }); 
      } 
      dialog.setVisible(true); 
     } 
     })); 

     JFrame frame = new JFrame("Frame"); 
     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+1

*「搜索部分標題爲」直接使用:「」哦,我沒有想到這一點。 +1它*很難重新創建'JOptionPane'的功能.. –

+0

我檢查了它,但我沒有看到它如何與inputdialogs一起工作?我輸入tablename = JOptionPane.showInputDialog(「輸入表名*(使用大寫字母)」)的樣式inputdialogs; – Looptech