hoanglan89
New member
- Xu
- 0
Giới thiệu mọi người 2 đoạn code để tạo và hủy System Tray icon trong nhóm lập trình Java cơ bản : Thiết kế giao diện GUI.
Đoạn code Tạo System Tray icon
?
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
static Image image = Toolkit.getDefaultToolkit().getImage("https://diendankienthuc.net/diendan/images/tray.gif");
static TrayIcon trayIcon = new TrayIcon(image, "Tester2″
public static void main(String[] a) throws Exception {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("In here");
trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
}
}
}
Hủy bỏ System tray icon
?
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SystemTrayDemo1 {
public static void main(String[] args) throws Exception {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
PropertyChangeListener pcl;
pcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent pce) {
System.out.println("Property changed = " + pce.getPropertyName());
TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
if (tia != null) {
System.out.println("Old tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia);
System.out.println();
}
tia = (TrayIcon[]) pce.getNewValue();
if (tia != null) {
System.out.println("New tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia);
System.out.println();
}
}
};
tray.addPropertyChangeListener("trayIcons", pcl);
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
TrayIcon icon = null;
tray.add(icon = new TrayIcon(bi));
Thread.sleep(3000);
tray.remove(icon);
Thread.sleep(3000);
System.exit(0);
}
}
Theo: Blog vovanhai
Đoạn code Tạo System Tray icon
?
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
static Image image = Toolkit.getDefaultToolkit().getImage("https://diendankienthuc.net/diendan/images/tray.gif");
static TrayIcon trayIcon = new TrayIcon(image, "Tester2″
public static void main(String[] a) throws Exception {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("In here");
trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
}
}
}
Hủy bỏ System tray icon
?
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SystemTrayDemo1 {
public static void main(String[] args) throws Exception {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
PropertyChangeListener pcl;
pcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent pce) {
System.out.println("Property changed = " + pce.getPropertyName());
TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
if (tia != null) {
System.out.println("Old tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia);
System.out.println();
}
tia = (TrayIcon[]) pce.getNewValue();
if (tia != null) {
System.out.println("New tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia);
System.out.println();
}
}
};
tray.addPropertyChangeListener("trayIcons", pcl);
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
TrayIcon icon = null;
tray.add(icon = new TrayIcon(bi));
Thread.sleep(3000);
tray.remove(icon);
Thread.sleep(3000);
System.exit(0);
}
}
Theo: Blog vovanhai