如何从另一个 Java 类调用 JFrame [Netbeans]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33727974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:03:06  来源:igfitidea点击:

How to call JFrame from another Java class [Netbeans]

javaswingnetbeansjframe

提问by mclaren12c

I know it has been posted before, but I was not able to achieve any results so I am asking for help.

我知道它之前已经发布过,但我无法取得任何结果,所以我寻求帮助。

I am trying to call my JFrameclass to my main class that will be setting up the Comm port. The frame is created using the design in the Netbeans software. My question is why is my main Java file not able to open the frame that is created in another class?

我正在尝试将我的JFrame班级称为将设置通信端口的主班级。该框架是使用 Netbeans 软件中的设计创建的。 我的问题是为什么我的主 Java 文件无法打开在另一个类中创建的框架?

Below are the shortened codes:

以下是缩短的代码:

The main class is SerialTest

主要类是 SerialTest

package javaapplication1;

import javaapplication1.RCDA_JFrame;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class SerialTest extends JFrame implements SerialPortEventListener {

    public static void main(String[] args) throws Exception {

                JFrame RCDA_JFrame = new JFrame();
                RCDA_JFrame.setVisible(true);
    }
}

And my GUI class is RCDA_JFrame

我的 GUI 类是 RCDA_JFrame

package javaapplication1;

import java.io.IOException;
//import java.io.OutputStream;
import javaapplication1.SerialTest;

public class RCDA_JFrame extends javax.swing.JFrame {
    //private OutputStream SerialOut;
    public RCDA_JFrame() {
        initComponents();
    }

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RCDA_JFrame().setVisible(true);
            }
        });
    }
}

回答by Nerdizzle

Firstly, only one class (the GUI class) should extend JFrame. However, this is not causing the problem. Secondly, neither class is ever instantiated even though both extend JFrame(which generally implies that one should be instantiated). Thirdly, you should only have one mainmethod throughout an entire application. Fourthly, not entirely sure what the main()method within the GUI class is doing -- why does it create an RCDA_JFrame (I though the Serial class was supposed to do that, based on the title question), and why does it do this within a separate thread? Finally, make sure that you are setting the JFrame's size (Though you may already be doing this within the initComponentsmethod.)

首先,只有一个类(GUI 类)应该扩展 JFrame。但是,这不会导致问题。其次,即使两个类都扩展了JFrame(这通常意味着应该实例化一个),但两个类都不会被实例化。第三,main在整个应用程序中应该只有一种方法。第四,不完全确定main()GUI 类中的方法在做什么——为什么它创建一个 RCDA_JFrame(我认为 Serial 类应该这样做,基于标题问题),以及为什么它在单独的线?最后,确保您正在设置 JFrame 的大小(尽管您可能已经在initComponents方法中这样做了。)

Here's some basic code that will allow you to create and access a custom JFrame from another class:

下面是一些基本代码,允许您从另一个类创建和访问自定义 JFrame:

Start class (This is the main class)

开始班(这是主班)

public class Start{
    private static CustomJFrame myFrame;
    public static void main(String[] args){
        myFrame = new CustomJFrame();
        //you can edit myFrame's properties here.
    }
}

CustomJFrame class (the JFrame class)

CustomJFrame 类(JFrame 类)

import javax.swing.JFrame;


public class CustomJFrame extends JFrame{
    public CustomJFrame(){
        //set its size in px.
        setSize(200,200);
        //center it on screen.
        setLocationRelativeTo(null);
        //give it a title.
        setTitle("My JFrame");
        //set the close operation.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //make it visible.
        setVisible(true);
    }
}

Tested and works just fine.

经测试,工作正常。

回答by Jonah

I recommend doing something like this. No need for two mainmethods.

我建议做这样的事情。不需要两种main方法。

import javax.swing.*;

public class SerialTest{

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(RCDA_JFrame::new);
    }
}

And this

还有这个

public class RCDA_JFrame extends JFrame {

    public RCDA_JFrame() {
        initComponents();
    }

    public void initComponents(){
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }
}