java - 新编码器来了!我需要学习如何修复无法找到或加载主类
问题描述
就像我说的我是代码新手,现在我开始学习如何在 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
,所以我真的可以使用一些帮助。我到处都查过,但我就是不明白,所以如果你们能帮我把它弄糊涂的话,将不胜感激
解决方案
你必须导入摇摆类试试这个代码
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);
}
}
}
推荐阅读
- python-3.x - 创建活动时出现 Google 日历 API 400 错误
- pandas - 为什么 pandas.melt 会弄乱我的 dtypes?
- mysql - 如何仅使用时间戳字段的日期部分更新日期字段?
- javascript - 避免从 JS 和 React 上的 datapicker 中选择今天之前的日期,而不包括今天
- python - OpenCV,更改为图像中最接近的颜色
- python - 在 Anaconda Jupyter 中使用 wget 获取 csv 文件
- java - 如果我们使用 Spring Data JPA 更改其结构,如何处理数据库
- visual-studio-2019 - 无法在 Visual Studio 2019 中打开 CLR C++ DLL 项目
- elasticsearch - Elasticsearch - 列出所有向 ES 发送消息的源
- git - Git临时恢复提交并放回