如何隐藏任务栏系统托盘中的JFrame

如何隐藏任务栏系统托盘中的JFrame,第1张

概述如何隐藏任务栏系统托盘中的JFrame

我创build了一个JFrame并希望隐藏在taskbar (在windows不可见的权利在底部,但隐藏在tray menu items )。


有谁可以告诉我该怎么做?


我是否需要在windows系统设置中进行一些更改?


例如,您可能已经看到一些download managers , team vIEwer , 4shared desktop等隐藏在任务栏的托盘菜单项。

如何获得任务栏的背景颜色

使用launch4j将Java应用程序固定到windows 7任务栏

Qt最小化任务栏图标鼠标事件

JavaFX在全屏模式下更改场景

如何获取通知区域图标列表?

import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.UIManager; /** * * @author Mohammad Faisal * ermohammadfaisal.blogspot.com * facebook.com/m.faisal6621 * */ public class HIDetoSystemTray extends JFrame{ TrayIcon trayIcon; SystemTray tray; HIDetoSystemTray(){ super("SystemTray test"); System.out.println("creating instance"); try{ System.out.println("setting look and feel"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassname()); }catch(Exception e){ System.out.println("Unable to set LookAndFeel"); } if(SystemTray.isSupported()){ System.out.println("system tray supported"); tray=SystemTray.getSystemTray(); Image image=Toolkit.getDefaultToolkit().getimage("/media/faisal/Dukeimg/Duke256.png"); ActionListner exitListner=new ActionListner() { public voID actionPerformed(ActionEvent e) { System.out.println("Exiting...."); System.exit(0); } }; PopupMenu popup=new PopupMenu(); MenuItem defaultItem=new MenuItem("Exit"); defaultItem.addActionListner(exitListner); popup.add(defaultItem); defaultItem=new MenuItem("Open"); defaultItem.addActionListner(new ActionListner() { public voID actionPerformed(ActionEvent e) { setVisible(true); setExtendedState(JFrame.norMAL); } }); popup.add(defaultItem); trayIcon=new TrayIcon(image,"SystemTray Demo",popup); trayIcon.setimageautoSize(true); }else{ System.out.println("system tray not supported"); } addwindowstateListner(new windowstateListner() { public voID windowstateChanged(WindowEvent e) { if(e.getNewState()==ICONIFIED){ try { tray.add(trayIcon); setVisible(false); System.out.println("added to SystemTray"); } catch (AWTException ex) { System.out.println("unable to add to tray"); } } if(e.getNewState()==7){ try{ tray.add(trayIcon); setVisible(false); System.out.println("added to SystemTray"); }catch(AWTException ex){ System.out.println("unable to add to system tray"); } } if(e.getNewState()==MAXIMIZED_BOTH){ tray.remove(trayIcon); setVisible(true); System.out.println("Tray icon removed"); } if(e.getNewState()==norMAL){ tray.remove(trayIcon); setVisible(true); System.out.println("Tray icon removed"); } } }); setIconImage(Toolkit.getDefaultToolkit().getimage("Duke256.png")); setVisible(true); setSize(300,200); setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); } public static voID main(String[] args){ new HIDetoSystemTray(); } }

这是工作程序!

myFrame#getExtendedState和myFrame#setExtendedState

基本上是更好地多重播放这些ExtendedStates

frame.setExtendedState(JFrame.ICONIFIED); frame.setExtendedState(frame.getExtendedState() | JFrame.ICONIFIED); frame.setExtendedState(JFrame.norMAL); frame.setExtendedState(frame.getExtendedState() & (~JFrame.ICONIFIED));

编辑1。

例如

import java.awt.event.ActionEvent; import javax.swing.*; public class WindowGCDemo1 { private javax.swing.Timer timer = null; private int count = 0; private JFrame myFrame = new JFrame(); public WindowGCDemo1() { myFrame = new JFrame(); myFrame.setLocation(150,150); myFrame.setSize(200,400); myFrame.setVisible(true); System.out.println(myFrame.getExtendedState()); start(); } private voID start() { timer = new javax.swing.Timer(1000,updateCol()); timer.start(); } public Action updateCol() { return new AbstractAction("Set Delay") { private static final long serialVersionUID = 1L; @OverrIDe public voID actionPerformed(ActionEvent e) { timer.stop(); myFrame.setExtendedState(JFrame.ICONIFIED); System.out.println(myFrame.getExtendedState()); count++; startAgain(); } }; } private voID startAgain() { timer = new javax.swing.Timer(1000,updateColAgain()); timer.start(); } public Action updateColAgain() { return new AbstractAction("Set Delay") { private static final long serialVersionUID = 1L; @OverrIDe public voID actionPerformed(ActionEvent e) { timer.stop(); myFrame.setExtendedState(JFrame.norMAL); System.out.println(myFrame.getExtendedState()); count++; if (count > 5) { timer.stop(); stop(); } start(); } }; } private voID stop() { System.out.println("--------------------------------------------"); System.out.println("System Exit"); System.exit(0); } public static voID main(String[] args) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { WindowGCDemo1 windowGCDemo = new WindowGCDemo1(); } }); } }

编辑2。

对于SystemTry,你必须设置你的JFrame#setDefaultCloSEOperation(JFrame.DO_nothing_ON_CLOSE); 并从jpopupmenu正确的JMenuItem(s) ,只是JFrame#setVisible(true);

检查文档中的TryIconDemo。 这个链接包含一小段代码。 复制并在您的IDE上运行它。

https://docs.oracle.com/javase/tutorial/displayCode.HTML?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/TrayIconDemo.java

/* * copyright (c) 1995,2008,Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms,with or without * modification,are permitted provIDed that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice,this List of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice,this List of conditions and the following disclaimer in the * documentation and/or other materials provIDed with the distribution. * * - Neither the name of Oracle or the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE copYRIGHT HolDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING,BUT NOT liMITED TO,* THE IMPLIED WARRANTIES OF MERCHANTABIliTY AND fitness FOR A PARTIculaR * PURPOSE ARE disCLaimED. IN NO EVENT SHALL THE copYRIGHT OWNER OR * CONTRIBUTORS BE liABLE FOR ANY DIRECT,INDIRECT,INCIDENTAL,SPECIAL,* EXEMPLARY,OR CONSEQUENTIAL damAGES (INCLUDING,* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA,OR * PROFITS; OR BUSInesS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * liABIliTY,WHETHER IN CONTRACT,STRICT liABIliTY,OR TORT (INCLUDING * NEGliGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE,EVEN IF ADVISED OF THE POSSIBIliTY OF SUCH damAGE. */ package misc; /* * TrayIconDemo.java */ import java.awt.*; import java.awt.event.*; import java.net.URL; import javax.swing.*; public class TrayIconDemo { public static voID main(String[] args) { /* Use an appropriate Look and Feel */ try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.windowsLookAndFeel"); //UIManager.setLookAndFeel("javax.swing.plaf.Metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printstacktrace(); } catch (illegalaccessexception ex) { ex.printstacktrace(); } catch (InstantiationException ex) { ex.printstacktrace(); } catch (ClassNotFoundException ex) { ex.printstacktrace(); } /* Turn off Metal's use of bold Fonts */ UIManager.put("swing.boldMetal",Boolean.FALSE); //Schedule a job for the event-dispatching thread: //adding TrayIcon. SwingUtilitIEs.invokelater(new Runnable() { public voID run() { createAndShowGUI(); } }); } private static voID createAndShowGUI() { //Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif","tray icon")); final SystemTray tray = SystemTray.getSystemTray(); // Create a popup menu components MenuItem aboutItem = new MenuItem("About"); CheckBoxMenuItem cb1 = new CheckBoxMenuItem("Set auto size"); CheckBoxMenuItem cb2 = new CheckBoxMenuItem("Set tooltip"); Menu displayMenu = new Menu("display"); MenuItem errorItem = new MenuItem("Error"); MenuItem warningItem = new MenuItem("Warning"); MenuItem infoItem = new MenuItem("Info"); MenuItem noneItem = new MenuItem("None"); MenuItem exitItem = new MenuItem("Exit"); //Add components to popup menu popup.add(aboutItem); popup.addSeparator(); popup.add(cb1); popup.add(cb2); popup.addSeparator(); popup.add(displayMenu); displayMenu.add(errorItem); displayMenu.add(warningItem); displayMenu.add(infoItem); displayMenu.add(noneItem); popup.add(exitItem); trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { System.out.println("TrayIcon Could not be added."); return; } trayIcon.addActionListner(new ActionListner() { public voID actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"This dialog Box is run from System Tray"); } }); aboutItem.addActionListner(new ActionListner() { public voID actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"This dialog Box is run from the About menu item"); } }); cb1.addItemListner(new ItemListner() { public voID itemStateChanged(ItemEvent e) { int cb1ID = e.getStateChange(); if (cb1ID == ItemEvent.SELECTED){ trayIcon.setimageautoSize(true); } else { trayIcon.setimageautoSize(false); } } }); cb2.addItemListner(new ItemListner() { public voID itemStateChanged(ItemEvent e) { int cb2ID = e.getStateChange(); if (cb2ID == ItemEvent.SELECTED){ trayIcon.settooltip("Sun TrayIcon"); } else { trayIcon.settooltip(null); } } }); ActionListner Listener = new ActionListner() { public voID actionPerformed(ActionEvent e) { MenuItem item = (MenuItem)e.getSource(); //TrayIcon.MessageType type = null; System.out.println(item.getLabel()); if ("Error".equals(item.getLabel())) { //type = TrayIcon.MessageType.ERROR; trayIcon.displayMessage("Sun TrayIcon Demo","This is an error message",TrayIcon.MessageType.ERROR); } else if ("Warning".equals(item.getLabel())) { //type = TrayIcon.MessageType.WARNING; trayIcon.displayMessage("Sun TrayIcon Demo","This is a warning message",TrayIcon.MessageType.WARNING); } else if ("Info".equals(item.getLabel())) { //type = TrayIcon.MessageType.INFO; trayIcon.displayMessage("Sun TrayIcon Demo","This is an info message",TrayIcon.MessageType.INFO); } else if ("None".equals(item.getLabel())) { //type = TrayIcon.MessageType.NONE; trayIcon.displayMessage("Sun TrayIcon Demo","This is an ordinary message",TrayIcon.MessageType.NONE); } } }; errorItem.addActionListner(Listener); warningItem.addActionListner(Listener); infoItem.addActionListner(Listener); noneItem.addActionListner(Listener); exitItem.addActionListner(new ActionListner() { public voID actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } }); } //Obtain the image URL protected static Image createImage(String path,String description) { URL imageURL = TrayIconDemo.class.getResource(path); if (imageURL == null) { System.err.println("Resource not found: " + path); return null; } else { return (new ImageIcon(imageURL,description)).getimage(); } } }

总结

以上是内存溢出为你收集整理的如何隐藏任务栏系统托盘中的JFrame全部内容,希望文章能够帮你解决如何隐藏任务栏系统托盘中的JFrame所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1228715.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存