码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA JLabel自定义子类无法显示

时间:2016-07-07 17:26:25      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 import java.awt.*;
 2 import java.util.Scanner;
 3 
 4 import javax.swing.*;
 5 
 6 public class Test_16_13 extends JFrame{
 7 
 8 public Test_16_13(){
 9     ImageIcon im = new ImageIcon("image/slide1.jpg");
10     String s = "jlabel_mine";
11     JLabel1 j1 = new JLabel1(s);        
12     add(j1);        
13         //add(new JLabel("JLabel标签")); 
14 }
15 
16 public static void  main(String[] args){    
17     Test_16_13 frame = new Test_16_13();
18     frame.setSize(800, 600);
19     frame.setTitle("Exercise15_8");
20     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21     frame.setLocationRelativeTo(null); // Center the frame
22     frame.setVisible(true);        
23 }   
24 class JLabel1 extends JLabel{
25     private ImageIcon ic;
26     private String s;
27     public JLabel1(ImageIcon ic){
28         this.ic =  ic;
29     }
30     public JLabel1(String s){
31         this.s = s;
32     }
33     }
34 }
View Code

代码如上,我在主类Test_16_13里面自定义了一个JLabel子类JLabel1,然后在JFrame的构造函数中添加,但是无法在JFrame里面显示,请问是为什么?

解答:

刚才把子类和父类的教程又看了一遍,想了下,原来我新建子类有一个最大的问题。那就是没有重载父类的构造函数。我的构造函数都是使用的自定义的构造函数。
所以,我的子类不能被认可。下面是我修改的JLabel1的构造函数:

 class JLabel1 extends JLabel{
//      private ImageIcon ic;
//      private String s;
        public JLabel1(ImageIcon ic){
            super(ic);
        }
        public JLabel1(String s){
//          this.s = s;
            super(s);
        }
        }

 

  

JAVA JLabel自定义子类无法显示

标签:

原文地址:http://www.cnblogs.com/wanjiang/p/5650618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!