标签
package com.hai.week2;
import javax.swing.*;
import java.awt.*;
public class Demo10 extends JFrame implements Icon {
int width;
int height;
public Demo10() {
}
public Demo10(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
Demo10 icon = new Demo10(15, 15);
JLabel jLabel = new JLabel("??", icon, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(jLabel);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo10().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;
}
}
图片标签
package com.hai.week2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo11 extends JFrame {
public Demo11() {
JLabel jLabel = new JLabel("????");
URL resource = Demo11.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(jLabel);
setBounds(100,100,500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo11();
}
}

浙公网安备 33010602011771号