java 任务栏图标

听说java6支持任务栏图标,于是写了个小程序测试下。
先看看截图吧:

1.那个最可爱的就是咱的了


2.右键点击下看看
java 任务栏图标

3.这是关于的界面
java 任务栏图标

4.看看程序的主界面吧
java 任务栏图标

5.计算器
java 任务栏图标

6.万年历
java 任务栏图标

7.测试用的
java 任务栏图标

好了,该看看源代码了:
(注:部分代码来自网络。)

MyFrame.java

  1. package test;

  2. WeatherPanel.java
    1. package test;

    2. import java.awt.BorderLayout;
    3. import java.awt.Graphics;
    4. import java.awt.Image;
    5. import java.awt.event.ActionEvent;
    6. import java.awt.event.ActionListener;
    7. import java.awt.event.KeyAdapter;
    8. import java.awt.event.KeyEvent;
    9. import java.awt.event.MouseAdapter;
    10. import java.awt.event.MouseEvent;
    11. import java.util.regex.Pattern;

    12. import javax.swing.ImageIcon;
    13. import javax.swing.JButton;
    14. import javax.swing.JOptionPane;
    15. import javax.swing.JPanel;
    16. import javax.swing.JProgressBar;
    17. import javax.swing.JTextField;

    18. public class WeatherPanel  extends JPanel implements ActionListener{
    19.     private static final long serialVersionUID = 1L;
    20.     private static WeatherPanel wp;
    21.     private Pattern cityP;
    22.     private JTextField jta=new JTextField(10);
    23.     private JButton button=new JButton("设定城市");
    24.     private JProgressBar loading_jpb=new JProgressBar();
    25.     private String city="上海";
    26.     private OpenUrl openUrl=new OpenUrl();
    27.     private Weather weather = null;
    28.     
    29.     public static WeatherPanel getInstance(){
    30.         if(wp==null)
    31.             wp=new WeatherPanel();
    32.         return wp;
    33.     }
    34.     
    35.     private WeatherPanel(){
    36.         jta.setText("上海");
    37.         init();
    38.         new Thread(){
    39.             public void run(){
    40.                 //weather=openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city);
    41.                 weather=openUrl.getWeatherByUrl("D:\\aaa\\tomcat6\\webapps\\aaa\\detail.html");
    42.                 WeatherPanel.getInstance().repaint();
    43.             }
    44.         }.start();
    45.     }
    46.     
    47.     private void init(){
    48.         this.setOpaque(false);
    49.         JPanel south=new JPanel();
    50.         south.setLayout(new BorderLayout());
    51.         JPanel panel=new JPanel();
    52.         panel.add(jta);
    53.         jta.addKeyListener(new KeyAdapter(){
    54.             public void keyPressed(KeyEvent e) {
    55.                 if(e.getKeyCode()==KeyEvent.VK_ENTER){
    56.                     paintWeather();
    57.                 }
    58.             }
    59.         });
    60.         panel.add(button);
    61.         south.add(panel,BorderLayout.SOUTH);
    62.         south.add(loading_jpb,BorderLayout.NORTH);
    63.         //button.addActionListener(this);
    64.         button.addMouseListener(new MouseAdapter(){
    65.             public void mouseClicked(MouseEvent e) {
    66.                 paintWeather();
    67.             }
    68.         });
    69.         this.setLayout(new BorderLayout());
    70.         this.add(south,BorderLayout.SOUTH);
    71.         loading_jpb.setVisible(true);
    72.         loading_jpb.setIndeterminate(true);
    73.     }
    74.     
    75.     protected void paintComponent(Graphics g){
    76.         int x=10,y=20;
    77.         if(weather==null){
    78. //          city="上海";
    79. //          weather = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city);
    80.             return;
    81.         }
    82.         g.drawString(weather.getCity()+":"+weather.getDate1(), x, y);
    83.         String weatherName1="";
    84.         String weatherName2="";
    85.         String legend1="";
    86.         String legend2="";
    87.         ImageIcon icon1=null,icon2=null;
    88.         Image img1=null,img2=null;
    89.         int t1=0;
    90.         if((t1=weather.getWeather1().indexOf("转"))!=-1){
    91.             weatherName1=weather.getWeather1().substring(0, t1);
    92.             weatherName2=weather.getWeather1().substring(t1+1, weather.getWeather1().length());
    93.             legend1="weather_legend/a"+openUrl.getWeatherNumber(weatherName1)+".gif";
    94.             legend2="weather_legend/a"+openUrl.getWeatherNumber(weatherName2)+".gif";
    95.             icon1=new ImageIcon(legend1);
    96.             icon2=new ImageIcon(legend2);
    97.             img1=icon1.getImage();
    98.             img2=icon2.getImage();
    99.             g.drawImage(img1, x, y+106051,null);
    100.             g.drawImage(img2, x+58, y+106051,null);
    101.         }else{
    102.             weatherName1=weather.getWeather1();
    103.             legend1="weather_legend/a"+openUrl.getWeatherNumber(weatherName1)+".gif";
    104.             icon1=new ImageIcon(legend1);
    105.             img1=icon1.getImage();
    106.             g.drawImage(img1, x+30, y+106051null);
    107.         }
    108.         g.drawString("天气:"+weather.getWeather1(), x, y+80);
    109.         g.drawString("气温:"+weather.getTemperature1(), x, y+80+25);
    110.         g.drawString("风力:"+weather.getWind1(), x, y+80+25+25);
    111.         
    112.         g.drawLine(18020180180);
    113.         x+=190;
    114.         g.drawString(weather.getCity()+":"+weather.getDate2(), x, y);
    115.         String weatherName3="";
    116.         String weatherName4="";
    117.         String legend3="";
    118.         String legend4="";
    119.         ImageIcon icon3=null,icon4=null;
    120.         Image img3=null,img4=null;
    121.         int t2=0;
    122.         if((t2=weather.getWeather2().indexOf("转"))!=-1){
    123.             weatherName3=weather.getWeather2().substring(0, t2);
    124.             weatherName4=weather.getWeather2().substring(t2+1, weather.getWeather2().length());
    125.             legend3="weather_legend/a"+openUrl.getWeatherNumber(weatherName3)+".gif";
    126.             legend4="weather_legend/a"+openUrl.getWeatherNumber(weatherName4)+".gif";
    127.             icon3=new ImageIcon(legend3);
    128.             icon4=new ImageIcon(legend4);
    129.             img3=icon3.getImage();
    130.             img4=icon4.getImage();
    131.             g.drawImage(img3, x, y+106051,null);
    132.             g.drawImage(img4, x+58, y+106051,null);
    133.         }else{
    134.             weatherName3=weather.getWeather2();
    135.             legend3="weather_legend/a"+openUrl.getWeatherNumber(weatherName3)+".gif";
    136.             icon3=new ImageIcon(legend3);
    137.             img3=icon3.getImage();
    138.             g.drawImage(img3, x+30, y+106051null);
    139.         }
    140.         g.drawString("天气:"+weather.getWeather2(), x, y+80);
    141.         g.drawString("气温:"+weather.getTemperature2(), x, y+80+25);
    142.         g.drawString("风力:"+weather.getWind2(), x, y+80+25+25);
    143.         //super.paintComponent(g);
    144.         loading_jpb.setVisible(false);
    145.         jta.setEditable(true);
    146.         button.setEnabled(true);
    147.     }
    148.     
    149.     /**
    150.      * 城市改变时,执行的操作
    151.      */
    152.     private void paintWeather(){
    153.         cityP = Pattern.compile("[^\\d\\w\\p{Punct}]{2,10}");
    154.         city=jta.getText().trim();
    155.         if (cityP.matcher(city).matches()) {
    156.             jta.setEditable(false);
    157.             button.setEnabled(false);
    158.             loading_jpb.setVisible(true);
    159.             new Thread(){
    160.                 public void run(){
    161.                     weather = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city);
    162. <l>
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Container;
  6. import java.awt.Cursor;
  7. import java.awt.Dimension;
  8. import java.awt.Font;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.MenuItem;
  12. import java.awt.PopupMenu;
  13. import java.awt.SystemTray;
  14. import java.awt.Toolkit;
  15. import java.awt.TrayIcon;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.MouseAdapter;
  19. import java.awt.event.MouseEvent;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.LinkedList;

  25. import javax.swing.ImageIcon;
  26. import javax.swing.JDialog;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JPanel;
  30. import javax.swing.JProgressBar;
  31. import javax.swing.JTabbedPane;
  32. import javax.swing.JTextPane;
  33. import javax.swing.JWindow;
  34. import javax.swing.UIManager;
  35. import javax.swing.UnsupportedLookAndFeelException;

  36. public class MyFrame extends JFrame implements ActionListener {
  37.     private static final long serialVersionUID = 1026416994451303162L;
  38.     private static MyFrame mf = null;
  39.     private JTabbedPane jtp = new JTabbedPane();
  40.     private JWindow windowSplash;

  41.     public static MyFrame getInstance() {
  42.         if (mf == null)
  43.             mf = new MyFrame();
  44.         return mf;
  45.     }

  46.     public MyFrame() {
  47.         super("改变窗体测试");
  48.         prepareSplash();
  49.         this.startSplash();
  50.         init();
  51.         systemTray();
  52.         this.stopSplash();
  53.     }

  54.     private void init() {
  55.         jtp.add("天气", WeatherPanel.getInstance());
  56.         jtp.add("计算器",MyCalculator.getInstance());
  57.         jtp.add("万年历",MyCalendar.getInstance());
  58.         jtp.add("测试", TestWindow.getInstance());
  59.         
  60.         //jtp.add("设置",SetCity.getInstance());
  61.         this.getContentPane().add(jtp, BorderLayout.CENTER);
  62.         jtp.getAccessibleContext().setAccessibleName("");
  63.         this.setSize(400350);
  64.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  65.         int h = (int) d.getHeight() / 3;
  66.         int w = (int) d.getWidth() / 3;
  67.         this.setLocation(w, h);
  68.         this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  69.         LinkedList<Image> icon = new LinkedList<Image>();
  70.         try {
  71.             icon.add(new ImageIcon(new URL("http://weather.tq121.com.cn/images/sun.gif")).getImage());
  72.         } catch (MalformedURLException e1) {
  73.             e1.printStackTrace();
  74.         }
  75.         this.setIconImages(icon);
  76.         this.addWindowListener(new WindowAdapter() {
  77.             public void windowClosing(WindowEvent e) {
  78.                 MyFrame.getInstance().setVisible(false);
  79.             }
  80.         });
  81.         this.setVisible(true);
  82.     }

  83.     private void prepareSplash(){
  84.         Toolkit toolkit=Toolkit.getDefaultToolkit();
  85.         windowSplash=new JWindow(this);
  86.         windowSplash.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  87.         Container container = windowSplash.getContentPane();
  88.         URL url = getClass().getResource("1.png"); // 图片的位置
  89.         if (url != null) {
  90.             container.add(new JLabel(new ImageIcon(url)), BorderLayout.CENTER); // 增加图片
  91.         }
  92.         JProgressBar progress=new JProgressBar();
  93.         progress.setVisible(true);
  94.         progress.setIndeterminate(true);
  95.         container.add(progress, BorderLayout.SOUTH);
  96.         Dimension scmSize = toolkit.getScreenSize();
  97.         windowSplash.setLocation(scmSize.width/2 , scmSize.height/2 );
  98.         windowSplash.pack();
  99.     }
  100.     
  101.     private void startSplash(){
  102.         windowSplash.setVisible(true);
  103.         windowSplash.toFront();
  104.     }

  105.     private void stopSplash(){ 
  106.         windowSplash.dispose();
  107.     }

  108.     
  109.     public void actionPerformed(ActionEvent e) {

  110.     }

  111.     public void systemTray() {
  112.         try {
  113.             if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘
  114.                 SystemTray st = SystemTray.getSystemTray();
  115.                 Image image = Toolkit.getDefaultToolkit().getImage(
  116.                         getClass().getResource("1.gif"));// 定义托盘图标的图片
  117.                 TrayIcon ti = new TrayIcon(image);
  118.                 ti.setToolTip("改变窗体测试");
  119.                 ti.addMouseListener(new MouseAdapter() {
  120.                     public void mouseClicked(MouseEvent e) {
  121.                         if (e.getButton() == MouseEvent.BUTTON1)// 鼠标左键单击,打开窗体
  122.                             MyFrame.getInstance().setVisible(true);
  123.                         MyFrame.getInstance().setExtendedState(JFrame.NORMAL);
  124.                     }
  125.                 });
  126.                 PopupMenu p = new PopupMenu("OK");
  127.                 MenuItem about = new MenuItem("关于");
  128.                 about.addActionListener(new ActionListener() {
  129.                     public void actionPerformed(ActionEvent e) {
  130.                         Thread t = new Thread(AboutUI.getInstance());
  131.                         t.start();
  132.                         AboutUI.getInstance().setVisible(true);
  133.                     }
  134.                 });
  135.                 p.add(about);
  136.                 p.addSeparator();
  137.                 MenuItem m1 = new MenuItem("打开");
  138.                 m1.addActionListener(new ActionListener() {
  139.                     public void actionPerformed(ActionEvent e) {
  140.                         MyFrame.getInstance().setVisible(true);
  141.                         MyFrame.getInstance().setExtendedState(JFrame.NORMAL);
  142.                     }
  143.                 });
  144.                 p.add(m1);
  145.                 MenuItem m2 = new MenuItem("最大化");
  146.                 m2.addActionListener(new ActionListener() {
  147.                     public void actionPerformed(ActionEvent e) {
  148.                         MyFrame.getInstance().setVisible(true);
  149.                         MyFrame.getInstance().setExtendedState(
  150.                                 JFrame.MAXIMIZED_BOTH);
  151.                     }
  152.                 });
  153.                 p.add(m2);
  154.                 p.addSeparator();
  155.                 MenuItem m = new MenuItem("退出");
  156.                 m.addActionListener(new ActionListener() {
  157.                     public void actionPerformed(ActionEvent e) {
  158.                         System.exit(0);
  159.                     }
  160.                 });
  161.                 p.add(m);
  162.                 ti.setPopupMenu(p); // 为托盘添加右键菜单
  163.                 st.add(ti);
  164.             }
  165.         } catch (Exception e) {

  166.         }

  167.     }

  168.     public static void main(String[] args) {
  169.         try {
  170.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  171. //          JFrame.setDefaultLookAndFeelDecorated(true);
  172.             MyFrame.getInstance();
  173.         } catch (ClassNotFoundException e) {
  174.             e.printStackTrace();
  175.         } catch (InstantiationException e) {
  176.             e.printStackTrace();
  177.         } catch (IllegalAccessException e) {
  178.             e.printStackTrace();
  179.         } catch (UnsupportedLookAndFeelException e) {
  180.             e.printStackTrace();
  181.         }
  182.     }
  183. }

  184. class AboutUI extends JDialog implements Runnable {
  185.     private static final long serialVersionUID = -6285085789330239239L;
  186.     private JTextPane messageText = new JTextPane();
  187.     ImageIcon icon = new ImageIcon("1.gif");

  188.     private AboutUI() {
  189.         // super(frame,true);
  190.         init();
  191.     }

  192.     private static AboutUI au;

  193.     public static AboutUI getInstance() {
  194.         if (au == null)
  195.             au = new AboutUI();
  196.         return au;
  197.     }

  198.     private void init() {
  199.         messageText.setFont(new Font("", Font.PLAIN, 12));
  200.         messageText.setBackground(Color.LIGHT_GRAY);
  201.         messageText.setText("    作者:Bllows.Van\n    Email(MSN):[email protected]\n    QQ:417616683");
  202.         messageText.setEditable(false);
  203.         JPanel panel = new JPanel() {
  204.             private static final long serialVersionUID = 1L;
  205.             protected void paintComponent(Graphics g) {
  206.                 g.drawImage(icon.getImage(), 00null);
  207.                 super.paintComponent(g);
  208.             }
  209.         };
  210.         panel.setOpaque(false);
  211.         panel.setPreferredSize(new Dimension(1020));
  212.         this.setTitle("关于Billows窗体测试");
  213.         this.getContentPane().add(messageText, BorderLayout.CENTER);
  214.         this.getContentPane().add(panel, BorderLayout.NORTH);
  215.         this.setSize(new Dimension(200150));
  216.         this.setLocationRelativeTo(null);
  217.         this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  218.         this.pack();
  219.     }

  220.     public void run() {
  221.         while (true) {
  222.             this.repaint();
  223.             try {
  224.                 Thread.sleep(200);
  225.             } catch (InterruptedException e) {
  226.                 e.printStackTrace();
  227.             }
  228.         }
  229.     }
  230. }

你可能感兴趣的:(java,thread,PHP,swing,qq)