lop imageicon trong java swing – Tài liệu text

lop imageicon trong java swing

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (368.07 KB, 5 trang )

/>
Copyright © vietjack.com

Lớp ImageIcon trong Java Swing
Lớp ImageIcon, là một trình triển khai của Icon Iterface, để vẽ các Icon từ các Image. Dưới đây là
cú pháp khai báo của lớp javax.swing.ImageIcon:
public class ImageIcon
extends Object
implements Icon, Serializable, Accessible

Lớp này kế thừa các phương thức từ các lớp sau:

java.lang.Object

Lớp ImageIcon có các trường sau:

protected static Component component

protected static MediaTracker tracker

Các constructor của lớp ImageIcon trong lớp Java
Swing
ImageIcon(): Tạo một image icon chưa được khởi tạo.
ImageIcon(byte[] imageData): Tạo một ImageIcon từ một mảng byte mà đã được đọc từ một image
file chứa một định dạng hình ảnh được hỗ trợ, như GIF, JPEG.
ImageIcon(Image image): Tạo một ImageIcon từ một đối tượng Image.
ImageIcon(String filename): Tạo ImageIcon từ file đã cho.

ImageIcon(URL location): Tạo ImageIcon từ URL đã cho.

Các phương thức của lớp ImageIcon trong Java Swing
STT

Phương thức & Miêu tả

1

Image getImage()

Trang chia sẻ các bài học online miễn phí

Page 1

/>
Copyright © vietjack.com

Trả về hình ảnh của icon này

2

int getImageLoadStatus()
Trả về trạng thái của hoạt động tải hình ảnh này

3

ImageObserver getImageObserver()
Trả về trình quan sát observer cho hình ảnh này

4

protected void loadImage(Image image)
Tải hình ảnh, trả về chỉ khi hình ảnh được tải

5

void paintIcon(Component c, Graphics g, int x, int y)
Sơn màu icon

6

void setDescription(String description)
Thiết lập sự miêu tả của hình ảnh

7

void setImage(Image image)
Thiết lập hình ảnh được hiển thi bởi icon này

8

String toString()
Trả về một biểu diễn chuỗi của hình ảnh

Chương trình ví dụ lớp ImageIcon
package com.vietjack.gui;

import java.awt.*;

import java.awt.event.*;
import javax.swing.*;

Trang chia sẻ các bài học online miễn phí

Page 2

/>
Copyright © vietjack.com

public class SwingControlDemo {

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

public SwingControlDemo(){
prepareGUI();
}

public static void main(String[] args){
SwingControlDemo

swingControlDemo = new SwingControlDemo();

swingControlDemo.showImageIconDemo();
}

private void prepareGUI(){
mainFrame = new JFrame(“Vi du Java Swing”);
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel(“”, JLabel.CENTER);
statusLabel = new JLabel(“”,JLabel.CENTER);

statusLabel.setSize(350,100);

controlPanel = new JPanel();

Trang chia sẻ các bài học online miễn phí

Page 3

/>
Copyright © vietjack.com

controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);

}

// Tra ve mot ImageIcon, hoac null neu path la khong hop le.
private static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = SwingControlDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println(“Couldn’t find file: ” + path);
return null;
}
}

private void showImageIconDemo(){
headerLabel.setText(“Control in action: ImageIcon”);

ImageIcon icon = createImageIcon(“/resources/java_icon.png”,”Java”);

JLabel commentlabel = new JLabel(“”, icon,JLabel.CENTER);

controlPanel.add(commentlabel);

mainFrame.setVisible(true);
}

Trang chia sẻ các bài học online miễn phí

Page 4

/>
Copyright © vietjack.com

}

Trang chia sẻ các bài học online miễn phí

Page 5

ImageIcon(URL location): Tạo ImageIcon từ URL đã cho.Các phương thức của lớp ImageIcon trong Java SwingSTTPhương thức & Miêu tảImage getImage()Trang chia sẻ các bài học online miễn phíPage 1/>Copyright © vietjack.comTrả về hình ảnh của icon nàyint getImageLoadStatus()Trả về trạng thái của hoạt động tải hình ảnh nàyImageObserver getImageObserver()Trả về trình quan sát observer cho hình ảnh nàyprotected void loadImage(Image image)Tải hình ảnh, trả về chỉ khi hình ảnh được tảivoid paintIcon(Component c, Graphics g, int x, int y)Sơn màu iconvoid setDescription(String description)Thiết lập sự miêu tả của hình ảnhvoid setImage(Image image)Thiết lập hình ảnh được hiển thi bởi icon nàyString toString()Trả về một biểu diễn chuỗi của hình ảnhChương trình ví dụ lớp ImageIconpackage com.vietjack.gui;import java.awt.*;import java.awt.event.*;import javax.swing.*;Trang chia sẻ các bài học online miễn phíPage 2/>Copyright © vietjack.compublic class SwingControlDemo {private JFrame mainFrame;private JLabel headerLabel;private JLabel statusLabel;private JPanel controlPanel;public SwingControlDemo(){prepareGUI();public static void main(String[] args){SwingControlDemoswingControlDemo = new SwingControlDemo();swingControlDemo.showImageIconDemo();private void prepareGUI(){mainFrame = new JFrame(“Vi du Java Swing”);mainFrame.setSize(400,400);mainFrame.setLayout(new GridLayout(3, 1));mainFrame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent windowEvent){System.exit(0);});headerLabel = new JLabel(“”, JLabel.CENTER);statusLabel = new JLabel(“”,JLabel.CENTER);statusLabel.setSize(350,100);controlPanel = new JPanel();Trang chia sẻ các bài học online miễn phíPage 3/>Copyright © vietjack.comcontrolPanel.setLayout(new FlowLayout());mainFrame.add(headerLabel);mainFrame.add(controlPanel);mainFrame.add(statusLabel);mainFrame.setVisible(true);// Tra ve mot ImageIcon, hoac null neu path la khong hop le.private static ImageIcon createImageIcon(String path,String description) {java.net.URL imgURL = SwingControlDemo.class.getResource(path);if (imgURL != null) {return new ImageIcon(imgURL, description);} else {System.err.println(“Couldn’t find file: ” + path);return null;private void showImageIconDemo(){headerLabel.setText(“Control in action: ImageIcon”);ImageIcon icon = createImageIcon(“/resources/java_icon.png”,”Java”);JLabel commentlabel = new JLabel(“”, icon,JLabel.CENTER);controlPanel.add(commentlabel);mainFrame.setVisible(true);Trang chia sẻ các bài học online miễn phíPage 4/>Copyright © vietjack.comTrang chia sẻ các bài học online miễn phíPage 5