⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingtrayicon.java

📁 Windows状态栏也称系统托盘
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		 		m_Balloon = new BalloonBox(SwingTrayIcon.this);		 		centerDialog(m_Balloon);		 	}		 	m_Balloon.updateButtons();			m_Balloon.setVisible(true);		}	}				// Callback listener handles balloon button	private class FlashListener implements ActionListener {		public void actionPerformed(ActionEvent evt) {			System.out.println("[Flash window].");			try {				WindowsTrayIcon.flashWindow(SwingTrayIcon.this);			} catch (TrayIconException e) {				System.out.println("Flash window not supprted.");				System.out.println("Are you running windows 95?");			}		}	}				// Callback listener handles balloon messages	private class BalloonMessageListener implements TrayBalloonListener {		public void balloonChanged(TrayBalloonEvent evt) {			if ((evt.getMask() & TrayBalloonEvent.SHOW) != 0) {				System.out.println("Balloon shown");			}			if ((evt.getMask() & TrayBalloonEvent.HIDE) != 0) {				System.out.println("Balloon hidden");			}			if ((evt.getMask() & TrayBalloonEvent.TIMEOUT) != 0) {				System.out.println("Balloon time out");			}			if ((evt.getMask() & TrayBalloonEvent.CLICK) != 0) {				System.out.println("Balloon clicked by user");			}		}	}		// Callback listener handles restore (click left on any icon / show popup menu)	private class RestoreListener implements ActionListener {				protected boolean from_menu;			public RestoreListener(boolean fromMenu) {			from_menu = fromMenu;		}		public void actionPerformed(ActionEvent evt) {			if (from_menu) {								setVisible(true);				toFront(); requestFocus();			} else if (evt.getActionCommand().equals("Left")) {				if (!isVisible()) {					// Make main window visible if it was hidden					setVisible(true);					// Request input focus					toFront(); requestFocus();				} else {					doHide(false);				}			}		}	}		// Callback listener handles restore (click left on any icon / show popup menu)	private class ChangeMenuListener implements ActionListener {		protected TrayIconPopupCheckItem m_Item;		protected int m_Change;		public ChangeMenuListener(TrayIconPopupCheckItem item, int change) {			m_Item = item;			m_Change = change;		}		public void actionPerformed(ActionEvent evt) {			TrayIconPopupCheckItem source = (TrayIconPopupCheckItem)evt.getSource();			boolean value = source.getCheck();			switch (m_Change) {			    case ENABLE_ITEM:			        m_Item.setEnabled(value); break;			    case BOLD_ITEM:			        m_Item.setDefault(value); break;			    case CHECK_ITEM:			        m_Item.setCheck(value); break;			}		}	}			// Callback listener handles restore (click left on any icon / show popup menu)	private class SwingChangeMListener implements ItemListener {		protected JCheckBoxMenuItem m_Item;		protected int m_Change;		public SwingChangeMListener(JCheckBoxMenuItem item, int change) {			m_Item = item;			m_Change = change;		}		public void itemStateChanged(ItemEvent e) {			boolean value = e.getStateChange() == ItemEvent.SELECTED;			switch (m_Change) {			    case ENABLE_ITEM:			        m_Item.setEnabled(value); break;/*			    case BOLD_ITEM:			        m_Item.setDefault(value); break;*/			    case CHECK_ITEM:			        m_Item.setState(value); break;			}		}	}			// Called when user selects "Use Swing Menu" checkbox	public class MySwingMenuListener implements ItemListener {        		public void itemStateChanged(ItemEvent e) {			boolean chinese = false; // m_UseChinese.getState();			if (e.getStateChange() == ItemEvent.SELECTED) {				doUpdateMenu(false, !chinese);			} else {                				doUpdateMenu(true, !chinese);                			}		}    	}    	// Test listener for double-click events	public class TestMouseListener extends MouseAdapter {    		public void mousePressed(MouseEvent evt) {			if ((evt.getModifiers() & MouseEvent.BUTTON1_MASK) != 0 && evt.getClickCount() == 2) {				System.out.println("[Tray icon double clicked].");			}		}	}}// Stupid about box for demo appclass AboutBox extends JDialog {	// Create new about box given parent frame	public AboutBox(JFrame parent)  {		// Make modal dialog given parent and title		super(parent, "About TrayIcon", true);		// Layout stuff		JPanel panel = new JPanel();		panel.setLayout(new BorderLayout(3,3));		JPanel txt = new JPanel();		txt.setLayout(new GridLayout(0,1));		txt.add(new JLabel("TrayIcon version: "+WindowsTrayIcon.TRAY_VERSION));		txt.add(new JLabel("OS version: "+WindowsTrayIcon.getWindowsVersionString()));		txt.add(new JLabel("Written by Jan Struyf <Jan.Struyf@cs.kuleuven.ac.be>"));				panel.add(txt, BorderLayout.CENTER);		JPanel buttons = new JPanel();		buttons.setLayout(new FlowLayout());		JButton button = new JButton("OK");		buttons.add(button, BorderLayout.CENTER);		panel.add(buttons, BorderLayout.SOUTH);		// Close listeners for OK button and window button		button.addActionListener(new CloseListener());		addWindowListener(new CloseWindowListener());		panel.setBorder(new EmptyBorder(4,4,4,4));		setContentPane(panel);		pack();	}	// Close listener for OK button	private class CloseListener implements ActionListener {		public void actionPerformed(ActionEvent evt) {			dispose();		}	}	// Close listener for windows button	private class CloseWindowListener extends WindowAdapter {		public void windowClosing(WindowEvent evt) {			dispose();		}	}}// Stupid about box for demo appclass BalloonBox extends JFrame {	SwingTrayIcon m_Parent;	JRadioButton m_Duke, m_Printer, m_MSDOS, m_France;	JRadioButton m_Info, m_Warning, m_Error, m_None;	JTextField m_Title, m_Message, m_Time;	JCheckBox m_Sound, m_Unicode;		// Create new about box given parent frame	public BalloonBox(SwingTrayIcon parent)  {		// Make modal dialog given parent and title		super("TrayIcon Balloon");		m_Parent = parent;		// Layout stuff		JPanel panel = new JPanel();		panel.setLayout(new BorderLayout(3,3));				JPanel select = new JPanel();		select.setLayout(new BorderLayout(3,3));				JPanel p1 = new JPanel();				ButtonGroup g1 = new ButtonGroup();		p1.setLayout(new GridLayout(0,1));		p1.add(m_Duke = new JRadioButton("Duke"));		p1.add(m_Printer = new JRadioButton("Printer"));		p1.add(m_MSDOS = new JRadioButton("MS-DOS"));		p1.add(m_France = new JRadioButton("France"));				m_Duke.setSelected(true);		g1.add(m_Duke);		g1.add(m_Printer);		g1.add(m_MSDOS);				g1.add(m_France);		select.add(p1, BorderLayout.WEST);				JPanel p2 = new JPanel();		ButtonGroup g2 = new ButtonGroup();		p2.setLayout(new GridLayout(0,1));				p2.add(m_Info = new JRadioButton("Info"));		p2.add(m_Warning = new JRadioButton("Warning"));		p2.add(m_Error = new JRadioButton("Error"));		p2.add(m_None = new JRadioButton("None"));				m_Info.setSelected(true);		g2.add(m_Info);		g2.add(m_Warning);				g2.add(m_Error);				g2.add(m_None);				select.add(p2, BorderLayout.EAST);				select.add(m_Sound = new JCheckBox("Sound"), BorderLayout.NORTH);		select.add(m_Unicode = new JCheckBox("Unicode Conversion"), BorderLayout.SOUTH);		m_Unicode.setSelected(WindowsTrayIcon.hasUnicodeConversion(WindowsTrayIcon.UNICODE_CONV_BALLOON));		m_Unicode.setEnabled(WindowsTrayIcon.hasUnicodeConversion(WindowsTrayIcon.UNICODE_CONV_SUPPORT));		m_Sound.setSelected(true);		JPanel p3 = new JPanel();		p3.setLayout(new GridLayout(0,1));		p3.add(new JLabel("Title:"));		p3.add(new JLabel("Message:"));		p3.add(new JLabel("Time:"));				JPanel p4 = new JPanel();		p4.setLayout(new GridLayout(0,1));		p4.add(m_Title = new JTextField(25));		p4.add(m_Message = new JTextField(25));		p4.add(m_Time = new JTextField(25));				m_Title.setText("WindowsTrayIcon");		m_Message.setText("Hello World!");		m_Time.setText("10000");		JPanel p5 = new JPanel();		p5.setLayout(new BorderLayout(3,3));		p5.add(p3, BorderLayout.WEST);		p5.add(p4, BorderLayout.EAST);				panel.add(p5, BorderLayout.NORTH);				JPanel p6 = new JPanel();		p6.setLayout(new BorderLayout(3,3));				p6.add(select, BorderLayout.NORTH);				JPanel buttons = new JPanel();		buttons.setLayout(new FlowLayout());		JButton button = new JButton("Show Balloon", new ImageIcon(parent.loadImage("Balloon.gif")));		buttons.add(button, BorderLayout.CENTER);		p6.add(buttons, BorderLayout.SOUTH);		panel.add(p6, BorderLayout.SOUTH);		button.addActionListener(new BalloonListener());		addWindowListener(new CloseWindowListener());		panel.setBorder(new EmptyBorder(4,4,4,4));		setContentPane(panel);		pack();			}	public void updateButtons() {		m_Duke.setEnabled(m_Parent.getIcon(0).isVisible());		m_Printer.setEnabled(m_Parent.getIcon(1).isVisible());		m_MSDOS.setEnabled(m_Parent.getIcon(2).isVisible());		m_France.setEnabled(m_Parent.getIcon(3).isVisible());	}	// Close listener for Show button	private class BalloonListener implements ActionListener {		public void actionPerformed(ActionEvent evt) {			int icon = 0;			int flags = 0;						int timeout = 10000;			boolean unicode_cnv = false;			if (m_Printer.isSelected()) icon = 1;			if (m_MSDOS.isSelected())   icon = 2;			if (m_France.isSelected())  icon = 3;			if (m_Info.isSelected())    flags =  WindowsTrayIcon.BALLOON_INFO;			if (m_Warning.isSelected()) flags =  WindowsTrayIcon.BALLOON_WARNING;			if (m_Error.isSelected())   flags =  WindowsTrayIcon.BALLOON_ERROR;			if (!m_Sound.isSelected())  flags |= WindowsTrayIcon.BALLOON_NOSOUND;			if (m_Unicode.isSelected()) unicode_cnv = true;			try {				timeout = Integer.parseInt(m_Time.getText());			} catch (NumberFormatException e) {}			String title = m_Title.getText();			String message = m_Message.getText();			WindowsTrayIcon ticon = m_Parent.getIcon(icon);			try {				WindowsTrayIcon.enableUnicodeConversion(WindowsTrayIcon.UNICODE_CONV_BALLOON, unicode_cnv);				ticon.showBalloon(message, title, timeout, flags);						} catch (TrayIconException e) {				System.out.println("Error showing balloon message");			}		}	}	// Close listener for windows button	private class CloseWindowListener extends WindowAdapter {		public void windowClosing(WindowEvent evt) {			setVisible(false);		}	}}// Panel with checkbox, string and iconclass TrayIconButton extends JPanel {	// The checkbox for enabling this Tray Icon	JCheckBox button;	// The Tray Icon's class	WindowsTrayIcon icon;	// The Icon's image	Image m_Image;	// The parent window	SwingTrayIcon m_Parent;	// Create new panel with checkbox, string and icon	// Name = Printer/France/Battery/MS-DOS	// Image = 16x16 Icon gif	// Wd = 16, Hi = 16	public TrayIconButton(String name, Image image, int wd, int hi, SwingTrayIcon parent) 							throws TrayIconException, InterruptedException {		m_Parent = parent;		setLayout(new BorderLayout(3,3));		// Add icon image in ImageDisplayer		add(new JLabel(new ImageIcon(image)), BorderLayout.EAST);		// Add checkbox for enabling Tray Icon		add(button = new JCheckBox(name), BorderLayout.CENTER);		button.addItemListener(new ToggleListener());		// Create Tray Icon and set tooltip		icon = new WindowsTrayIcon(image, wd, hi);		icon.setToolTipText(name);		m_Image = image;	}	// Add Tray Icon to System Tray		public void selectIcon(boolean select) {		button.setSelected(select);	}		// Add popup menu to Tray Icon		public void setSwingPopup(SwingTrayPopup popup) {	    		popup.setTrayIcon(icon);	}	// Add popup menu to Tray Icon	public void setPopup(TrayIconPopup popup) {		icon.setPopup(popup);	}	// Add action listener to Tray Icon	public void addActionListener(ActionListener listener) {		icon.addActionListener(listener);	}		// Add mouse listener to Tray Icon	public void addMouseListener(MouseListener mouse) {		icon.addMouseListener(mouse);	}		// Add balloon listener to Tray Icon	public void addBalloonListener(TrayBalloonListener mouse) {		icon.addBalloonListener(mouse);	}		// Icon visible?	public boolean testVisible() {		return icon.isVisible();	}		// Return the associated WindowsTrayIcon	public WindowsTrayIcon getIcon() {		return icon;	}		// Return the icon's image	public Image getImage() {		return m_Image;	}	// Listener for checkbox	// Make icon visible/invisible depending on checkbox state	private class ToggleListener implements ItemListener {		public void itemStateChanged(ItemEvent evt) {			icon.setVisible(evt.getStateChange() == ItemEvent.SELECTED);			m_Parent.updateBalloonButtons();		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -