javaAnfänger3
Grünschnabel
Bei einem Doppelklick werden die Ausgaben in der Konsole geschrieben. Bei einem normalen Klick jedoch nicht.
Hier mal der gesamte Code:
Hier mal der gesamte Code:
Java:
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
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.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.SwingUtilities;
import org.omg.CORBA.PRIVATE_MEMBER;
public class TrayIconFeature {
TerminSoftware terminsoftware;
public TrayIconFeature(TerminSoftware terminSoftware) {
this.terminsoftware = terminSoftware;
{
TrayIcon trayIcon = null;
if (!SystemTray.isSupported()) {
// TODO Meldung ausgeben, bevor Programm beendet wird
System.exit(-1);
}
Image imageIcon = Toolkit.getDefaultToolkit().getImage("res/appdefault.png");
SystemTray tray = SystemTray.getSystemTray();
PopupMenu popup = new PopupMenu();
MenuItem anzeigen = new MenuItem(Konstanten.PROGRAMMNAME + " anzeigen");
MenuItem ausblenden = new MenuItem(Konstanten.PROGRAMMNAME + " ausblenden");
MenuItem beenden = new MenuItem(Konstanten.PROGRAMMNAME + " beenden");
popup.add(anzeigen);
popup.add(ausblenden);
popup.addSeparator();
popup.add(beenden);
trayIcon = new TrayIcon(imageIcon, "TerminSoftware", popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
class windowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
popup.remove(ausblenden);
popup.insert(anzeigen,0);
}
@Override
public void windowOpened(WindowEvent e) {
popup.remove(anzeigen);
popup.insert(ausblenden,0);
}
@Override
public void windowDeiconified(WindowEvent e) {
terminSoftware.setVisible(true);
popup.remove(anzeigen);
popup.insert(ausblenden,0);
}
@Override
public void windowIconified(WindowEvent e) {
terminSoftware.setVisible(false);
popup.remove(ausblenden);
popup.insert(anzeigen,0);
}
}
terminSoftware.addWindowListener(new windowListener());
class IconListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
if(terminSoftware.isVisible()) {
terminSoftware.setVisible(false);
System.out.println("Software ist nicht eingeblendet");
}
else {
terminSoftware.setVisible(true);
System.out.println("Software ist eingeblendet");
}
}
}
trayIcon.addActionListener(new IconListener());
class anzeigenListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
terminSoftware.setVisible(true);
popup.remove(anzeigen);
popup.insert(ausblenden,0);
}
}
anzeigen.addActionListener(new anzeigenListener());
class ausblendenListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
terminSoftware.setVisible(false);
popup.remove(ausblenden);
popup.insert(anzeigen,0);
}
}
ausblenden.addActionListener(new ausblendenListener());
class closeProgram implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}
beenden.addActionListener(new closeProgram());
}
}
}
Java:
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class TerminSoftware extends JFrame {
private static final long serialVersionUID = 1L;
public TerminSoftware() {
new TrayIconFeature(this);
pack();
}
}
Java:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.omg.CORBA.portable.InputStream;
public class TerminSoftwareDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new TerminSoftware();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
try {
// ImageIcon oben links
frame.setIconImage(ImageIO.read(new File("res/appdefault.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}
frame.setTitle("Termin Software");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Zuletzt bearbeitet: