英文:
Compile Error cannot find symbol new Dashboard(); ^ symbol: class Dashboard location: class Main 1
问题
所以我尝试编译这个程序,但我一直收到错误消息
Main.java:39: 错误: 无法找到符号
new Dashboard();
^
符号: 类 Dashboard
位置: 类 Main
1 错误
我尝试查看其他类似的帖子,尝试了他们正在做的事情,但对我都不起作用。所以我想重新安装我的Java安装程序会起作用(从jdk7升级到jdk13),但我得到了相同的结果。我觉得自己很愚蠢,要问的可能是一个非常容易忽视的问题,但我一无所知。帮助我
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Main implements ActionListener {
private int clicks = 0;
private JLabel label = new JLabel("点击次数: 0 ");
private JFrame frame = new JFrame();
public void Dashboard() {
JButton button = new JButton("点击我");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
clicks++;
label.setText("点击次数: " + clicks);
}
public static void main(String[] args) {
new Main().Dashboard();
}
}
<details>
<summary>英文:</summary>
So I'm trying to compile this program but I keep getting the error
Main.java:39: error: cannot find symbol
new Dashboard();
^
symbol: class Dashboard
location: class Main
1 error
I've tried looking at other similar posts trying out what they're doing but none of that worked for me. So I figured reinstalling my java installation would work (from jdk7 to jdk13), but I got the same results. I feel so stupid to ask what is probably a very easy oversight but I'm clueless. help me
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Main implements ActionListener {
private int clicks = 0;
private JLabel label = new JLabel("Number of clicks: 0 ");
private JFrame frame = new JFrame();
public void Dashboard() {
JButton button = new JButton("Click Me");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
clicks++;
label.setText("Number of clicks: " + clicks);
}
public static void main(String[] args) {
new Dashboard();
}
}
</details>
# 答案1
**得分**: 1
将你的类从`Main`重命名为`Dashboard`,并将`public void Dashboard()`更改为`public Dashboard()`,因为它是一个构造函数。
<details>
<summary>英文:</summary>
Rename your class to `Dashboard` from `Main`, and change `public void Dashboard()` to `public Dashboard()` since its a constructor.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论