如何将变量从 jtextfield 传递到另一个 JFrame/类中的另一个 jtextfield?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您实例化一个新的 ClassA 实例(顺便说一句,语法是 new ClassA()),那么您将拥有...一个新实例,其中包含另一个 jtextfield1,而不是现有 ClassA 实例中的 jtextfield1 。您需要将对现有 ClassA 实例的引用传递给 ClassB 实例:
当构造 ClassB 时:
您似乎没有掌握类和对象的基本 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:
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
你要求一个简单的例子,你就会得到一个。我将如何将这个示例与您的代码相匹配由您决定,这应该不会太困难。
我希望我没有在其中犯下太多拼写错误,因为我没有尝试运行它,也没有使用我的 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.
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