【问题标题】:Passing data from JDialog to frame by customize button通过自定义按钮将数据从 JDialog 传递到框架
【发布时间】:2019-06-22 10:00:05
【问题描述】:

我是 jdialog 的新手,我一直在搜索,但似乎找不到使用自定义按钮的解决方案。我发现的唯一解决方案是使用他们的内置输入 JDialog。但这并不能解决我的问题。

我正在尝试将数据(在单击保存后从对话框 texfield“hello”传递到父框架 textField)但无法这样做。

有人遇到过同样的问题吗? 有什么帮助吗?

public class dataparse {

  String result;
  String inputValue;

public void mainFrame() {
    JFrame frame = new JFrame(" Parent Frame ");
    JPanel center = new JPanel();
    JButton enter = new JButton("Enter");
    // JLabel data = new JLabel("data is...");
    JTextField text = new JTextField();

    frame.setSize(400, 400);
    center.setLayout(new GridLayout(0, 1));
    center.add(text);
    center.add(enter);
    frame.add(center, BorderLayout.CENTER);

    enter.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            confirmDialog();
            text.setText(inputValue);

        }
    });

    frame.setVisible(true);

}


    private void confirmDialog(){
        JTextField output = new JTextField("Hellloooo");
        JButton save = new JButton("Save");


          JDialog customDialog = new JDialog();
          Container pane = customDialog.getContentPane();
          pane.setLayout(new GridLayout(0,1));

          pane.add(new JLabel("Startubg"));
          pane.add(output);
          pane.add(save);

          customDialog.setSize(300,400);

          customDialog.add(output);
          customDialog.add(save);

          customDialog.setVisible(true);

          save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                result = output.getText();
            }
        });

    }

提前谢谢你:)

例如,我有 2 个类,我希望第二个类从第一个类调用 dialog.dispose()。但我无法调用该方法,任何想法

ma​​in.java

public JDialog dialogBox(){

    //JDialog dialog = new JDialog(frame, "Date picker", true);
    dialogButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            dialog = new JDialog(frame,"Date picker " , true);
            DatePicker_Demo picker = new DatePicker_Demo();
            dialog.setSize(500, 300);
            dialog.setLayout(new GridLayout(0,2));
            dialog.add(new DatePicker_Demo().addBtn());
            dialog.pack();
            dialog.setVisible(true);
        }
    });
    return dialog;
}

second.java

public JButton addBtn(){
    JButton btn = new AddBills().exitBtn();
    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            new AddBills().dialogBox().dispose();
        }
    });

    return btn;
}

【问题讨论】:

  • 在对话框中,您将文本字段的值设置为“结果”变量。但是在JFrame 中,您在text.setText(inputValue); 中使用变量inputValue。这是一个错误吗?
  • 请参阅stackoverflow.com/help/someone-answers。您发布了 38 个问题并接受了一个。你得到的所有〜50个答案都没有帮助吗? Please accept 并为您认为有帮助的答案投票。

标签: java swing jbutton jdialog


【解决方案1】:

将 text.setText 部分放在“保存”按钮的 actionListener 中有什么问题?

例如

public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
       result = output.getText();
       text.setText(result);
 }

【讨论】:

    【解决方案2】:
    public class Window extends JFrame {
    
    private JPanel contentPane;
    private JTextField txtField;
    private JButton btnSave;
    private static Window frame2; // making it static so i can work on it from main()
    private static int num = 500;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window frame1 = new Window();
                    frame2 = new Window();
                    frame1.setVisible(true);
                    frame2.setVisible(true);
                    frame2.txtField.setText("");// clear the text box of window 2
                    frame2.setTitle("window 2");
                    frame2.btnSave.setVisible(false); // won't see button on window 2
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Window() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100 + num, 100, 506, 514);
        setTitle("window1");
        num += num; // so you can see both windows
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    
        txtField = new JTextField();
        txtField.setText("Hello");
        txtField.setBounds(39, 151, 376, 117);
        contentPane.add(txtField);
        txtField.setColumns(10);
    
        btnSave = new JButton("save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String text = txtField.getText(); // get the text after save button was clicked.
                txtField.setText("");// clear textfield of window 1.
                frame2.txtField.setText(text); // set the text on textField of window 2
                setVisible(true); // update
                frame2.setVisible(true); // update
            }
        });
        btnSave.setBounds(169, 302, 115, 29);
        contentPane.add(btnSave);
        }
    }
    

    当然你可以让它更加面向对象,但我想以一种形式保持简单,这样你就可以看到所有内容。

    【讨论】:

      【解决方案3】:

      你可以像下面的例子那样做。我认为您的代码中缺少的重要行是setModal(true);

      如果没有setModal(true); 行,执行将直接从confirmDialog(); 转到text.setText(inputValue);,而无需等待用户在对话框中输入文本。

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      
      public class InputDialogExample {
      
        public static void main(String[] args) {
      
          JFrame frame = new JFrame("Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
          JLabel label = new JLabel();
      
          JButton openDialogButton = new JButton("Open Dialog");
          openDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              CustomDialog dialog = new CustomDialog(frame);
              dialog.setVisible(true);
              label.setText(dialog.getValue());
            }
          });
      
          frame.getContentPane().setLayout(new GridLayout(2, 1));
          frame.getContentPane().add(label);
          frame.getContentPane().add(openDialogButton);
          frame.setBounds(300, 200, 400, 300);
          frame.setVisible(true);
        }
      }
      
      class CustomDialog extends JDialog {
      
        private String value;
      
        CustomDialog(Frame owner) {
      
          super(owner, "Dialog");
      
          setModal(true); // This is the important line
      
          JTextField textField = new JTextField();
      
          JButton okButton = new JButton("OK");
          okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              value = textField.getText();
              setVisible(false);
            }
          });
      
          getContentPane().setLayout(new GridLayout(2, 1));
          getContentPane().add(textField);
          getContentPane().add(okButton);
          setBounds(500, 400, 300, 200);
        }
      
        String getValue() {
          return value;
        }
      }
      

      【讨论】:

      • 例如,在我的主类 first.java 中,我编写了一个显示 Jdialog 的方法,但我正在从 second.java 中的另一个类访问 Jbutton 以在我的第一个对话框中添加班级。我可以从 second.java 类中杀死 JDialog 吗?
      • @FaridAvesko,这个 first.java 和 second.java 代码与问题中的代码不同吗?如果是这样,请在问题中发布该实际代码。正如我在回答中提到的,我认为 dialog.setModal(true); 是您的代码中缺少的部分。如果没有这一行,程序执行将不会等到用户在对话框中输入文本。
      • 是的,我已经更新了问题,我理解你写的部分。但现在如果我在不同的类中创建了一个新按钮,我似乎无法调用该函数
      • @FaridAvesko,您必须编写更“面向对象”的代码。你必须了解执行流程。当用户点击 second.java 中的btn 时,会创建一个AddBills 对象。然后,dialogBox() 方法被调用。在dialogBox() 方法中,ActionListener 被添加到dialogButton。然后返回dialog。 (请注意,actionPerformed() 中的代码不会执行。该代码仅在单击 dialogButton 时才会执行。因此,dialog 可能是 null。)然后,在那个返回的 dialog 对象上(可能是null), dispose() 被调用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2016-05-05
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多