听说java6支持任务栏图标,于是写了个小程序测试下。
先看看截图吧:
1.那个最可爱的就是咱的了
2.右键点击下看看
3.这是关于的界面
4.看看程序的主界面吧
5.计算器
6.万年历
7.测试用的
好了,该看看源代码了:
(注:部分代码来自网络。)
MyFrame.java
- package test;
-
WeatherPanel.java- package test;
- import java.awt.BorderLayout;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.regex.Pattern;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JTextField;
- public class WeatherPanel extends JPanel implements ActionListener{
- private static final long serialVersionUID = 1L;
- private static WeatherPanel wp;
- private Pattern cityP;
- private JTextField jta=new JTextField(10);
- private JButton button=new JButton("设定城市");
- private JProgressBar loading_jpb=new JProgressBar();
- private String city="上海";
- private OpenUrl openUrl=new OpenUrl();
- private Weather weather = null;
- public static WeatherPanel getInstance(){
- if(wp==null)
- wp=new WeatherPanel();
- return wp;
- }
- private WeatherPanel(){
- jta.setText("上海");
- init();
- new Thread(){
- public void run(){
- //weather=openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city);
- weather=openUrl.getWeatherByUrl("D:\\aaa\\tomcat6\\webapps\\aaa\\detail.html");
- WeatherPanel.getInstance().repaint();
- }
- }.start();
- }
- private void init(){
- this.setOpaque(false);
- JPanel south=new JPanel();
- south.setLayout(new BorderLayout());
- JPanel panel=new JPanel();
- panel.add(jta);
- jta.addKeyListener(new KeyAdapter(){
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode()==KeyEvent.VK_ENTER){
- paintWeather();
- }
- }
- });
- panel.add(button);
- south.add(panel,BorderLayout.SOUTH);
- south.add(loading_jpb,BorderLayout.NORTH);
- //button.addActionListener(this);
- button.addMouseListener(new MouseAdapter(){
- public void mouseClicked(MouseEvent e) {
- paintWeather();
- }
- });
- this.setLayout(new BorderLayout());
- this.add(south,BorderLayout.SOUTH);
- loading_jpb.setVisible(true);
- loading_jpb.setIndeterminate(true);
- }
- protected void paintComponent(Graphics g){
- int x=10,y=20;
- if(weather==null){
- // city="上海";
- // weather = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city);
- return;
- }
- g.drawString(weather.getCity()+":"+weather.getDate1(), x, y);
- String weatherName1="";
- String weatherName2="";
- String legend1="";
- String legend2="";
- ImageIcon icon1=null,icon2=null;
- Image img1=null,img2=null;
- int t1=0;
- if((t1=weather.getWeather1().indexOf("转"))!=-1){
- weatherName1=weather.getWeather1().substring(0, t1);
- weatherName2=weather.getWeather1().substring(t1+1, weather.getWeather1().length());
- legend1="weather_legend/a"+openUrl.getWeatherNumber(weatherName1)+".gif";
- legend2="weather_legend/a"+openUrl.getWeatherNumber(weatherName2)+".gif";
- icon1=new ImageIcon(legend1);
- icon2=new ImageIcon(legend2);
- img1=icon1.getImage();
- img2=icon2.getImage();
- g.drawImage(img1, x, y+10, 60, 51,null);
- g.drawImage(img2, x+58, y+10, 60, 51,null);
- }else{
- weatherName1=weather.getWeather1();
- legend1="weather_legend/a"+openUrl.getWeatherNumber(weatherName1)+".gif";
- icon1=new ImageIcon(legend1);
- img1=icon1.getImage();
- g.drawImage(img1, x+30, y+10, 60, 51, null);
- }
- g.drawString("天气:"+weather.getWeather1(), x, y+80);
- g.drawString("气温:"+weather.getTemperature1(), x, y+80+25);
- g.drawString("风力:"+weather.getWind1(), x, y+80+25+25);
- g.drawLine(180, 20, 180, 180);
- x+=190;
- g.drawString(weather.getCity()+":"+weather.getDate2(), x, y);
- String weatherName3="";
- String weatherName4="";
- String legend3="";
- String legend4="";
- ImageIcon icon3=null,icon4=null;
- Image img3=null,img4=null;
- int t2=0;
- if((t2=weather.getWeather2().indexOf("转"))!=-1){
- weatherName3=weather.getWeather2().substring(0, t2);
- weatherName4=weather.getWeather2().substring(t2+1, weather.getWeather2().length());
- legend3="weather_legend/a"+openUrl.getWeatherNumber(weatherName3)+".gif";
- legend4="weather_legend/a"+openUrl.getWeatherNumber(weatherName4)+".gif";
- icon3=new ImageIcon(legend3);
- icon4=new ImageIcon(legend4);
- img3=icon3.getImage();
- img4=icon4.getImage();
- g.drawImage(img3, x, y+10, 60, 51,null);
- g.drawImage(img4, x+58, y+10, 60, 51,null);
- }else{
- weatherName3=weather.getWeather2();
- legend3="weather_legend/a"+openUrl.getWeatherNumber(weatherName3)+".gif";
- icon3=new ImageIcon(legend3);
- img3=icon3.getImage();
- g.drawImage(img3, x+30, y+10, 60, 51, null);
- }
- g.drawString("天气:"+weather.getWeather2(), x, y+80);
- g.drawString("气温:"+weather.getTemperature2(), x, y+80+25);
- g.drawString("风力:"+weather.getWind2(), x, y+80+25+25);
- //super.paintComponent(g);
- loading_jpb.setVisible(false);
- jta.setEditable(true);
- button.setEnabled(true);
- }
- /**
- * 城市改变时,执行的操作
- */
- private void paintWeather(){
- cityP = Pattern.compile("[^\\d\\w\\p{Punct}]{2,10}");
- city=jta.getText().trim();
- if (cityP.matcher(city).matches()) {
- jta.setEditable(false);
- button.setEnabled(false);
- loading_jpb.setVisible(true);
- new Thread(){
- public void run(){
- weather = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city="+city); <l>
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- 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.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.LinkedList;
- import javax.swing.ImageIcon;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JTabbedPane;
- import javax.swing.JTextPane;
- import javax.swing.JWindow;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class MyFrame extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1026416994451303162L;
- private static MyFrame mf = null;
- private JTabbedPane jtp = new JTabbedPane();
- private JWindow windowSplash;
- public static MyFrame getInstance() {
- if (mf == null)
- mf = new MyFrame();
- return mf;
- }
- public MyFrame() {
- super("改变窗体测试");
- prepareSplash();
- this.startSplash();
- init();
- systemTray();
- this.stopSplash();
- }
- private void init() {
- jtp.add("天气", WeatherPanel.getInstance());
- jtp.add("计算器",MyCalculator.getInstance());
- jtp.add("万年历",MyCalendar.getInstance());
- jtp.add("测试", TestWindow.getInstance());
- //jtp.add("设置",SetCity.getInstance());
- this.getContentPane().add(jtp, BorderLayout.CENTER);
- jtp.getAccessibleContext().setAccessibleName("");
- this.setSize(400, 350);
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- int h = (int) d.getHeight() / 3;
- int w = (int) d.getWidth() / 3;
- this.setLocation(w, h);
- this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- LinkedList<Image> icon = new LinkedList<Image>();
- try {
- icon.add(new ImageIcon(new URL("http://weather.tq121.com.cn/images/sun.gif")).getImage());
- } catch (MalformedURLException e1) {
- e1.printStackTrace();
- }
- this.setIconImages(icon);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- MyFrame.getInstance().setVisible(false);
- }
- });
- this.setVisible(true);
- }
- private void prepareSplash(){
- Toolkit toolkit=Toolkit.getDefaultToolkit();
- windowSplash=new JWindow(this);
- windowSplash.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
- Container container = windowSplash.getContentPane();
- URL url = getClass().getResource("1.png"); // 图片的位置
- if (url != null) {
- container.add(new JLabel(new ImageIcon(url)), BorderLayout.CENTER); // 增加图片
- }
- JProgressBar progress=new JProgressBar();
- progress.setVisible(true);
- progress.setIndeterminate(true);
- container.add(progress, BorderLayout.SOUTH);
- Dimension scmSize = toolkit.getScreenSize();
- windowSplash.setLocation(scmSize.width/2 , scmSize.height/2 );
- windowSplash.pack();
- }
- private void startSplash(){
- windowSplash.setVisible(true);
- windowSplash.toFront();
- }
- private void stopSplash(){
- windowSplash.dispose();
- }
- public void actionPerformed(ActionEvent e) {
- }
- public void systemTray() {
- try {
- if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘
- SystemTray st = SystemTray.getSystemTray();
- Image image = Toolkit.getDefaultToolkit().getImage(
- getClass().getResource("1.gif"));// 定义托盘图标的图片
- TrayIcon ti = new TrayIcon(image);
- ti.setToolTip("改变窗体测试");
- ti.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON1)// 鼠标左键单击,打开窗体
- MyFrame.getInstance().setVisible(true);
- MyFrame.getInstance().setExtendedState(JFrame.NORMAL);
- }
- });
- PopupMenu p = new PopupMenu("OK");
- MenuItem about = new MenuItem("关于");
- about.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Thread t = new Thread(AboutUI.getInstance());
- t.start();
- AboutUI.getInstance().setVisible(true);
- }
- });
- p.add(about);
- p.addSeparator();
- MenuItem m1 = new MenuItem("打开");
- m1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- MyFrame.getInstance().setVisible(true);
- MyFrame.getInstance().setExtendedState(JFrame.NORMAL);
- }
- });
- p.add(m1);
- MenuItem m2 = new MenuItem("最大化");
- m2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- MyFrame.getInstance().setVisible(true);
- MyFrame.getInstance().setExtendedState(
- JFrame.MAXIMIZED_BOTH);
- }
- });
- p.add(m2);
- p.addSeparator();
- MenuItem m = new MenuItem("退出");
- m.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- p.add(m);
- ti.setPopupMenu(p); // 为托盘添加右键菜单
- st.add(ti);
- }
- } catch (Exception e) {
- }
- }
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- // JFrame.setDefaultLookAndFeelDecorated(true);
- MyFrame.getInstance();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (UnsupportedLookAndFeelException e) {
- e.printStackTrace();
- }
- }
- }
- class AboutUI extends JDialog implements Runnable {
- private static final long serialVersionUID = -6285085789330239239L;
- private JTextPane messageText = new JTextPane();
- ImageIcon icon = new ImageIcon("1.gif");
- private AboutUI() {
- // super(frame,true);
- init();
- }
- private static AboutUI au;
- public static AboutUI getInstance() {
- if (au == null)
- au = new AboutUI();
- return au;
- }
- private void init() {
- messageText.setFont(new Font("", Font.PLAIN, 12));
- messageText.setBackground(Color.LIGHT_GRAY);
- messageText.setText(" 作者:Bllows.Van\n Email(MSN):haidii@126.com\n QQ:417616683");
- messageText.setEditable(false);
- JPanel panel = new JPanel() {
- private static final long serialVersionUID = 1L;
- protected void paintComponent(Graphics g) {
- g.drawImage(icon.getImage(), 0, 0, null);
- super.paintComponent(g);
- }
- };
- panel.setOpaque(false);
- panel.setPreferredSize(new Dimension(10, 20));
- this.setTitle("关于Billows窗体测试");
- this.getContentPane().add(messageText, BorderLayout.CENTER);
- this.getContentPane().add(panel, BorderLayout.NORTH);
- this.setSize(new Dimension(200, 150));
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- this.pack();
- }
- public void run() {
- while (true) {
- this.repaint();
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }