英文:
How to display a textfields text from one class into another class' label
问题
我正在努力弄清楚如何将我在“Test1”框架的文本字段中的文本放入“Test2”框架的标签中。
请有人能帮忙,这是我的全部代码:
package Frame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test1 extends JFrame {
private JPanel contentPane;
public static JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setText("yes");
textField.setBounds(136, 98, 96, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
现在是第二个框架“Test2”的代码:
package Frame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Test2 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel jlabel = new JLabel("");
String text = Test1.textField.getText().toString();
jlabel.setBounds(169, 113, 48, 14);
contentPane.add(jlabel);
}
}
出现的错误是:
java.lang.NullPointerException
at Frame.Test2.<init>(Test2.java:44)
at Frame.Test2$1.run(Test2.java:22)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
...
我尝试过很多不同的方法来做这件事,但似乎没有任何一种方法可行。我尝试了大约8种不同的方法,但它们都不起作用,但它们似乎总是对其他人起作用。我不明白为什么。有人能帮忙吗?希望我的解释足够清楚。
提前感谢!
英文:
I'm struggling to figure out how I can put the text I have in my textField in Frame 'Test1' into my label in Frame 'Test2'.
Please could somebody help, here is all my code:
package Frame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test1 extends JFrame {
private JPanel contentPane;
public static JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setText("yes");
textField.setBounds(136, 98, 96, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
And now the code of the 2nd frame, Test2.
package Frame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Test2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel jlabel = new JLabel("");
String text = Test1.textField.getText().toString();
jlabel.setBounds(169, 113, 48, 14);
contentPane.add(jlabel);
}
}
The errors it comes up with is:
java.lang.NullPointerException
at Frame.Test2.<init>(Test2.java:44)
at Frame.Test2$1.run(Test2.java:22)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I have tried and tried loads of different methods of doing this, and nothing seems to work. I have tried about 8 different ways and none of them work, but they always seem to work for other people. I don't understand why. Can someone please help. Hopefully my explanation is sufficient.
Thanks in advance!!
答案1
得分: 0
老实说,你需要掌握各种Java概念才能理解这个异常。我将尝试澄清其中的一些。这可能会有所帮助。
-
你在Test1类中将textField定义为静态成员,但没有赋值。这实际上意味着它是空的。
-
在Test2类中,你试图访问textField的值,但它尚未被初始化,因为你在Test1类的构造函数中初始化了textField,而当创建Test1的对象时,构造函数不会被调用。
你在这里创建了一种循环冗余,可以通过以下方式解决,但它是否能解决你的业务问题还不确定:
- 在声明时初始化textField。
- 在访问textField的值之前实例化Test1类的一个对象。
英文:
To be honest, you will need to brush up various Java concepts to understand this exception. I will try to clarify a few. This can help.
-
You have textField defined as a static member of Test1 Class without a value. This essentially means that it is null.
-
In Test2 Class, you are trying to access the value of textField which has not been initialized yet as you are initializing textField in Test1 class' constructor which will not be called when an object of Test1 is created.
You have created a kind of cyclic redundancy here which can be solved in the following ways but whether it will solve your business problem or not, that is not definite:
- Initialize the textField with declaration itself.
- Instantiate an object of Test1 class before accessing textField's value.
专注分享java语言的经验与见解,让所有开发者获益!
评论