我将把“Public static void main(String [] args)”放在哪里? [英] Where will i put "Public static void main(String[] args)"?
问题描述
我几天前开始编写java代码。我做了一些成功的程序,但我坚持这一个。
无论我写Public static void main(String [] args)代码,我都会收到错误。大多数时候它告诉我;是期待。我知道把;面对那段代码是错误的。有人能告诉我到底在哪写吗?或者如果可能的话为我修复代码?以下是代码:
I started coding java a few days ago. İ made a few succesfull programs but im stuck on this one. Where ever i write the "Public static void main(String[] args)" code i get an error. Most of the time it tells me that ";" is expected. I know that putting ";" infront of that code is wrong. Can someone tell me where exactly to write that? Or if possible fix the code for me? Here is the code:
import javax.swing.*;
import java.awt.*;
public class Panel_Test extends JFrame{
public static void main(String[] args){
public Board(){
super("Java Panel");
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JPanel a = new JPanel();
JPanel a2 = new JPanel();
JButton b = new JButton("Button 1");
JButton c = new JButton("Button 2");
JCheckBox cb = new JCheckBox("CB1");
JCheckBox cb2 = new JCheckBox("CB2");
a.add(b);
a.add(c);
a2.add(cb);
a2.add(cb2);
add(a, BorderLayout.SOUTH);
add(a2, BorderLayout.NORTH);
}
}
}
推荐答案
让您的构造函数与 main
方法分开,并使其名称与您的类名相同(即 public Panel_Test
因为您的班级名称是 Panel_Test
)。
Have your constructor be separate from the main
method, and have its name be the same as your class name (i.e. public Panel_Test
since the name of your class is Panel_Test
).
public class Panel_Test extends JFrame {
public Panel_Test() {
// code here
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Panel_Test();
}
});
}
}
不要忘记对你进行Swing操作Event Dispatch Thread,使用 javax.swing.SwingUtilities
。
Don't forget to do your Swing operations on the Event Dispatch Thread, using javax.swing.SwingUtilities
.
这篇关于我将把“Public static void main(String [] args)”放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!