有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有JDialog的java静态输入对话框

我做了这个样本:

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class JDialogTest extends JDialog implements ActionListener{
    private boolean actionPerformed;
    private JButton okButton;
    private JTextField textField;

    private JDialogTest(String title, JFrame frame){
        super(frame, title, true);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setMinimumSize(new Dimension(200, 200));
        init();
    }

    public static String getInput(String title, JFrame frame){
        JDialogTest input = new JDialogTest(title, frame);
        input.setVisible(true);

        while(true){
            if(input.actionPerformed){
                input.setVisible(false);
                String text = input.textField.getText();
                return text;
            }
        }
    }

    private void init(){

        textField = new JTextField();
        okButton = new JButton("OK");
        okButton.addActionListener(this);

        setLayout(new GridLayout(2, 1, 5, 5));

        add(textField);
        add(okButton);
        pack();     
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        System.out.println("click");
        actionPerformed = true;     
    }

    public static void main(String[] args) {
        System.out.println(JDialogTest.getInput("Test", null));
    }



}

我通过一个返回字符串的静态方法创建新对话框。 但是while loop witch应该检测到按钮是否被按下,但它不会启动! 我知道JOptionPanes,但我不想用。我尝试改用JFrame,但如果我尝试在另一个JFrame/JDialog中初始化对话框(它不会渲染),它就不起作用


共 (3) 个答案

  1. # 1 楼答案

    同样,您需要使用模态JDialog,然后在处理完后查询它所包含的文本。由于对话框是模态的,如果您使对话框在按钮的ActionListener中不可见,则调用应用程序将知道按钮何时被按下。然后将控制权返回给调用程序。然后,调用程序可以通过调用给定的公共方法(这里我称之为getText())来查询对话框,然后返回对话框的JTextField所包含的字符串

    例如,使用你的代码

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JDialogTest extends JDialog implements ActionListener {
       // private boolean actionPerformed;
       private JButton okButton;
       private JTextField textField;
    
       private JDialogTest(String title, JFrame frame) {
          super(frame, title, true);
          setDefaultCloseOperation(HIDE_ON_CLOSE);
          setMinimumSize(new Dimension(200, 200));
          init();
       }
    
       // public static String getInput(String title, JFrame frame) {
       // JDialogTest input = new JDialogTest(title, frame);
       // input.setVisible(true);
       //
       // while (true) {
       // if (input.actionPerformed) {
       // input.setVisible(false);
       // String text = input.textField.getText();
       // return text;
       // }
       // }
       // }
    
       private void init() {
    
          textField = new JTextField();
          okButton = new JButton("OK");
          okButton.addActionListener(this);
    
          // UGLY layout
          setLayout(new GridLayout(2, 1, 5, 5));
    
          add(textField);
          add(okButton);
          pack();
       }
    
       // I've added this method to allow outside methods to get text
       public String getText() {
          return textField.getText();
       }
    
       @Override
       public void actionPerformed(ActionEvent evt) {
          System.out.println("click");
          // actionPerformed = true;
          setVisible(false);
       }
    
       private static void createAndShowGui() {
          final JTextField textField = new JTextField(20);
          JFrame frame = new JFrame("Test JFrame");
          final JDialogTest jDialogTest = new JDialogTest("Dialog", frame);
    
          JButton button = new JButton(
                new AbstractAction("Press Me to Show Dialog") {
                   @Override
                   public void actionPerformed(ActionEvent arg0) {
                      jDialogTest.setVisible(true);  // code is frozen here
                                              // until the dialog is no longer visible
    
    
                      // when code flow reaches here, we know that the dialog
                      // is no longer visible and 
                      // we now can query our JDialog to get its text
                      textField.setText(jDialogTest.getText());
                   }
                });
    
          textField.setFocusable(false);
          JPanel panel = new JPanel();
          panel.add(button);
          panel.add(textField);
    
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    编辑

    如果希望以静态方式显示对话框,请将getInput方法更改为:

    public static String getInput(String title, JFrame frame) {
      JDialogTest input = new JDialogTest(title, frame);
    
      input.setVisible(true);
    
      return input.getText();
    }
    

    呼叫代码的更改:

      final JTextField textField = new JTextField(20);
      final JFrame frame = new JFrame("Test JFrame");
    
      JButton button = new JButton(
            new AbstractAction("Press Me to Show Dialog") {
               @Override
               public void actionPerformed(ActionEvent arg0) {                  
                  String result = JDialogTest.getInput("Get Input", frame);
                  textField.setText(result);
               }
            });
    

    编辑2

    请注意,JOptionPane的效果非常好:

    String result = JOptionPane.showInputDialog(frame, "Please enter input:", 
          "Get Input", JOptionPane.PLAIN_MESSAGE);
    

    编辑3
    你问:

    The solution in the edit seems to work but what method should I use in the action listener to close the JDialog and return the text?

    对话框按钮的ActionListener将通过调用setVisible(false)dispose()使对话框不可见。请参阅上面的我的代码或nachokk的代码以获取示例

  2. # 2 楼答案

    这里有一个可能的解决方案。特别注意JDialogTest构造函数getInput()actionPerformed()的更改

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JDialogTest extends JDialog implements ActionListener{
        private JButton okButton;
        private JTextField textField;
    
        private JDialogTest(String title, JFrame frame){
            super(frame, title, true);
            this.setModal(true);                   // Set the dialog as modal
            setDefaultCloseOperation(HIDE_ON_CLOSE);
            setMinimumSize(new Dimension(200, 200));
            init();
        }
    
        public static String getInput(String title, JFrame frame){
            JDialogTest input = new JDialogTest(title, frame);
            input.setVisible(true);
            return input.textField.getText();       // If this is executed, the dialog box has closed
        }
    
        private void init(){
    
            textField = new JTextField();
            okButton = new JButton("OK");
            okButton.addActionListener(this);
    
            setLayout(new GridLayout(2, 1, 5, 5));
    
            add(textField);
            add(okButton);
            pack();     
        }
    
        @Override
        public void actionPerformed(ActionEvent evt) {
            System.out.println("click");
            setVisible(false);             // Close the modal dialog box  
        }
    
        public static void main(String[] args) {
            System.out.println(JDialogTest.getInput("Test", null));
        }
    }
    
  3. # 3 楼答案

    while(true)与gui在同一线程中运行时,将冻结您的视图,这不是您使用侦听器的正确方式。由于对话框是模态的,因此该对话框具有流控制

    在你的例子中,看看这个基于SSCCE的,有一些变化

    例如:

    public class JDialogTest {
    
        private JDialog dialog;
        private JTextField textField;
    
    private JDialogTest (String title, JFrame frame){
        dialog = new JDialog(frame, title, true);
        dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        dialog.setMinimumSize(new Dimension(200, 200));
        init();
    }
    
    public void setVisible(Boolean flag){
        dialog.setVisible(flag);
    }
    
    public static String getInput(String title, JFrame frame){
        JDialogTest input = new JDialogTest (title, frame);
        input.setVisible(true);
        String text = input.textField.getText();
        return text;
    }
    
    private void init(){
    
        textField = new JTextField();
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.dispose();
                }
    
        });
    
        dialog.setLayout(new GridLayout(2, 1, 5, 5));
    
        dialog.add(textField);
        dialog.add(okButton);
        dialog.pack();     
    }
    
    public static void main(String args []){
        String s = getInput("Dialog",null);
        System.out.println(s);
    }
    
    
    }