亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過自定義按鈕將數據從 JDialog 傳遞到框架

通過自定義按鈕將數據從 JDialog 傳遞到框架

萬千封印 2022-05-25 10:35:58
我是 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();            }        });    }先感謝您 :)
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

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);

    }

}

當然,你可以讓它更加面向對象,但我想以一種形式保持簡單,這樣你就可以看到這一切。


查看完整回答
反對 回復 2022-05-25
?
白板的微信

TA貢獻1883條經驗 獲得超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;

  }

}


查看完整回答
反對 回復 2022-05-25
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

將 text.setText 部分放入“保存”按鈕的 actionListener 有什么問題?


例如


public void actionPerformed(ActionEvent arg0) {

        // TODO Auto-generated method stub

       result = output.getText();

       text.setText(result);

 }


查看完整回答
反對 回復 2022-05-25
  • 3 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號