帧添加不';无法工作到另一个java文件
test.java帧添加不';无法工作到另一个java文件,java,swing,jframe,Java,Swing,Jframe,test.java import javax.swing.JFrame; public class test { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setVisible(true); frame.setSize(600, 600); } } 我的另一个java文件test2.java import java
import javax.swing.JFrame;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
}
}
我的另一个java文件test2.java
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
frame.add(Button);
}
}
我试图调用frame到test2.java我假设您试图将
按钮添加到您在test
中创建的JFrameframe
中。要做到这一点,您需要使frame
对基本上是全局范围可见,例如:
import javax.swing.JFrame;
public class test {
public static JFrame frame;
public static void main(String[] args) {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
test2.main(args)
}
}
然后,要在test2
中添加按钮,您需要按名称访问test
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
test.frame.add(Button);
}
}
我假设您试图将按钮添加到您在测试中创建的JFrame框架
中。为此,您需要使框架
对基本上是全局范围的对象可见,例如:
import javax.swing.JFrame;
public class test {
public static JFrame frame;
public static void main(String[] args) {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
test2.main(args)
}
}
然后,要在test2
中添加按钮,您需要按名称访问test
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
test.frame.add(Button);
}
}
出现此问题的原因:
运行java应用程序时,将调用应用程序的main
函数。因此,每个应用程序实际上应该只有一个main
函数
在您的场景中,您有2个主要函数。将其视为两个不同的应用程序。发生了以下情况:
- 运行
Test
类时,应用程序正在创建一个新的JFrame
对象。差不多就是这样,就这样结束了。它不知道存在Test2
类
- 运行
Test2
类时,应用程序正在创建一个新的JButton
对象。尽管如此,您的Test2
类没有对frame变量的引用(这就是您得到错误的原因)。它甚至不知道有一个测试类
要在您的情况下解决此问题,请尝试以下方法:
Test.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
Test2.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
一种更为有效的方法:
在这里,我创建了一个Driver
类,它将Test2
和MyFrame
类连接在一起
Driver.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
MyFrame.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
Test2.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
出现此问题的原因:
运行java应用程序时,将调用应用程序的main
函数。因此,每个应用程序实际上应该只有一个main
函数
在您的场景中,您有2个主要函数。将其视为两个不同的应用程序。发生了以下情况:
- 运行
Test
类时,应用程序正在创建一个新的JFrame
对象。差不多就是这样,就这样结束了。它不知道存在Test2
类
- 运行
Test2
类时,应用程序正在创建一个新的JButton
对象。尽管如此,您的Test2
类没有对frame变量的引用(这就是您得到错误的原因)。它甚至不知道有一个测试类
要在您的情况下解决此问题,请尝试以下方法:
Test.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
Test2.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
一种更为有效的方法:
在这里,我创建了一个Driver
类,它将Test2
和MyFrame
类连接在一起
Driver.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
MyFrame.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
Test2.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
你确定它能用@Dylan?因为我可以在test.main(test.java:7)的线程“main”java.lang.NullPointerException中看到errorsException,现在我看到了我的错误:(我没有收到错误?按钮没有出现,但那只是因为我需要调用test2.main();
,我添加了它以清楚地说明问题。确保在test.main()之前调用test2.main();
)
是否调用了@Dylan?因为我可以在test.main(test.java:7)的线程“main”java.lang.NullPointerException中看到errorsException,现在我看到了我的错误:(我没有收到错误?按钮没有出现,但那只是因为我需要调用test2.main()
,我为了清晰起见添加了它。确保在调用test.main();
之前调用test2.main();
调用test
您需要像在公共类测试扩展JFrame{
中那样扩展test2
并在使用它之前创建一个实例:testframe=new test()
.1)请参阅,以了解一个我不再费心解决的问题。2)请学习常见的Java命名法(命名约定-例如,EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是一个大写字母常量
)您需要像在公共类测试扩展JFrame{
中那样扩展test
,并在使用它之前创建test2
的实例:testframe=new test()
。1)查看我不再费心解决的问题。2)请学习通用Java术语(命名约定-例如,EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是一个大写常量
)并一致地使用它。谢谢Worked@Nick没问题,很高兴我能帮上忙:)谢谢你Worked@Nick没问题,很高兴我能帮忙:)