我对托盘有疑问。 该计划有效,但存在问题。
应该是:
当我左键单击TrayIcon(TNA)时,JFrame应该消失。 当我再次点击它时,应该出现JFrame。
目前:
当我点击TrayIcon时,JFrame消失并再次快速出现,之后它可以正常工作。 但是,当我第一次点击它时,它会消失并出现。
package IReminderMain;
import javax.swing.SwingUtilities;
import GUI.IReminderMainFrame;
public class IReminderStart {
public static void main(String[] args) {
/**
* New EDT
* */
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try{
//New JFrame Main
new IReminderMainFrame();
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}
package GUI;
import GUI.ImageIoReader;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Tray extends MouseAdapter {
private boolean visible;
private boolean vis;
private Image img;
private JFrame neu;
String i = new String("img/IReminderIcon.png");
MouseListener mouseL = new Mouse();
private void erstelleImage(){
try {
img = ImageIO.read(ImageIoReader.class.getClassLoader().getResourceAsStream(i));
System.out.println("ImageIoRead Tray: Image erstellt");
} catch (IOException e) {
//TODO throw to user input
e.printStackTrace();
}
}
//--------------------------------------------Konstruktor------------------------------------------------------------
public Tray(boolean visible, JFrame neu, boolean vis) {
System.out.println("Tray: Konstruktor aufgerufen");
erstelleImage();
this.visible = visible;
this.neu = neu;
this.vis = vis;
TrayIcon trayIcon = null;
//--------------------------------------------if------------------------------------------------------------
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
//------------------------------------------ActionListener---------------------------------------------------------------------
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
//------------------------------------------/ActionListener---------------------------------------------------------------------
PopupMenu popup = new PopupMenu("IReminder");
MenuItem beenden = new MenuItem("Beenden");
beenden.addActionListener(listener);
popup.add(beenden);
trayIcon = new TrayIcon(img, "IReminder", popup);
//------------------------------------------MouseListener---------------------------------------------------------------------
MouseListener mouseListener = new Mouse() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Tray: Mouse Clicked");
boolean visible = getvisFrame();
System.out.println("Tray: getvisFrame"+ getVisibleFrame());
if (visible == true) {
getFrame().setVisible(false);
System.out.println("Tray: false");
setvisFrame(false);
} else {
System.out.println("Tray: true");
getFrame().setVisible(true);
setvisFrame(true);
}
}
};
//------------------------------------------/MouseListener---------------------------------------------------------------------
trayIcon.addMouseListener(mouseListener);
try {
System.out.println("Tray: Try");
trayIcon.setImageAutoSize(true);
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
} else {
}
//--------------------------------------------/Ende if------------------------------------------------------------
}
//--------------------------------------------/Konstruktor------------------------------------------------------------
public boolean getVisibleFrame() {
System.out.println("Tray: Getter Visibility Frame");
return visible;
}
public JFrame getFrame(){
System.out.println("Tray: Getter Frame");
return neu;
}
public boolean getvisFrame(){
return vis;
}
public void setvisFrame(boolean vis){
System.out.println("Tray: Getter Frame");
this.vis = vis;
}
public void setFrame(JFrame frame){
System.out.println("Tray: Getter Frame");
neu = frame;
}
}
package GUI;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("The Window is closing but the program is still running!");
}
}
package GUI;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Mouse extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
}
}
package GUI;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
public class Look {
private LookAndFeel look;
private LookAndFeel newLook;
/**
* Konstruktor: Erstellt und aktiviert das Look and Feel
* */
public Look(LookAndFeel look){
setLookAndFeel(look);
activateLookAndFeel();
System.out.println("Look: Look and Feel done");
}
/**
* Methode: Setzt das Look and Feel
* */
public void activateLookAndFeel(){
try {
UIManager.setLookAndFeel(newLook = look);
System.out.println("Look: Look and Feel activated");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Setter: Setzt das Objekt new Look and Feel der Variable look
* */
private void setLookAndFeel(LookAndFeel look){
this.look = look;
System.out.println("Look: Look and Feel set");
}
public LookAndFeel getLookAndFeel(){
return newLook;
}
}
package GUI;
import javax.swing.JFrame;
public abstract class JWindows extends JFrame {
private static final long serialVersionUID = -8698706321873075551L;
public abstract void getLook();
public abstract void getNewTitle();
public abstract void getCloseOperation();
public abstract void getNewLayout();
public abstract void getVisible();
public abstract void getIcon();
}
package GUI;
import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class IReminderMainFrame extends JWindows {
private static final long serialVersionUID = -21323574593098624L;
boolean frameVisible = getVisableReminder();
private WindowListener wListener;
public IReminderMainFrame(){
getLook();
getNewTitle();
getNewLayout();
ImagePath i = new ImagePath("img/IReminderIcon.png");
setIconImage(i.getImage());
getIcon();
setSize(500, 500);
setLocationRelativeTo(null);
getVisible();
addWindowListener(wListener);
}
private boolean getVisableReminder() {
return this.isVisible();
}
@Override
public void getLook() {
NimbusLookAndFeel nimbus = new NimbusLookAndFeel();
new Look(nimbus);
}
@Override
public void getNewTitle() {
setTitle("IReminder");
}
@Override
public void getCloseOperation() {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
@Override
public void getNewLayout() {
setLayout(new BorderLayout());
}
@Override
public void getVisible() {
setVisible(true);
}
private void setListenerFrame (WindowListener listener){
this.wListener = listener;
}
@Override
public void getIcon() {
new Tray(isVisible(), this, frameVisible);
//---------------------------------------WindowListener-----------------------------------------------------------
WindowListener frameListener = new MyWindowListener() {
@Override
public void windowClosing(WindowEvent e) {
int answer = JOptionPane
.showConfirmDialog(
IReminderMainFrame.this,
"Das Programm wird im Hintergrund fortgesetzt \nFenster schliessen?",
"Programm wird nicht beendet",
JOptionPane.YES_OPTION);
if (answer == JOptionPane.YES_OPTION) {
System.out
.println("Das Fenster wird geschlossen, jedoch wird das Programm im Hintergrund noch ausgeführt!");
IReminderMainFrame.this.setVisible(false);
}
System.out.println("Fenster nicht geschlossen");
}
};
setListenerFrame(frameListener);
}
}
package GUI;
public class ImagePath extends ImageIoReader {
/**
* Konstruktor: ImagePath sendet den String zu ImageIoReader, das Image kann man sich
* dann über die Methode getImage(); sich von hier oder der Vaterklasse ziehen.
* */
public ImagePath(String s){
/**
* Sendet den String zu ImageIoReader Klasse
* */
super (s);
}
}
package GUI;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageIoReader {
private String i;
private BufferedImage img;
/**
* Konstruktor: übergibt den Pfad des Images and die Methode und erstellt dann das img
* */
public ImageIoReader(String i){
setImage(i);
createImage();
System.out.println("ImageIoRead: Konstruktor ImageIoRead ausgeführt");
}
/**
* Methode: erstellt das img und speichert es in die Variable ab
* */
private void createImage(){
try {
img = ImageIO.read(ImageIoReader.class.getClassLoader().getResourceAsStream(i));
System.out.println("ImageIoRead: Image erstellt");
} catch (IOException e) {
//TODO throw to user input
e.printStackTrace();
}
}
/**
* Braucht ein String der den Pfad des Img übergibt
* */
private void setImage(String i){
this.i = i;
System.out.println("ImageIoRead: Imagepfad in Variable i gespeichert");
}
/**
* Gibt ein Image zurück an den aufrufer
* */
public BufferedImage getImage(){
System.out.println("ImageIoRead: Image aufgerufen");
return img;
}
}
这是重要代码的一部分:
package GUI;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Tray extends MouseAdapter {
private boolean visible;
private boolean vis;
private Image img;
private JFrame neu;
String i = new String("img/IReminderIcon.png");
MouseListener mouseL = new Mouse();
//Start Constructor----------------------------------------------------------------------------------------------------------------------
public Tray(boolean visible, JFrame neu, boolean vis) {
System.out.println("Tray: Konstruktor aufgerufen");
erstelleImage();
this.visible = visible; //IReminderMainFrame isVisible()
this.neu = neu;
this.vis = vis; //IReminderMainFrame from method to a variable isVisible()
TrayIcon trayIcon = null;
//Start if--------------------------------------------------------------------------------------------------------
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
//Start ActionListener---------------------------------------------------------------------------------------------------------------
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
//***End ActionListener---------------------------------------------------------------------------------------------------------------
PopupMenu popup = new PopupMenu("IReminder");
MenuItem beenden = new MenuItem("Beenden");
beenden.addActionListener(listener);
popup.add(beenden);
trayIcon = new TrayIcon(img, "IReminder", popup);
//Start MouseListener---------------------------------------------------------------------------------------------------------------
MouseListener mouseListener = new Mouse() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Tray: Mouse Clicked");
boolean visible = getvisFrame();
System.out.println("Tray: getvisFrame"+ getVisibleFrame());
if (visible == true) {
getFrame().setVisible(false);
System.out.println("Tray: false");
setvisFrame(false);
} else {
System.out.println("Tray: true");
getFrame().setVisible(true);
setvisFrame(true);
}
}
};
//***End MouseListener---------------------------------------------------------------------------------------------------------------
trayIcon.addMouseListener(mouseListener);
try {
System.out.println("Tray: Try");
trayIcon.setImageAutoSize(true);
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
} else {
}
//***End if--------------------------------------------------------------------------------------------------------------------------------
}
//***Constructor end----------------------------------------------------------------------------------------------------------------------
//Start instance method--------------------------------------------------------------------------------------------------------------------------
private void erstelleImage(){
try {
img = ImageIO.read(ImageIoReader.class.getClassLoader().getResourceAsStream(i));
System.out.println("ImageIoRead Tray: Image erstellt");
} catch (IOException e) {
//TODO throw to user input
e.printStackTrace();
}
}
public boolean getVisibleFrame() {
System.out.println("Tray: Getter visible Frame");
return visible;
}
public JFrame getFrame(){
System.out.println("Tray: Getter JFrame");
return neu;
}
public boolean getvisFrame(){
return vis;
}
public void setvisFrame(boolean vis){
System.out.println("Tray: Setter vis JFrame");
this.vis = vis;
}
public void setFrame(JFrame frame){
System.out.println("Tray: Setter JFrame");
neu = frame;
}
//***End instance method--------------------------------------------------------------------------------------------------------
}
我真的希望你能帮助我,并感谢所有的帮助! PS:对不起我的英语。
编辑代码(sysout) 这是我运行并在trayicon上单击某个时间的sysout
Look: Look and Feel set
Look: Look and Feel activated
Look: Look and Feel done
ImageIoRead: Imagepfad in Variable i gespeichert
ImageIoRead: Image erstellt
ImageIoRead: Konstruktor ImageIoRead ausgeführt
ImageIoRead: Image aufgerufen
Tray: Konstruktor aufgerufen
ImageIoRead Tray: Image erstellt
Tray: Try
Tray: Mouse Clicked
Tray: Getter visible Frame
Tray: getvisFramefalse
Tray: true
Tray: Getter JFrame
Tray: Setter vis JFrame
Tray: Mouse Clicked
Tray: Getter visible Frame
Tray: getvisFramefalse
Tray: Getter JFrame
Tray: false
Tray: Setter vis JFrame
Tray: Mouse Clicked
Tray: Getter visible Frame
Tray: getvisFramefalse
Tray: true
Tray: Getter JFrame
Tray: Setter vis JFrame
Tray: Mouse Clicked
Tray: Getter visible Frame
Tray: getvisFramefalse
Tray: Getter JFrame
Tray: false
Tray: Setter vis JFrame