图标
package lesson6;
import javax. swing. * ;
import java. awt. * ;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo ( ) { }
public IconDemo ( int width, int height) {
this . width= width;
this . height= height;
}
public void init ( ) {
IconDemo iconDemo= new IconDemo ( 15 , 15 ) ;
JLabel label= new JLabel ( "icontest" , iconDemo, SwingConstants. CENTER) ;
Container container = getContentPane ( ) ;
container. add ( label) ;
this . setVisible ( true ) ;
this . setDefaultCloseOperation ( WindowConstants. EXIT_ON_CLOSE) ;
}
public static void main ( String[ ] args) {
new IconDemo ( ) . init ( ) ;
}
@Override
public void paintIcon ( Component c, Graphics g, int x, int y) {
g. fillOval ( x, y, width, height) ;
}
@Override
public int getIconWidth ( ) {
return this . width;
}
@Override
public int getIconHeight ( ) {
return this . height;
}
}
如果需要插入图片 logo
package lesson6;
import javax. swing. * ;
import java. awt. * ;
import java. net. URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo ( ) {
URL url= ImageIconDemo. class . getResource ( "头像.jpg" ) ;
JLabel label= new JLabel ( "ImageIcon" ) ;
ImageIcon imageIcon = new ImageIcon ( url) ;
label. setIcon ( imageIcon) ;
label. setHorizontalAlignment ( SwingConstants. CENTER) ;
Container container= getContentPane ( ) ;
container. add ( label) ;
setVisible ( true ) ;
setDefaultCloseOperation ( WindowConstants. EXIT_ON_CLOSE) ;
}
public static void main ( String[ ] args) {
new ImageIconDemo ( ) ;
}
}