一个FlowLayout小问题 你的文件名必须和你的主类名(public声明的)一致把你的文件名改为DemoLayout.java就行了这种问题最好去多看看书啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 忘了看第一个错f.getContentPane().add...这个我从没见过:)getContentPane().add就行了,去掉前面的f 注意你第一个程序中f是FlowLayout的一个实例,它是没有getContentPane()方法的,你应该调用JFrame类的getContentPane()方法. 第一个的f对象不是JFrame类型,用this.getContentPane()就可以了。第二个是文件名出错。 第二个程序文件名改成一致了,可运行后dos窗口一闪而过,好像提示什么i/o出错,不明白....第一个程序用this.getContentPane()也不行, 不用this也不行,大家帮忙给调试一下. getContentPane()不是FlowLayout的成员函数。public void init()是成员函数,不是构造函数,不会默认调用。我的代码如下,可以通过:import javax.swing.*;import java.awt.*;class DemoLayout extends JFrame{ JButton b1,b2,b3; FlowLayout f; JPanel p1; public DemoLayout() { f=new FlowLayout(); p1=new JPanel(); p1.setLayout(f); Container container=getContentPane(); container.add(p1); b1=new JButton("Button1"); b2=new JButton("Button2"); b3=new JButton("Button3"); p1.add(b1); p1.add(b2); p1.add(b3); } public static void main(String[] args) { DemoLayout ft=new DemoLayout(); ft.pack(); ft.setVisible(true); ft.setSize(300,300); ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 谢谢七夜,我又修改了一下import javax.swing.*;import java.awt.*;public class DemoLayout extends JFrame{ JFrame f=new JFrame(); JButton b1,b2,b3; FlowLayout f1; JPanel p1; public void init() { f1=new FlowLayout(); p1=new JPanel(); f.getContenPane().add(p1); setLayout(f1); b1=new JButton("Button1"); b2=new JButton("Button2"); b3=new JButton("Button3"); p1.add(b1); p1.add(b2); p1.add(b3); } public static void main(String[] args) { DemoLayout ft=new DemoLayout(); ft.pack(); ft.setVisible(true); ft.setSize(300,300); ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}看起来和你的差不多啊,可是还提示symbol : method getContenPane ()location: class javax.swing.JFrame f.getContenPane().add(p1); 看起来差不多,其实差了个t,楼主应该更细心点,先自己调试一下,实在不行再来问import javax.swing.*;import java.awt.*;public class DemoLayout extends JFrame{ JFrame f=new JFrame(); JButton b1,b2,b3; FlowLayout f1; JPanel p1; public void init() { f1=new FlowLayout(); p1=new JPanel(); f.getContentPane().add(p1); setLayout(f1); b1=new JButton("Button1"); b2=new JButton("Button2"); b3=new JButton("Button3"); p1.add(b1); p1.add(b2); p1.add(b3); } public static void main(String[] args) { DemoLayout ft=new DemoLayout(); ft.pack(); ft.setVisible(true); ft.setSize(300,300); ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 成员初始化放在构造函数中就一切ok,而且也没必要再要一个JFrame,你已经继承了通过如下:import javax.swing.*;import java.awt.*;public class DemoLayout extends JFrame{ JButton b1,b2,b3; FlowLayout f1; JPanel p1; public DemoLayout() { f1=new FlowLayout(); p1=new JPanel(); getContentPane().setLayout(f1); b1=new JButton("Button1"); b2=new JButton("Button2"); b3=new JButton("Button3"); p1.add(b1); p1.add(b2); p1.add(b3); getContentPane().add(p1); } public static void main(String[] args) { DemoLayout ft=new DemoLayout(); ft.pack(); ft.setVisible(true); ft.setSize(300,300); ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 先向上转型,再向下转型。和,先向下转型,再向上转型。会有危险吗?请举例说明? csdn的bug? 放在非技术区没人理我, SecurityException的触发时机是什么呢? java 问题 请教过来人:是应该学得深还是要学得广 如何得知某一个.class文件存在于哪个jar包中?? 请教水仙花数的程序? 请大家提供图形包的信息! 关于#2005-02-12 23:00:00#的问题 java pert 任务大家看看 怎么实现 初学者问题 我用Jbuilder9写JavaBean时引用没有包名的类出错,急!!!
f.getContentPane().add...这个我从没见过:)
getContentPane().add就行了,去掉前面的f
第二个是文件名出错。
第一个程序用this.getContentPane()也不行,
public void init()是成员函数,不是构造函数,不会默认调用。
我的代码如下,可以通过:
import javax.swing.*;
import java.awt.*;class DemoLayout extends JFrame
{
JButton b1,b2,b3;
FlowLayout f;
JPanel p1;
public DemoLayout()
{
f=new FlowLayout();
p1=new JPanel();
p1.setLayout(f);
Container container=getContentPane();
container.add(p1);
b1=new JButton("Button1");
b2=new JButton("Button2");
b3=new JButton("Button3");
p1.add(b1);
p1.add(b2);
p1.add(b3); }
public static void main(String[] args)
{
DemoLayout ft=new DemoLayout();
ft.pack();
ft.setVisible(true);
ft.setSize(300,300);
ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.*;public class DemoLayout extends JFrame
{
JFrame f=new JFrame();
JButton b1,b2,b3;
FlowLayout f1;
JPanel p1;
public void init()
{
f1=new FlowLayout();
p1=new JPanel();
f.getContenPane().add(p1);
setLayout(f1);
b1=new JButton("Button1");
b2=new JButton("Button2");
b3=new JButton("Button3");
p1.add(b1);
p1.add(b2);
p1.add(b3);
}
public static void main(String[] args)
{
DemoLayout ft=new DemoLayout();
ft.pack();
ft.setVisible(true);
ft.setSize(300,300);
ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
看起来和你的差不多啊,可是还提示
symbol : method getContenPane ()
location: class javax.swing.JFrame
f.getContenPane().add(p1);
import javax.swing.*;
import java.awt.*;public class DemoLayout extends JFrame
{
JFrame f=new JFrame();
JButton b1,b2,b3;
FlowLayout f1;
JPanel p1;
public void init()
{
f1=new FlowLayout();
p1=new JPanel();
f.getContentPane().add(p1);
setLayout(f1);
b1=new JButton("Button1");
b2=new JButton("Button2");
b3=new JButton("Button3");
p1.add(b1);
p1.add(b2);
p1.add(b3);
}
public static void main(String[] args)
{
DemoLayout ft=new DemoLayout();
ft.pack();
ft.setVisible(true);
ft.setSize(300,300);
ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
通过如下:
import javax.swing.*;
import java.awt.*;public class DemoLayout extends JFrame
{
JButton b1,b2,b3;
FlowLayout f1;
JPanel p1;
public DemoLayout()
{
f1=new FlowLayout();
p1=new JPanel();
getContentPane().setLayout(f1);
b1=new JButton("Button1");
b2=new JButton("Button2");
b3=new JButton("Button3");
p1.add(b1);
p1.add(b2);
p1.add(b3);
getContentPane().add(p1);
}
public static void main(String[] args)
{
DemoLayout ft=new DemoLayout();
ft.pack();
ft.setVisible(true);
ft.setSize(300,300);
ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}