JAVA学习39-标签

tech2024-06-21  68

import javax.swing.*; import java.awt.*; //这个图标是一个接口,实现继承JFrame public class IncoDemo extends JFrame implements Icon{ private int width; private int length; //无参构造 public IncoDemo(){ } //有参构造 public IncoDemo(int width,int length){ this.width=width; this.length=length; } public void init(){ IncoDemo incoDemo = new IncoDemo(30, 30); //图标可以放在标签,也可以放在按钮 JLabel label=new JLabel("icon",incoDemo,SwingConstants.CENTER);//设置标签的位置 Container contentPane = getContentPane(); contentPane.add(label);//添加标签 setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } //实现接口的方法 @Override public void paintIcon(Component c, Graphics g, int x, int y) { //获取图标 g.fillOval(x,y,width,length);//画一个圆 } @Override public int getIconWidth() { return this.width;//获取值 } @Override public int getIconHeight() { return this.length; } public static void main(String[] args) { new IncoDemo().init(); } }
最新回复(0)