如何将JFrame类用作主类?

问题描述 投票:0回答:1

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

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

已经尝试创建一个主类并调用Calc()JFrame类,但它没有用。

看看代码:

public class Calc extends javax.swing.JPanel {
    public Calc() {
        initComponents();
    }
}
java swing class jframe main
1个回答
1
投票

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

请查看FrameDemo上Swing教程中的How to Make Frames示例代码,以获取入门的基本示例。

/* 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();
        }
    });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.