图标icon、图片imageicon
package com. dong. lesson04 ;
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 jLabel = new JLabel ( "icontest" , iconDemo, SwingConstants . CENTER) ;
Container container = getContentPane ( ) ;
container. add ( jLabel) ;
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 width;
}
@Override
public int getIconHeight ( ) {
return height;
}
}
package com. dong. lesson04 ;
import javax. swing. * ;
import java. awt. * ;
import java. net. URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo ( ) {
JLabel jLabel = new JLabel ( "ImageIcon" ) ;
URL url = ImageIconDemo . class . getResource ( "滑稽.jpg" ) ;
ImageIcon imageIcon = new ImageIcon ( url) ;
jLabel. setIcon ( imageIcon) ;
Container container = getContentPane ( ) ;
container. add ( jLabel) ;
setVisible ( true ) ;
setBounds ( 100 , 100 , 200 , 200 ) ;
}
public static void main ( String [ ] args) {
new ImageIconDemo ( ) ;
}
}