将类中用户输入的 JTextField 更新/获取/同步到另一个类的 JTextField

发布于 2024-11-14 19:07:03 字数 8201 浏览 3 评论 0原文

public class FormattedName extends javax.swing.JFrame {

/** Creates new form FormattedName */
public FormattedName() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    Formatted = new javax.swing.JTextField();
    NameOpt = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    Formatted.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            FormattedActionPerformed(evt);
        }
    });

    NameOpt.setText("Name");
    NameOpt.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            NameOptActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(NameOpt)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(Formatted, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(NameOpt)
                .addComponent(Formatted, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(21, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void FormattedActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

private void NameOptActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Opt option = new Opt();

}


/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new FormattedName().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JTextField Formatted;
private javax.swing.JButton NameOpt;
// End of variables declaration

}

和另一个类

public class Opt extends javax.swing.JFrame {

/** Creates new form Opt */
public Opt() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    firstname = new javax.swing.JLabel();
    lastname = new javax.swing.JLabel();
    middleName = new javax.swing.JLabel();
    first_name = new javax.swing.JTextField();
    last_name = new javax.swing.JTextField();
    middlename = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    firstname.setText("FirstName");

    lastname.setText("LastName");

    middleName.setText("MiddleName");

    first_name.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            first_nameActionPerformed(evt);
        }
    });

    last_name.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            last_nameActionPerformed(evt);
        }
    });

    jButton1.setText("OK");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton1)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(middleName)
                        .addComponent(lastname)
                        .addComponent(firstname))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(first_name, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addComponent(last_name)
                        .addComponent(middlename))))
            .addContainerGap(22, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(firstname)
                .addComponent(first_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lastname)
                .addComponent(last_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(middleName)
                .addComponent(middlename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void first_nameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

private void last_nameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
  String firstname =  first_name.getText();
    String lastname = last_name.getText();
    outputName = firstname + lastname;
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Opt().setVisible(true);
        }
    });
}

private String outputName;
// Variables declaration - do not modify
private javax.swing.JTextField first_name;
private javax.swing.JLabel firstname;
private javax.swing.JButton jButton1;
private javax.swing.JTextField last_name;
private javax.swing.JLabel lastname;
private javax.swing.JLabel middleName;
private javax.swing.JTextField middlename;
// End of variables declaration

}

如何将类 Opt 文本字段中输入的输入同步到类 FormattedName 的文本字段(带或不带 OK 按钮事件) 。

尝试使用 get() 但不起作用。

public class FormattedName extends javax.swing.JFrame {

/** Creates new form FormattedName */
public FormattedName() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    Formatted = new javax.swing.JTextField();
    NameOpt = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    Formatted.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            FormattedActionPerformed(evt);
        }
    });

    NameOpt.setText("Name");
    NameOpt.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            NameOptActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(NameOpt)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(Formatted, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(NameOpt)
                .addComponent(Formatted, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(21, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void FormattedActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

private void NameOptActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Opt option = new Opt();

}


/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new FormattedName().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JTextField Formatted;
private javax.swing.JButton NameOpt;
// End of variables declaration

}

and another class

public class Opt extends javax.swing.JFrame {

/** Creates new form Opt */
public Opt() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    firstname = new javax.swing.JLabel();
    lastname = new javax.swing.JLabel();
    middleName = new javax.swing.JLabel();
    first_name = new javax.swing.JTextField();
    last_name = new javax.swing.JTextField();
    middlename = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    firstname.setText("FirstName");

    lastname.setText("LastName");

    middleName.setText("MiddleName");

    first_name.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            first_nameActionPerformed(evt);
        }
    });

    last_name.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            last_nameActionPerformed(evt);
        }
    });

    jButton1.setText("OK");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton1)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(middleName)
                        .addComponent(lastname)
                        .addComponent(firstname))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(first_name, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addComponent(last_name)
                        .addComponent(middlename))))
            .addContainerGap(22, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(firstname)
                .addComponent(first_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lastname)
                .addComponent(last_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(middleName)
                .addComponent(middlename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void first_nameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

private void last_nameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
  String firstname =  first_name.getText();
    String lastname = last_name.getText();
    outputName = firstname + lastname;
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Opt().setVisible(true);
        }
    });
}

private String outputName;
// Variables declaration - do not modify
private javax.swing.JTextField first_name;
private javax.swing.JLabel firstname;
private javax.swing.JButton jButton1;
private javax.swing.JTextField last_name;
private javax.swing.JLabel lastname;
private javax.swing.JLabel middleName;
private javax.swing.JTextField middlename;
// End of variables declaration

}

how do i sync the input entered in class Opt textfield to textfield of class FormattedName w/ or w/o OK button event.

tried using get() but not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-11-21 19:07:03

在 Opt 中创建一个描述文本的 getter 方法,类似于

public String getFullName(){   
    return first_name.getText() + middelname.getText() + last_name.getText();  
}

您可以在名称中使用它OptActionPerformed()

private void nameOptActionPerformed(java.awt.event.ActionEvent evt) {
    Opt option = new Opt();
    option.setVisible(true);

}

您现在有两种选择,

  1. 使 Opt 类成为模态 JDialog 而不是 JFrame,或者
  2. 将 ok 按钮的事件处理公开给FormattedName 类。

我个人会选择模式对话框。

Create a getter method in Opt that describes the text, something along the lines of

public String getFullName(){   
    return first_name.getText() + middelname.getText() + last_name.getText();  
}

You can use this in your nameOptActionPerformed()

private void nameOptActionPerformed(java.awt.event.ActionEvent evt) {
    Opt option = new Opt();
    option.setVisible(true);

}

You now have two choices

  1. make the Opt class a modal JDialog instead of JFrame or
  2. expose the event handling of the ok button to the FormattedName class.

Id personally go with the modal dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文