如何将变量从 jtextfield 传递到另一个 JFrame/类中的另一个 jtextfield?

发布于 2025-01-03 13:08:31 字数 4386 浏览 0 评论 0原文

我的 JTextField 遇到问题。我不知道如何将变量从 JTextField (位于 JFrame A 中)传递到另一个 JTextField (位于 JFrame B 中)。 我尝试这样做,但它没有执行任何操作,即它没有收到任何运行时/编译错误,也没有收到文本。

我尝试在 ClassB 中执行此操作:

ClassA a = new ClassA();
String text = a.jtextfield1.getText();

但它不起作用!

你能帮我一个简单的例子吗?我做错了什么?

ps: 我正在使用 netbeans

[编辑 --] 这是 ClassA 的代码:

public class ClassA extends javax.swing.JFrame {

public ClassA() {
    initComponents();
}

public void initComponents() {

    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTextField1.setText("Some text blah blah");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(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()
            .addGap(33, 33, 33)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(124, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(47, 47, 47)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(86, Short.MAX_VALUE))
    );

    pack();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

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

public javax.swing.JTextField jTextField1;

}

这是 ClassB 的代码:

public class ClassB extends javax.swing.JFrame {

public ClassB() {
    initComponents();
}

  public  ClassA a = new ClassA();


public void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    getText = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    getText.setText("GetText");
    getText.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            getTextActionPerformed(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()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(50, 50, 50)
                    .addComponent(getText)))
            .addContainerGap(143, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(50, 50, 50)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(26, 26, 26)
            .addComponent(getText)
            .addContainerGap(49, Short.MAX_VALUE))
    );

    pack();
}

public void getTextActionPerformed(java.awt.event.ActionEvent evt) {
    a.jTextField1.getText(); //this doesn't work. How can I it makes work?
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

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

public javax.swing.JButton getText;
public javax.swing.JTextField jTextField1;

}

提前致谢。

I've got a problem with JTextField. I don't know how to pass a variable from a JTextField (situated in JFrame A) to another JTextField (situated in JFrame B).
I tried to do that, but it doesn't do anything, i.e. it doesn't get any runtime/compilation error nor it receives the text.

I tried to do this in ClassB:

ClassA a = new ClassA();
String text = a.jtextfield1.getText();

but it doesn't work!!

Could you help me, may with a simple example? What did I do wrong?

p.s.: i'm using netbeans

[edit --] That's the code of ClassA:

public class ClassA extends javax.swing.JFrame {

public ClassA() {
    initComponents();
}

public void initComponents() {

    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTextField1.setText("Some text blah blah");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(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()
            .addGap(33, 33, 33)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(124, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(47, 47, 47)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(86, Short.MAX_VALUE))
    );

    pack();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

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

public javax.swing.JTextField jTextField1;

}

Here's the code of ClassB:

public class ClassB extends javax.swing.JFrame {

public ClassB() {
    initComponents();
}

  public  ClassA a = new ClassA();


public void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    getText = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    getText.setText("GetText");
    getText.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            getTextActionPerformed(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()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(50, 50, 50)
                    .addComponent(getText)))
            .addContainerGap(143, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(50, 50, 50)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(26, 26, 26)
            .addComponent(getText)
            .addContainerGap(49, Short.MAX_VALUE))
    );

    pack();
}

public void getTextActionPerformed(java.awt.event.ActionEvent evt) {
    a.jTextField1.getText(); //this doesn't work. How can I it makes work?
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

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

public javax.swing.JButton getText;
public javax.swing.JTextField jTextField1;

}

Thanks in advance.

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

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

发布评论

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

评论(2

初懵 2025-01-10 13:08:31

如果您实例化一个新的 ClassA 实例(顺便说一句,语法是 new ClassA()),那么您将拥有...一个新实例,其中包含另一个 jtextfield1,而不是现有 ClassA 实例中的 jtextfield1 。您需要将对现有 ClassA 实例的引用传递给 ClassB 实例:

当构造 ClassB 时:

ClassB theClassB = new ClassB(theClassA);

您似乎没有掌握类和对象的基本 OO 概念,甚至没有掌握 Java 的语法。我建议现在不要使用 Swing,先学习基础知识。 Swing 很难,对于不理解这些概念的开发人员来说太难了。

阅读 Java 教程 的基础知识和 OO 课程

If you instantiate a new ClassA instance (and, BTW, the syntax is new ClassA()), then you will have... a new instance, with another jtextfield1 than the one in the existing ClassA instance. You need to pass a reference to the existing ClassA instance to the ClassB instance:

When ClassB is constructed:

ClassB theClassB = new ClassB(theClassA);

You don't seem to master the basic OO concepts of classes and objects, and not even the syntax of Java. I would advise not using Swing now, and learning the basics first. Swing is hard, much too hard for a developer which doesn't understand these concepts.

Read the basics and OO lessons of the Java tutorial

万人眼中万个我 2025-01-10 13:08:31

你要求一个简单的例子,你就会得到一个。我将如何将这个示例与您的代码相匹配由您决定,这应该不会太困难。

public class PanelWithTextField extends JPanel{
  private JTextField textField = new JTextField();
  public JTextField getTextField(){ return textField; };
}

public static void main( String[] args ){
  EventQueue.invokeLater(){ new Runnable(){
    public void run(){
      //create a first panel
      PanelWithTextField panel = new PanelWithTextField();
      panel.getTextField().setText( "Some text" );
      //create a second panel
      PanelWithTextField anotherPanel = new PanelWithTextField();
      //copy the text from the first panel's textfield to the second panel's textfield
      anotherPanel.getTextField().setText( panel.getTextField().getText() );
    }
  }
}

我希望我没有在其中犯下太多拼写错误,因为我没有尝试运行它,也没有使用我的 IDE 来编写这段代码。

基本上,您需要在第二个面板中引用第一个面板(或在访问该文本字段的代码的任何部分)。

正如其他人已经建议的那样。在开始使用 Swing 和 UI 之前,您应该首先确保了解基本的 OO 概念和基本的 Java 语法

You ask for a simple example, you will get one. I leave it up to you how to match this example on your code, which shouldn't be too difficult.

public class PanelWithTextField extends JPanel{
  private JTextField textField = new JTextField();
  public JTextField getTextField(){ return textField; };
}

public static void main( String[] args ){
  EventQueue.invokeLater(){ new Runnable(){
    public void run(){
      //create a first panel
      PanelWithTextField panel = new PanelWithTextField();
      panel.getTextField().setText( "Some text" );
      //create a second panel
      PanelWithTextField anotherPanel = new PanelWithTextField();
      //copy the text from the first panel's textfield to the second panel's textfield
      anotherPanel.getTextField().setText( panel.getTextField().getText() );
    }
  }
}

I hope I did not make too much typo's in it, as I did not try to run it nor did I used my IDE to write this code.

Basically you will need a reference to your first panel in your second panel (or in any part of the code where you which to access that textfield).

And as already suggested by others. You should start by making sure you understand the basic OO concepts and basic Java syntax before you start messing around with Swing and UI's

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