1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.net.DatagramPacket; import java.net.DatagramSocket;
public class Reminder extends JFrame { private DatagramSocket socket;
public Reminder() throws Exception { initSystemTray();
initListen(initSocket()); }
private void initSystemTray() throws Exception { SystemTray tray = SystemTray.getSystemTray(); TrayIcon trayIcon = new TrayIcon(ImageIO.read(getClass().getResourceAsStream("icon.png")), ""); trayIcon.setToolTip(""); tray.add(trayIcon);
trayIcon.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON2) { socket.close(); System.exit(0); } } }); }
private DatagramPacket initSocket() throws Exception { socket = new DatagramSocket(11111); byte[] bytes = new byte[1024]; return new DatagramPacket(bytes, bytes.length); }
private void initListen(DatagramPacket packet) throws Exception { while (true) { socket.receive(packet);
showDialog(packet); } }
private void showDialog(DatagramPacket packet) { JFrame frame = new JFrame(); frame.setSize(300, 150); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JTextArea area = new JTextArea(); String osName = System.getProperty("os.name"); switch (osName) { case "Linux": area.setFont(new Font("WenQuanYi Micro Hei", Font.PLAIN, 14)); break; default: break; }
area.setText(new String(packet.getData())); frame.add(area);
frame.setVisible(true); }
public static void main(String[] args) { try { new Reminder(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } } }
|