java Swing 实现将系统最小化到系统托盘
2010-01-14 14:01
483 查看
源码:
/*
* MainFrame.java
*
* Created on 2008年9月20日, 上午11:44
*/
package
com.topking.tray.ui;
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.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.WindowEvent;
import
java.awt.event.WindowListener;
import
javax.swing.ImageIcon;
/**
*
* @author lzkj
*/
public
class
MainFrame
extends
javax.swing.JFrame
implements
ActionListener, WindowListener{
// Variables declaration - do not modify
private
javax.swing.JLabel L_img;
private
javax.swing.JLabel L_img2;
private
PopupMenu pop;
private
MenuItem open,close;
private
TrayIcon trayicon;
// End of variables declaration
/** Creates new form MainFrame */
public
MainFrame() {
this
.setTitle(
"Java实现系统托盘示例"
);
this
.setLocation(
300
,
300
);
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings
(
"unchecked"
)
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private
void
initComponents() {
// L_img = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean1.png")));
// L_img2 = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean2.png")));
L_img = new
javax.swing.JLabel(
new
ImageIcon(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/netbean1.png"
)));
L_img2 = new
javax.swing.JLabel(
new
ImageIcon(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/netbean2.png"
)));
pop = new
PopupMenu();
open = new
MenuItem(
"打开"
);
open.addActionListener(this
);
close = new
MenuItem(
"关闭"
);
close.addActionListener(this
);
pop.add(open);
pop.add(close);
if
(SystemTray.isSupported()){
SystemTray tray = SystemTray.getSystemTray();
Image icon = this
.getToolkit().getImage(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/user_edit.png"
));
trayicon = new
TrayIcon(icon,
"系统托盘示例(java)"
,pop);
trayicon.addMouseListener(new
MouseListener(){
public
void
mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if
(e.getClickCount()==
2
){
openFrame();
}
}
public
void
mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
try
{
tray.add(trayicon);
} catch
(AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(L_img2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380
, Short.MAX_VALUE)
.addComponent(L_img, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380
, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(L_img)
.addGap(29
,
29
,
29
)
.addComponent(L_img2, javax.swing.GroupLayout.PREFERRED_SIZE, 222
, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(39
, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public
static
void
main(String args[]) {
java.awt.EventQueue.invokeLater(new
Runnable() {
public
void
run() {
new
MainFrame().setVisible(
true
);
}
});
}
public
void
actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if
(e.getSource()==open){
openFrame();
}
if
(e.getSource()==close){
System.exit(-1
);
}
}
public
void
openFrame(){
this
.setVisible(
true
);
this
.setAlwaysOnTop(
true
);
}
public
void
windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
this
.setVisible(
false
);
this
.dispose();
}
public
void
windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
//窗口最小化
public
void
windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
this
.dispose();
}
public
void
windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
/*
* MainFrame.java
*
* Created on 2008年9月20日, 上午11:44
*/
package
com.topking.tray.ui;
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.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.WindowEvent;
import
java.awt.event.WindowListener;
import
javax.swing.ImageIcon;
/**
*
* @author lzkj
*/
public
class
MainFrame
extends
javax.swing.JFrame
implements
ActionListener, WindowListener{
// Variables declaration - do not modify
private
javax.swing.JLabel L_img;
private
javax.swing.JLabel L_img2;
private
PopupMenu pop;
private
MenuItem open,close;
private
TrayIcon trayicon;
// End of variables declaration
/** Creates new form MainFrame */
public
MainFrame() {
this
.setTitle(
"Java实现系统托盘示例"
);
this
.setLocation(
300
,
300
);
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings
(
"unchecked"
)
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private
void
initComponents() {
// L_img = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean1.png")));
// L_img2 = new javax.swing.JLabel(new ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean2.png")));
L_img = new
javax.swing.JLabel(
new
ImageIcon(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/netbean1.png"
)));
L_img2 = new
javax.swing.JLabel(
new
ImageIcon(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/netbean2.png"
)));
pop = new
PopupMenu();
open = new
MenuItem(
"打开"
);
open.addActionListener(this
);
close = new
MenuItem(
"关闭"
);
close.addActionListener(this
);
pop.add(open);
pop.add(close);
if
(SystemTray.isSupported()){
SystemTray tray = SystemTray.getSystemTray();
Image icon = this
.getToolkit().getImage(
this
.getClass().getClassLoader().getResource(
"com/topking/tray/images/user_edit.png"
));
trayicon = new
TrayIcon(icon,
"系统托盘示例(java)"
,pop);
trayicon.addMouseListener(new
MouseListener(){
public
void
mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if
(e.getClickCount()==
2
){
openFrame();
}
}
public
void
mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public
void
mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
try
{
tray.add(trayicon);
} catch
(AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(L_img2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380
, Short.MAX_VALUE)
.addComponent(L_img, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380
, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(L_img)
.addGap(29
,
29
,
29
)
.addComponent(L_img2, javax.swing.GroupLayout.PREFERRED_SIZE, 222
, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(39
, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public
static
void
main(String args[]) {
java.awt.EventQueue.invokeLater(new
Runnable() {
public
void
run() {
new
MainFrame().setVisible(
true
);
}
});
}
public
void
actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if
(e.getSource()==open){
openFrame();
}
if
(e.getSource()==close){
System.exit(-1
);
}
}
public
void
openFrame(){
this
.setVisible(
true
);
this
.setAlwaysOnTop(
true
);
}
public
void
windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
this
.setVisible(
false
);
this
.dispose();
}
public
void
windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public
void
windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
//窗口最小化
public
void
windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
this
.dispose();
}
public
void
windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
相关文章推荐
- java swing 点击关闭按钮最小化到系统托盘的实现
- Java JFrame设计及最小化到系统托盘的实现
- Java swing 仿QQ实现系统托盘图标(下)
- java实现最小化到系统托盘图标的代码
- WinForm实现最小化到系统托盘方法实例详解
- .net实例:WINFORM是实现窗体最小化到系统托盘中
- java中系统托盘的实现
- Java实现把窗体隐藏到系统托盘方法
- java实现系统托盘
- C# 实现WinForm窗口最小化到系统托盘代码
- C#实现窗口最小化到系统托盘
- C#实现窗口最小化到系统托盘
- C#实现WinForm窗口最小化到系统托盘
- java(swing)+ mysql实现学生信息管理系统源码
- C#实现winform软件开机自动启动并最小化到系统托盘
- .net实例:WINFORM是实现窗体最小化到系统托盘中
- C#实现系统托盘,C#窗体最小化时隐藏为任务栏图标
- java swing mysql实现的ktv点歌系统项目源码附带视频教程
- C#最小化到系统托盘实现
- java最小化到托盘显示图标实现