Java 启动GUI程序时没有界面
我对Intellij的想法有意见。当我运行代码时,即使没有错误,GUI也不会给我任何接口 你能帮帮我吗 代码如下:Java 启动GUI程序时没有界面,java,swing,intellij-idea,Java,Swing,Intellij Idea,我对Intellij的想法有意见。当我运行代码时,即使没有错误,GUI也不会给我任何接口 你能帮帮我吗 代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GUI implements ActionListener { int count =0; JFrame fr
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
int count =0;
JFrame frame;
JPanel panel;
JButton button;
JLabel label;
public GUI(){
frame=new JFrame();
button =new JButton("Click me");
button.addActionListener(this);
label =new JLabel("Number of Clicks : 0");
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("OUR GUI");
frame.pack();
frame.setVisible(true);
}
public void main(String[] args){
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of Clicks : "+count );
}
}
您应该将
公共void main(String[]args)
替换为公共静态void main(String[]args)
。
java中的
main
方法始终是publicstaticvoidmain(String[]args)
,其他标题不能是您的主方法。为什么在主方法中使用新GUI()?一旦您在main声明中包含static,您的代码就可以为我工作了。new GUI()是打开windowJems的构造函数谢谢您…它终于工作了!!您忘记将static
添加到main
方法