简体   繁体   English

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

[英]How to call JFrame from another Java class [Netbeans]

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 JFrame class to my main class that will be setting up the Comm port. 我试图将我的JFrame类调用到将设置Comm端口的主类中。 The frame is created using the design in the Netbeans software. 框架是使用Netbeans软件中的设计创建的。 My question is why is my main Java file not able to open the frame that is created in another class? 我的问题是为什么我的主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);
            }
        });
    }
}

Firstly, only one class (the GUI class) should extend JFrame. 首先,只有一个类(GUI类)应该扩展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). 其次,即使两个类都扩展了JFrame ,这两个类都不曾被实例化(这通常意味着应该实例化一个)。 Thirdly, you should only have one main method throughout an entire application. 第三,整个应用程序中只有一种main方法。 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? 第四,不能完全确定GUI类中的main()方法在做什么-为什么创建一个RCDA_JFrame(尽管我基于标题问题,虽然应该是Serial类这样做),但为什么要这样做呢?在单独的线程中? Finally, make sure that you are setting the JFrame's size (Though you may already be doing this within the initComponents method.) 最后,请确保您正在设置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. 经过测试,工作正常。

I recommend doing something like this. 我建议做这样的事情。 No need for two main methods. 不需要两种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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM