java小程序,为何不显示按钮?
classMyFrameextendsFrame{MyFrame(Strings,intx,inty,intw,inth,Colorc){super(s);setBoun...
class MyFrame extends Frame {
MyFrame (String s, int x, int y, int w, int h, Color c) {
super(s);
setBounds(x, y, w, h);
setBackground (c);
Panel p = new Panel(null);
setLayout (new FlowLayout());
Button b = new Button("OK");
p.setBounds(w/4, h/4, w/2, h/2);
p.setBackground (Color.yellow);
p.add(b,BorderLayout.CENTER); //已经添加进去了,可是界面不显示Button,为何?
add(p);
setVisible (true);
}
} 展开
MyFrame (String s, int x, int y, int w, int h, Color c) {
super(s);
setBounds(x, y, w, h);
setBackground (c);
Panel p = new Panel(null);
setLayout (new FlowLayout());
Button b = new Button("OK");
p.setBounds(w/4, h/4, w/2, h/2);
p.setBackground (Color.yellow);
p.add(b,BorderLayout.CENTER); //已经添加进去了,可是界面不显示Button,为何?
add(p);
setVisible (true);
}
} 展开
2个回答
展开全部
位置不对,你的add(p)放在最后了,你把它放到panel p=new Panel()后面。望采纳
更多追问追答
追问
你的回答不行
追答
MyFrame(String s, int x, int y, int w, int h, Color c) {
super(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(x, y, w, h);
setBackground(c);
JPanel p = new JPanel();
JButton b = new JButton("OK");
add(p);
b.setBounds(30, 30, 30, 30);
p.add(b, BorderLayout.CENTER);
setLayout(new FlowLayout());
p.setBounds(w / 4, h / 4, w / 2, h / 2);
p.setBackground(Color.yellow);
setVisible(true);
}
你Panel p = new Panel(null);不行,把null去了!你再试试,我这段代码就可以
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询