java新手问题:为啥在JPanel中的控件显示不出来?代码如下:
求人指点import java.awt.*;
import javax.swing.*;public class Test extends JFrame{
public Test(){
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setSize(200, 200);
ImagePanel ip = new ImagePanel();
Container contentPane = getContentPane();
contentPane.add(ip);
}
public static void main(String args[]){
Test fd = new Test();
}
public class MakeLabel extends JLabel{
public MakeLabel(String str,int x,int y){
setText(str);
setLocation(x,y);
setVisible(true);
}
}
public class ImagePanel extends JPanel{
public ImagePanel(){
setLayout(null);
MakeLabel ml = new MakeLabel("dgfgfdg",50,50);
add(ml);
setVisible(true);
JButton jb = new JButton("ddf");
jb.setLocation(35, 35);
add(jb);
}
}}
求人指点import java.awt.*;
import javax.swing.*;public class Test extends JFrame{
public Test(){
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setSize(200, 200);
ImagePanel ip = new ImagePanel();
Container contentPane = getContentPane();
contentPane.add(ip);
}
public static void main(String args[]){
Test fd = new Test();
}
public class MakeLabel extends JLabel{
public MakeLabel(String str,int x,int y){
setText(str);
setLocation(x,y);
setVisible(true);
}
}
public class ImagePanel extends JPanel{
public ImagePanel(){
setLayout(null);
MakeLabel ml = new MakeLabel("dgfgfdg",50,50);
add(ml);
setVisible(true);
JButton jb = new JButton("ddf");
jb.setLocation(35, 35);
add(jb);
}
}}
import java.awt.*;
import javax.swing.*;public class Test extends JFrame{
public Test(){
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setSize(200, 200);
ImagePanel ip = new ImagePanel();
Container contentPane = jf.getContentPane();//就是这行
contentPane.add(ip);
}
public static void main(String args[]){
Test fd = new Test();
}
public class MakeLabel extends JLabel{
public MakeLabel(String str,int x,int y){
setText(str);
setLocation(x,y);
setVisible(true);
}
}
public class ImagePanel extends JPanel{
public ImagePanel(){
setLayout(null);
MakeLabel ml = new MakeLabel("dgfgfdg",50,50);
add(ml);
setVisible(true);
setSize(30,30); //还有这行!!
JButton jb = new JButton("ddf");
jb.setLocation(35, 35);
add(jb);
}
}}
import javax.swing.*;
public class TestFrame extends JFrame {
public static void main(String[] args) {
new TestFrame();
}
public TestFrame(){
this.setSize(200,200);
this.setVisible(true);
}
}
和这个
import javax.swing.*;
public class TestFrame {
public static void main(String[] args) {
JFrame jf = new JFrame("hehe!");
jf.setSize(200,200);
jf.setVisible(true);
}
}
这是两种最常见的编写GUI程序的方法。
第一种是:自己写的类继承JFrame(),这个时候自己写的类就已经有了父类JFrame()所有的属性(包括动态属性和静态属性)所以没必要在new JFrame()了,直接在构造方法中用就行了。
第二种是:既然已经引入了javax.swing.*;这个包了,直接new JFrame()用就好了。以上都是题外话,和楼主的问题没有关系。望楼主见谅!!
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true); jf.setSize(200, 200); ImagePanel ip = new ImagePanel();
Container contentPane = jf.getContentPane();//就是这行
contentPane.add(ip);
移到main函数里面就对了
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true); jf.setSize(200, 200); ImagePanel ip = new ImagePanel();
Container contentPane = jf.getContentPane();//就是这行
contentPane.add(ip);
窗口应该在实例化的时候 就要执行
不然是不会显示的
上面的代码放入 构造函数中
试试看
^_*