歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中Icon接口的應用(以JLabel為例)

Java中Icon接口的應用(以JLabel為例)

日期:2017/3/1 10:35:53   编辑:Linux編程

This example shows the drawing of an icon using the Icon interface for the JLable component.

[java]
  1. package com.han;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. /**
  5. * This example shows the drawing of an icon using the Icon interface
  6. * for the JLable component.
  7. * @author han
  8. *
  9. */
  10. public class DrawIcon implements Icon{
  11. private int width;
  12. private int height;
  13. @Override
  14. public int getIconHeight(){
  15. return this.height;
  16. }
  17. @Override
  18. public int getIconWidth(){
  19. return this.width;
  20. }
  21. @Override
  22. public void paintIcon(Component c, Graphics g, int x, int y){
  23. g.setColor(Color.red);
  24. g.fillOval(x, y, width, height);
  25. }
  26. /*the construct function*/
  27. public DrawIcon(int width, int height){
  28. this.width=width;
  29. this.height=height;
  30. }
  31. public static void main(String[] args){
  32. DrawIcon icon=new DrawIcon(15,15);
  33. JLabel jl=new JLabel("測試",icon,SwingConstants.CENTER);
  34. JFrame jf=new JFrame();
  35. Container c=jf.getContentPane();
  36. c.add(jl);
  37. jf.setVisible(true);
  38. jf.setSize(300,300);
  39. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. }
  41. }
Copyright © Linux教程網 All Rights Reserved