生活随笔
收集整理的這篇文章主要介紹了
【标签组件与图标 3.3】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.圖片圖標。
SWing 利用javax.swing.ImageIcon 類根據現有圖片創建圖標,ImageIcon類實現了Icon接口,同時Java支持多種圖片格式。
public ImageIcon():該構造方法創建了一個通用的ImageIcon對象,當正真需要設置圖片時在使用ImageIcon對象調用setImage(Image image)public ImageIcon(Image image):可以直接從圖片源創建圖標public ImageIcon(Image image,string description):除了可以從圖片源創建圖標之外,還可以添加簡單的描述,但是描述不會出現在圖標上,可以使用getDescription()方法獲取這個描述。public ImageIcon(URL url):用于計算機網絡上的圖像文件創建圖標。實例在項目組中創建實現Icon接口的DrawIcon類,該類實現自定義的圖標類。 1 package 常用窗體;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Container;
6
7 import javax.print.DocFlavor.URL;
8 import javax.swing.Icon;
9 import javax.swing.ImageIcon;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.SwingConstants;
13 import javax.swing.WindowConstants;
14
15 public class MyImageIcon extends JFrame{
16
17
18 private boolean ture;
19 public MyImageIcon() {
20 Container container=getContentPane();
21 JLabel jl=new JLabel("This is a new JFrame window",JLabel.CENTER);//to create a label or jl.setText("乖乖貓貓");
22
23 setTitle("使用標簽組件");
24 setBounds(100,100,300,200);
25
26 getContentPane().add(jl,BorderLayout.NORTH);
27 java.net.URL url=MyImageIcon.class.getResource("imageButton.jpg");//get the picture's url
28 Icon icon =new ImageIcon(url);//make a real icon example
29
30 jl.setIcon(icon);;//setting the picture for the label
31 jl.setHorizontalAlignment(SwingConstants.CENTER);//set the constants in the center
32 //container.setBackground(Color.blue);
33 jl.setOpaque(ture);//不透明狀態
34 container.add(jl);
35 setSize(200,150);
36 setVisible(true);
37 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
38 }
39 public static void main(String[] args) {
40 new MyImageIcon();
41 // TODO Auto-generated method stub
42
43 }
44
45 }
轉載于:https://www.cnblogs.com/yitou13/p/8506473.html
總結
以上是生活随笔為你收集整理的【标签组件与图标 3.3】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。