Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
帧添加不';无法工作到另一个java文件_Java_Swing_Jframe - Fatal编程技术网

帧添加不';无法工作到另一个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

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 javax.swing.JButton;

public class test2 {

    public static void main(String[] args) {
        JButton Button = new JButton();
        frame.add(Button);

    }

}

我试图调用frame到test2.java

我假设您试图将
按钮添加到您在
test
中创建的JFrame
frame
中。要做到这一点,您需要使
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没问题,很高兴我能帮忙:)