1 Importjava.awt.*;2 ImportJava.util.Scanner;3 4 Importjavax.swing.*;5 6 Public classTest_16_13extendsjframe{7 8 Publictest_16_13 () {9ImageIcon im =NewImageIcon ("Image/slide1.jpg");TenString s = "Jlabel_mine"; OneJLabel1 J1 =NewJLabel1 (s); A Add (J1); - //Add (New JLabel ("JLabel label")); - } the - Public Static voidMain (string[] args) { -Test_16_13 frame =Newtest_16_13 (); -Frame.setsize (800, 600); +Frame.settitle ("Exercise15_8"); - frame.setdefaultcloseoperation (jframe.exit_on_close); +Frame.setlocationrelativeto (NULL);//Center the frame AFrame.setvisible (true); at } - classJLabel1extendsjlabel{ - PrivateImageIcon IC; - PrivateString S; - PublicJLabel1 (ImageIcon ic) { - This. IC =IC; in } - PublicJLabel1 (String s) { to This. S =s; + } - } the}View Code
The code above, I in the main class test_16_13 inside custom a JLabel subclass JLabel1, and then added in the JFrame constructor, but can not be displayed in the JFrame, excuse me why?
Answer:
Just now the class and the parent class to see the tutorial again, think of the next, I have a new sub-class has one of the biggest problems. That is, the constructor for the parent class is not overloaded. My constructors are all custom constructors that are used.
Therefore, my subclass cannot be recognized. Here is the constructor of the JLabel1 I modified:
class 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 Custom subclasses cannot be displayed