1.背景图片
1.1 利用JLabel加载图片
1.2 重载JPanel的paintComponent(Graphics g)方法
2.托盘
(1)获取系统托盘(实例化)
SystemTray systemTray = SystemTray.getSystemTray();
(2)创建一个当前的程序托盘容器
ImageIcon icon = new ImageIcon("images\\QQ.png");
Image image = icon.getImage();
TrayIcon trayIcon = new TrayIcon(image);
setState(NORMAL);//还原窗口
3.右击菜单
PopupMenu popupMenu = new PopupMenu();
MenuItem menuItemA = new MenuItem("打开");
MenuItem menuItemB = new MenuItem("退出");
1.JPanel 背景图片的添加
import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /*** 1.背景图片的的设置* Swing提供了2种解决方案* (1)利用JLabel组件进行操作* (2)重绘JPanel面板(重点)常见的**/ public class JPanelBGDemo extends JFrame {//(1)利用JLabel组件进行操作//1.新建一个中间组件Jpanelprivate JPanel jpa = new JPanel(null);//2.创建一个JLabel标签实现装载一张图片//图片存放到Jlabel组件中必要要使用ImageIcon对象(存储的图片路径private ImageIcon ima = new ImageIcon("images\\草地.gif");//3.将创建好的imgeIcon对象存储到JLabel组件中private JLabel jla = new JLabel(ima);/*** 问题:使用JLabel组件添加背景图后,继续往窗体中添加其他组件* * 在JLabel组件上,否则会覆盖。*/public JPanelBGDemo() {this.setTitle("");this.setSize(841,310);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);//设置jla的坐标 0 0 宽度和高度与窗体的宽度和高度保持一致即可jla.setBounds(0, 0, 841, 310);JButton jButton = new JButton("按钮");jButton.setBounds(20, 20, 50, 20);jla.add(jButton);jpa.add(jla);this.getContentPane().add(jpa);this.setVisible(true);}public static void main(String[] args) {new JPanelBGDemo();} }
2.重绘JPanel面板 常用
import java.awt.Image;import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; /*** 1.背景图片的的设置* Swing提供了2种解决方案* (1)利用JLabel组件进行操作* (2)重绘JPanel面板(重点)常见的**/ public class BackGroundDemo extends JFrame {// 重绘JPanel面板(重点)常见的//直接在实例化JPanel对象时立刻重构该类private JPanel jpa = new JPanel(){//重构背景的方法:不带s 黄色图标 paintCprotected void paintComponent(java.awt.Graphics g) {ImageIcon ima = new ImageIcon("images\\R-C.jpg");Image image = ima.getImage();//将ImageIcon对象转换成Image对象g.drawImage(image, 0, 0, getWidth(), getHeight(), this);};};public BackGroundDemo() {this.setTitle("");this.setSize(600,500);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);this.getContentPane().add(jpa);this.setVisible(true);}public static void main(String[] args) {new BackGroundDemo();} }
3.程序托盘的使用
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.WindowEvent; import java.awt.event.WindowListener; import java.io.IOException;import javax.swing.ImageIcon; import javax.swing.JFrame;public class TrayIconDemo extends JFrame {/*** 程序托盘的使用* 1.调用到系统托盘 SystemTray* 2.创建一个当前程序的托盘 TrayIcon* 3.当程序被鼠标点击最小化时,将程序的托盘添加到系统托盘中即可实现。*///1.调用到系统托盘private SystemTray st = SystemTray.getSystemTray();//2.创建一个当前程序的托盘 TrayIconprivate ImageIcon ima = new ImageIcon("images\\炸弹.gif"); // private Image image = ima.getImage().getScaledInstance(10, 10, Image.SCALE_DEFAULT);private Image image = ima.getImage();TrayIcon ti = new TrayIcon(image);//给程序托盘添加一个右击菜单//1.实例化右击菜单总容器private PopupMenu pm = new PopupMenu();//2.实例化右击菜单中的子选项组件private MenuItem mia = new MenuItem("打开");private MenuItem mib = new MenuItem("打开计算器");private MenuItem mic = new MenuItem("关闭程序");public TrayIconDemo() {this.setTitle("");this.setSize(300, 200);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);//3.将子选项组件添加到右击菜单中去pm.add(mia);pm.add(mib);pm.add(mic);//4.将右击菜单总容器设置到程序托盘中即可实现ti.setPopupMenu(pm);mia.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//打开时 窗口被还原 利用窗体对象去实现,窗体对象使用this调 // NORMAL 正常TrayIconDemo.this.setState(NORMAL);}});ti.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubTrayIconDemo.this.setState(NORMAL);}});mib.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//打开计算器Runtime rt = Runtime.getRuntime();try {rt.exec("calc");} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});mic.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});ti.setImageAutoSize(true);//自动调整图片的大小//设置窗体事件this.addWindowListener(new WindowListener() {@Overridepublic void windowOpened(WindowEvent e) {}@Overridepublic void windowIconified(WindowEvent e) {//将程序托盘添加到系统托盘中显示即可try {st.add(ti);} catch (AWTException e1) {e1.printStackTrace();}}//窗口被还原@Overridepublic void windowDeiconified(WindowEvent e) {//从系统托盘中移除程序托盘st.remove(ti);}@Overridepublic void windowDeactivated(WindowEvent e) {}@Overridepublic void windowClosing(WindowEvent e) {}@Overridepublic void windowClosed(WindowEvent e) {}@Overridepublic void windowActivated(WindowEvent e) {}});this.setVisible(true);}public static void main(String[] args) {new TrayIconDemo();} }