首页 > 解决方案 > 新编码器来了!我需要学习如何修复无法找到或加载主类

问题描述

就像我说的我是代码新手,现在我开始学习如何在 java 中制作 gui,这是我的代码

import java.awt.*;
import java.awt.event.*;




public class gui implements ActionListener{

    private int count = 0;
    private JLabel fat;
    private JButton button;
    private JLabel label;

        
    public gui(){
        JFrame frame = new JFrame();
        button = new JButton("yes");
            button.addActionListener(this);

        fat = new JLabel("number of fatties: 0");
        
        label = new JLabel("are you fat?");
        
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(label);
        panel.add(button);
        panel.add(fat);


        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("mmmm gui");
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        new gui();
        
    }
    
    @Override
    public void actionPerformed(ActionEvent a) {
        count++;
        fat.setText("number of fatties: " + count);
        if (count == 100){
            JFrame frame = new JFrame();
            
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
            panel.setLayout(new GridLayout(0, 1));
            
            
            JLabel bigfat = new JLabel("wow to be so fat to take up 100 people, you must be really fat");
            


            panel.add(bigfat);
            frame.add(panel, BorderLayout.CENTER);
            frame.setTitle("wow");
            frame.pack();
            frame.setVisible(true);

   
            

            
    
        }
        }


    }



它在 Visual Studio 代码中运行得非常好,但是当我使用 cmd 并 java gui 在 .java 文件的文件夹中执行时,它会返回Error: Could not find or load main class gui ,所以我真的可以使用一些帮助。我到处都查过,但我就是不明白,所以如果你们能帮我把它弄糊涂的话,将不胜感激

标签: java

解决方案


你必须导入摇摆类试试这个代码

import javax.swing.*;

导入 java.awt。; 导入 java.awt.event。;

类 gui 实现 ActionListener{

private int count = 0;
private JLabel fat;
private JButton button;
private JLabel label;


public gui(){
    JFrame frame = new JFrame();
    button = new JButton("yes");
    button.addActionListener(this);

    fat = new JLabel("number of fatties: 0");

    label = new JLabel("are you fat?");

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
    panel.setLayout(new GridLayout(0, 1));
    panel.add(label);
    panel.add(button);
    panel.add(fat);


    frame.add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("mmmm gui");
    frame.pack();
    frame.setVisible(true);

}

public static void main(String[] args) {
    new gui();

}
@Override
public void actionPerformed(ActionEvent a) {
    count++;
    fat.setText("number of fatties: " + count);
    if (count == 100){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        panel.setLayout(new GridLayout(0, 1));


        JLabel bigfat = new JLabel("wow to be so fat to take up 100 people, you must be really fat");



        panel.add(bigfat);
        frame.add(panel, BorderLayout.CENTER);
        frame.setTitle("wow");
        frame.pack();
        frame.setVisible(true);


    }
}

}


推荐阅读