如何将 JFrame class 用作主要 class?

How do I use a JFrame class as main class?

我正在尝试构建一个计算器。

我已经使用 Swing 插件(容器和控件)开发了界面,但是当我尝试 运行 我的程序时,它说程序包需要一个主程序 class。

已经尝试创建主 class 并调用 Calc() JFrame class,但没有成功。

看看代码:

public class Calc extends javax.swing.JPanel {
    public Calc() {
        initComponents();
    }
}

您需要一个 main() 方法来执行您的 class。

查看 How to Make Frames 上 Swing 教程中的 FrameDemo 示例代码,作为入门的基本示例。

/* FrameDemo.java requires no other files. */
public class FrameDemo {

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(175, 100));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
  }
}