在秒内生成 GUI class

Generate GUI in second class

我对 Java WindowBuilder 经验不多。你能帮我么? 我将所有 GUI 放在第二个 class 中,但在我的主 class 中使用我需要的东西。我所做的没有打印任何东西。

主要Class:

import java.awt.EventQueue;
public class MainClass {

static GUIClass gui;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {

                gui = new GUIClass();
                gui.getFrame().setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainClass() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    gui.btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("Test"); //Not printing

        }
    });



}
}

图形界面 class:

import java.awt.EventQueue;

 public class GUIClass {

private JFrame frame;
public JButton btnNewButton;
/**
 * Launch the application.
 */


/**
 * Create the application.
 */
public GUIClass() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    btnNewButton = new JButton("New button");
    btnNewButton.setBounds(170, 107, 89, 23);
    frame.getContentPane().add(btnNewButton);
}


public JButton getBtnNewButton() {
    return btnNewButton;
}

public JFrame getFrame() {
    return frame;
}

}

问题是您从未创建 Main Class。

main方法与Main无关Class。 main 方法必须创建 MainClass.

试试这个

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {

                gui = new GUIClass();
                gui.getFrame().setVisible(true);
                new MainClass();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}