事件处理
ActionListener点击事件(动作监听器)
WindowListener窗体事件
windowOpened(WindowEvent e) 窗体被打开
windowClosing(WindowEvent e) 窗体正在关闭
windowIconified(WindowEvent e) 窗体最小化
windowDeiconified(WindowEvent e) 窗体从最小化恢复
windowActivated(WindowEvent e) 窗体被选中
windowDeactivated(WindowEvent e) 取消窗体被选中
MouseListener/MouseMotionListener鼠标事件
mouseEntered(MouseEvent e) 鼠标移入组件
mouseExited(MouseEvent e) 鼠标移出组件
mousePressed(MouseEvent e) 鼠标按下
mouseReleased(MouseEvent e) 鼠标松开
mouseClicked(MouseEvent e) 鼠标点击
mouseMoved (MouseEvent) 鼠标移动
mouseDragged(MouseEvent) 鼠标拖动
KeyListener键盘事件
鼠标滑轮监听: mouseWheelMoved(MouseWheelEvent e) 滑轮滚动
完成窗口设置背景图片(2种方式)
1.第一种利用JLabel组件进行操作
private JPanel jpa = new JPanel(null);//空布局
private JLabel jla = new JLabel(new ImageIcon("images\\嘿嘿.gif"));
public work() {
this.setTitle("");
this.setSize(400,400);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
jla.setBounds(0,0,400,400);
jpa.add(jla);
this.getContentPane().add(jpa);
this.setVisible(true);
}
2.第二种重绘JPanel面板
private JPanel jpa = new JPanel() {//重构背景的方法
protected void paintComponent(java.awt.Graphics g) {
ImageIcon ima = new ImageIcon("images\\嘿嘿.gif");
//转换
Image image = ima.getImage();
g.drawImage(image,0,0,getWidth(), getHeight(), this);
};
};
public work() {
this.setTitle("");
this.setSize(400,400);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.getContentPane().add(jpa);
this.setVisible(true);
}
系统托盘及托盘右击菜单
//1.调用到系统托盘 SystemTray
private SystemTray st = SystemTray.getSystemTray();
//2.创建一个图书管理程序的托盘 TrayTcon
private ImageIcon ima = new ImageIcon("images\\哈哈.jpg");
private Image image = ima.getImage();
TrayIcon ti = new TrayIcon(image);
//实例化右击菜单总容器
private PopupMenu pm = new PopupMenu();
//实例化右击菜单中的子选项组件
private MenuItem mia = new MenuItem("打开页面");
private MenuItem mib = new MenuItem("关闭程序");
public work2() {
this.setTitle("");
this.setSize(400,400);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
//将子选择添加到右击菜单中
pm.add(mia);
pm.add(mib);
//将右击菜单总容器设置到程序托盘中
ti.setPopupMenu(pm);
ti.setImageAutoSize(true);
//打开页面
mia.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//打开时 窗体被还原 利用窗体对象去实现
work2.this.setState(NORMAL);//NORMAL 正常
}
});
//关闭程序
mib.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
ti.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
work2.this.setState(NORMAL);
}
});
//设置窗体事件
this.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {
//窗体最小化
//3.当程序被鼠标点击最小化时,将程序的托盘添加到系统托盘中
try {
st.add(ti);
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void windowDeiconified(WindowEvent e) {
//窗体还原
//从系统托盘中移除程序托盘
st.remove(ti);
}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowActivated(WindowEvent e) {}
});
this.setVisible(true);
}