java 空指针异常问题。。。不知道该怎么解决。
importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjavax.swing.JButton;impor...
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test
{
private JFrame f;
public test()
{
f.setLayout(new BorderLayout());
f.getContentPane().add(new panel1(),"North");
f.getContentPane().add(new panel2(),"South");
f.setSize(50,50);
f.setVisible(true);
}
public static void main(String [] args)
{
new test();
}
}
class panel1 extends JPanel
{
private JButton btn,btn2,btn3;
private JLabel l;
public panel1()
{
this.setLayout(new FlowLayout());
btn = new JButton("OK");
btn2 = new JButton("Cancel");
btn3 = new JButton("NO");
l = new JLabel("hgf");
add(btn);
add(btn2);
add(btn3);
add(l);
}
}
class panel2 extends JPanel
{
private JButton btn;
private JLabel l;
public panel2()
{
this.setLayout(new FlowLayout());
btn = new JButton("WoWo");
l = new JLabel("asas");
add(btn);
add(l);
}
} 展开
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test
{
private JFrame f;
public test()
{
f.setLayout(new BorderLayout());
f.getContentPane().add(new panel1(),"North");
f.getContentPane().add(new panel2(),"South");
f.setSize(50,50);
f.setVisible(true);
}
public static void main(String [] args)
{
new test();
}
}
class panel1 extends JPanel
{
private JButton btn,btn2,btn3;
private JLabel l;
public panel1()
{
this.setLayout(new FlowLayout());
btn = new JButton("OK");
btn2 = new JButton("Cancel");
btn3 = new JButton("NO");
l = new JLabel("hgf");
add(btn);
add(btn2);
add(btn3);
add(l);
}
}
class panel2 extends JPanel
{
private JButton btn;
private JLabel l;
public panel2()
{
this.setLayout(new FlowLayout());
btn = new JButton("WoWo");
l = new JLabel("asas");
add(btn);
add(l);
}
} 展开
4个回答
展开全部
public class test
{
private JFrame f;
public test()
{
f.setLayout(new BorderLayout());
f.getContentPane().add(new panel1(),"North");
f.getContentPane().add(new panel2(),"South");
f.setSize(50,50);
f.setVisible(true);
}}
这段代码,请在构造函数内添加 f = new JFrame();
{
private JFrame f;
public test()
{
f.setLayout(new BorderLayout());
f.getContentPane().add(new panel1(),"North");
f.getContentPane().add(new panel2(),"South");
f.setSize(50,50);
f.setVisible(true);
}}
这段代码,请在构造函数内添加 f = new JFrame();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
f没有初始化,值为null,引用f.setLayout(new BorderLayout());时导致空指针异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private JFrame f;
要先实例化
要先实例化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private JFrame f; 没有实例化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询