方法要放在构造函数里面:import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{ Container contentPane = getContentPane();
JPanel p = new JPanel();
contentPane.add(p,"Center"); JButton b = new JButton("sdf"); p.add(b);
}
public static void main(String[] args)
{
JFrame f = new Test();
f.show();
}
}
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{ Container contentPane = getContentPane();
JPanel p = new JPanel();
contentPane.add(p,"Center"); JButton b = new JButton("sdf"); p.add(b);
}
public static void main(String[] args)
{
JFrame f = new Test();
f.show();
}
}
解决方案 »
- 求助~
- 关于正则表达式问题:有谁用过Matcher类的group()方法没有
- JFace编译提示找不到ListenerList,可能是环境配置的问题
- 一个新手对于学习JAVA的迷茫,请大家多多指点!
- 如何在frame上画一个矩形,并控制对它的匀速直线运动和停止?
- 排序算法求助,急!
- 简单的菜单问题,怎么不能显示。
- 菜鸟的问题!
- 谁有Borland Web Services for Java(jbuilder 7)的注册码?及Borland MobileSet3 的注册码(Jbuilder)??
- 请问如何得到一个目录下的所有文件名????
- jave怎么学习?
- main方法一定要是static的吗?
怎么可以放在定义变量初始化的地方呢
调用、执行方法,其代码必须处于某个方法之内才行拉
将这两行代码移到某个方法内即可,改动如下:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{ } Container contentPane = getContentPane();
JPanel p = new JPanel(); JButton b = new JButton("sdf");public void ss() {
contentPane.add(p,"Center");
p.add(b);
} public static void main(String[] args) {
Test f = new Test();
f.ss();
f.show();
}}
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{ } Container contentPane = getContentPane(); JPanel p = new JPanel(); JButton b = new JButton("sdf"); {
contentPane.add(p,"Center");
p.add(b);
}
public static void main(String[] args) {
Test f = new Test(); f.show();
}
}
class Test extends JFrame
{
Container contentPane = getContentPane();
JPanel p = new JPanel();
JButton b = new JButton("sdf");
public Test()
{
p.add(b);
contentPane.add(p,"Center");
}
public static void main(String[] args)
{
JFrame f = new Test();
f.show();
}
}
{
static helperForSee staticone = new helperForSee("I'm static one");
helperForSee normalone = new helperForSee("I'm normal one");
static
{
System.out.println("I'm in static block");
}
{
System.out.println("I'm in normal block");
} public LifeInOrder()
{
System.out.println("I'm in LifeInOrder Constructor");
} public static void main(String[] args)
{
System.out.println("Hello World!");
new LifeInOrder();
}
}
class helperForSee
{
public helperForSee(String str)
{
System.out.println(str);
}
};运行这个小示例,你就应该知道一个类初始化的顺序,你写的类违反了类的初始化规则! 你可以参照楼上几位的修改方法和意见!