当前位置: 代码迷 >> Java相关 >> [求助]不知道那里错了
  详细解决方案

[求助]不知道那里错了

热度:154   发布时间:2006-03-23 16:32:00.0
[求助]不知道那里错了
import javax.swing.*;
public class Weizhidaxiao extends JApplet
{
static JFrame f;
JPanel p;
JButton b;
public Weizhidaxiao()
{
p=new JPanel();
b=new JButton("button1");
p.setLayout(null);
b.setBounds(50,60,40,23);
p.add(b);
f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);
f.setSize(200,200);

}
public static void main(String args[])
{
Weizhidaxiao obj=new Weizhidaxiao();
}

}

[此贴子已经被作者于2006-3-23 16:40:07编辑过]

搜索更多相关的解决方案: public  static  JButton  JFrame  JPanel  

----------------解决方案--------------------------------------------------------

f.getContentPane().add(p);
这就是你错误的地方
这个时候 f 根本没有初始化
并且你这个方法也不对
你在使f.setVisible(true)后,再调整其大小是不对的,这样会使加入的按钮看不到
import javax.swing.*;
public class Weizhidaxiao
{
static JFrame f;
JPanel p;
JButton b;
public Weizhidaxiao()
{
b=new JButton("button1");
b.setBounds(50,60,40,23);
f=new JFrame();
f.getContentPane().setLayout(null);
f.getContentPane().add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100,100);
f.setResizable(false);
f.setVisible(true);


}
public static void main(String args[])
{
Weizhidaxiao obj=new Weizhidaxiao();
}

}
你试下,这个可以看到你想要的效果
还有,原来你那个继承JApplet没什么用,我就没有继承了


----------------解决方案--------------------------------------------------------
谢谢大哥! 成功了!
我的QQ是360071884 可以加你吗?
刚才是网络出问题发2个! 请谅解
----------------解决方案--------------------------------------------------------

有问题的话就在论坛上发吧,
这样大家都能看到,并且有些问题我也不清楚啊
大家的力量才是最大的


----------------解决方案--------------------------------------------------------
  相关解决方案