對不起,如果標題含糊不清,但這是我想實現的。
這是Battery Care
軟件iconified /最小化。當您將鼠標懸停在圖標上時,您會看到圖片中顯示的窗口。
這怎麼可以在Java中實現?
回答
這將使用JPopupMenu
上顯示點擊信息,而不是最值得推薦的方法,因爲它很難佈置其他組件...但你可以很容易地使用一個JWindow
,而不是...
public class SystemTrayTest {
public static void main(String[] args) {
if (SystemTray.isSupported()) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
try {
final JPopupMenu popup = new JPopupMenu();
popup.add(new JLabel("Charging (45%)", JLabel.CENTER));
popup.add(new JLabel("Charging", new ImageIcon(ImageIO.read(SystemTrayTest.class.getResource("/battery_connection.png"))), JLabel.LEFT));
popup.add(new JLabel("Power Saver", new ImageIcon(ImageIO.read(SystemTrayTest.class.getResource("/flash_yellow.png"))), JLabel.LEFT));
popup.add(new JSeparator());
JMenuItem exitMI = new JMenuItem("Exit");
exitMI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popup.add(exitMI);
TrayIcon trayIcon = new TrayIcon(ImageIO.read(SystemTrayTest.class.getResource("/battery_green.png")), "Feel the power");
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
popup.setLocation(e.getX(), e.getY());
popup.setInvoker(popup);
popup.setVisible(true);
}
});
SystemTray.getSystemTray().add(trayIcon);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
});
}
}
}
更新與鼠標支持
因爲我無法抗拒玩...
public class SystemTrayTest {
public static void main(String[] args) {
new SystemTrayTest();
}
public SystemTrayTest() {
if (SystemTray.isSupported()) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
try {
TrayIcon trayIcon = new TrayIcon(ImageIO.read(SystemTrayTest.class.getResource("/battery_green.png")), "Feel the power");
MouseHandler mouseHandler = new MouseHandler();
trayIcon.addMouseMotionListener(mouseHandler);
trayIcon.addMouseListener(mouseHandler);
SystemTray.getSystemTray().add(trayIcon);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
});
}
}
public class MouseHandler extends MouseAdapter {
private Timer popupTimer;
private JWindow popup;
private Point point;
public MouseHandler() {
popup = new JWindow();
((JComponent)popup.getContentPane()).setBorder(new LineBorder(Color.LIGHT_GRAY));
popup.setLayout(new GridLayout(0, 1));
popup.add(new JLabel("Charging (45%)", JLabel.CENTER));
try {
popup.add(new JLabel("Charging", new ImageIcon(ImageIO.read(getClass().getResource("/battery_connection.png"))), JLabel.LEFT));
popup.add(new JLabel("Power Saver", new ImageIcon(ImageIO.read(getClass().getResource("/flash_yellow.png"))), JLabel.LEFT));
} catch (IOException exp) {
exp.printStackTrace();
}
popup.pack();
popupTimer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (point != null) {
System.out.println(point);
Rectangle bounds = getScreenViewableBounds(point);
int x = point.x;
int y = point.y;
if (y < bounds.y) {
y = bounds.y;
} else if (y > bounds.y + bounds.height) {
y = bounds.y + bounds.height;
}
if (x < bounds.x) {
x = bounds.x;
} else if (x > bounds.x + bounds.width) {
x = bounds.x + bounds.width;
}
if (x + popup.getWidth() > bounds.x + bounds.width) {
x = (bounds.x + bounds.width) - popup.getWidth();
}
if (y + popup.getWidth() > bounds.y + bounds.height) {
y = (bounds.y + bounds.height) - popup.getHeight();
}
popup.setLocation(x, y);
popup.setVisible(true);
}
}
});
popupTimer.setRepeats(false);
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Stop");
point = null;
popupTimer.stop();
popup.setVisible(false);
}
@Override
public void mouseMoved(MouseEvent e) {
popupTimer.restart();
point = e.getPoint();
}
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
}
public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
GraphicsDevice device = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
for (GraphicsDevice gd : lstGDs) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screenBounds = gc.getBounds();
if (screenBounds.contains(pos)) {
lstDevices.add(gd);
}
}
if (lstDevices.size() == 1) {
device = lstDevices.get(0);
}
return device;
}
public static Rectangle getScreenViewableBounds(Point p) {
return getScreenViewableBounds(getGraphicsDeviceAt(p));
}
public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (gd != null) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
}
return bounds;
}
}
'JWindow'基本上就像一個可以顯示在屏幕上的'JPanel',對嗎? –
不,不是真的。 'JWindow'更像是一個未裝飾的'JFrame'('JFrame'從'JWindow'延伸) – MadProgrammer
我知道層次結構,只是給出了一個明示的:)如果你仍然想要更多的Java:http:// stackoverflow.com/questions/14375338/cannot-play-embedded-sound-in-a-jar-file –
起初,我認爲這將是相當微不足道的 - 增加一個鼠標監聽使用addMouseListener將(在任務欄圖標),並使用的mouseEntered()和mouseExitedEvents():
icon.addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
System.out.println("Mouse over icon");
}
@Override
public void mouseExited(MouseEvent e)
{
System.out.println("Mouse leaving icon");
}
});
但是,至少在我測試過的平臺上(Windows上的Java 7u10),這不起作用 - 當我將鼠標光標懸停在托盤圖標上時,沒有生成任何事件。做什麼工作是一個MouseMotionListener
和addMouseMotionListener
方法:
icon.addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseMoved(MouseEvent e)
{
System.out.println("Moving...");
}
});
活動在mouseMoved()
產生只有當鼠標移動到圖標。通過一些工作,在收到第一個事件以模擬工具提示時間之後的某個延遲之後,這可以用於顯示JWindow
(您可以通過類似於ToolTipManager.sharedInstance().getInitialDelay()
的東西獲得平臺的工具提示顯示時間)
- 1. 如何添加應用程序到系統托盤
- 2. 如何添加系統托盤圖標到應用程序?
- 3. 添加東西到UIPopoverController
- 4. 如何添加一些東西到PYTHONPATH?
- 5. 將東西添加到隱藏文件
- 6. 將東西添加到Twitch bot
- 7. iphone - 將東西添加到類方法
- 8. 如何在explorer.exe崩潰後將圖標重新添加到系統托盤中
- 9. 如何以編程方式將ContextMenu添加到系統托盤圖標?
- 10. 如何添加一些東西到Android鍵盤?
- 11. 我的圖標添加到系統托盤的順序?
- 12. 簡單的方法來窗口添加到系統托盤
- 13. 如何將延遲添加到jquery mouseover?
- 14. 如何將功能添加到窗體
- 15. 如何將SwipeRefreshLayout功能添加到MaterialDrawer
- 16. 如何將Exif功能添加到TBitmap?
- 17. 如何將mysql功能添加到tagmate?
- 18. 將功能添加到ToggleButton,
- 19. 將功能添加到servlet
- 20. 如何將鍵盤綁定添加到輪播功能? - jQuery
- 21. 如何在新行添加東西
- 22. 如何添加「basename」的東西?
- 23. 如何在TitleBar中添加「最小化到系統托盤」按鈕?
- 24. 添加東西JSON對象
- 25. 如何將Angular功能添加到新添加的HTML中?
- 26. Chrome擴展程序將新菜單添加到系統托盤Chrome的圖標
- 27. 如何以編程方式將東西添加到JS對象?
- 28. 系統托盤圖標,添加一個「點擊這裏」鏈接
- 29. 添加東西到導航欄
- 30. 是system.js添加東西到我的JavaScript?
也許您可以從[這個](http://stackoverflow.com/questions/1882055/java-swing-change-background-color-on-mouse-over) – Jeff
首先看看[如何使用系統托盤](http:///docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html) – MadProgrammer
這是關於系統托盤的具體內容嗎?或者您只想瞭解onMouseOver事件?對於系統托盤功能,您可以看到http:// docs。 oracle.com/javase/tutorial/uiswing/misc/systemtray.html –