网络编程

本文介绍了一个使用Java Swing构建的GUI应用程序示例,包括窗口初始化、事件处理等关键步骤,并展示了如何利用系统托盘进行应用状态展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;


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();
            }
        }
    }
}
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);
                    if(weather==null){
                        jta.setEditable(true);
                        button.setEnabled(true);
                        loading_jpb.setVisible(false);
                        return;
                    }
                    else{
                        WeatherPanel.getInstance().repaint();
                    }
                }
            }.start();
        }else{
            JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,请输入正确的城市名!");
        }
    }


    public void actionPerformed(ActionEvent e) {
        String str=e.getActionCommand();
        if(str.equals("设定城市")){
            paintWeather();
        }
    }
}


WeatherPanel.java中用到OpenUrl.java和Weather.java,代码如下:
OpenUrl.java

package test;
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.URL;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  


import javax.swing.JOptionPane;
 
public class OpenUrl { 
    String[] s1 = {"晴", "多云", "阴", "阵雨", "雷阵雨", "雷阵雨并伴有冰雹", "雨加雪", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨", "阵雪", "小雪", "中雪", "大雪", "暴雪", "雾", "冻雨", "沙尘暴", "小雨-中雨", "中雨-大雨", "大雨-暴雨", "暴雨-大暴雨", "大暴雨-特大暴雨", "小雪-中雪", "中雪-大雪", "大雪-暴雪", "浮尘", "扬沙", "强沙尘暴"};  
    public static void main(String[] args){  
        OpenUrl openUrl = new OpenUrl(); 
       Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=上海");
        System.out.println(w.getCity());
        System.out.println(w.getDate1());  
        System.out.println(w.getTemperature1());  
        System.out.println(w.getWeather1());  
        System.out.println(w.getWind1());  
          
        System.out.println(w.getDate2());  
        System.out.println(w.getTemperature2());  
        System.out.println(w.getWeather2());  
        System.out.println(w.getWind2());  
          
        System.out.println(w.getDate3());  
        System.out.println(w.getTemperature3());  
        System.out.println(w.getWeather3());  
        System.out.println(w.getWind3());  
          
        System.out.println(w.getDate4());  
        System.out.println(w.getTemperature4());  
        System.out.println(w.getWeather4());  
        System.out.println(w.getWind4());  
          
        System.out.println(w.getDate5());  
        System.out.println(w.getTemperature5());  
        System.out.println(w.getWeather5());  
        System.out.println(w.getWind5());  
       
        System.out.println(w.getUltravioletData());
        System.out.println(w.getAirData());
    }  
      
    /**  
     * 根据连接抓出5天天气  
     */  
    public Weather getWeatherByUrl(String url){  
        Weather weather = new Weather();  
        try {  
            long l1 = System.currentTimeMillis();  
            String content = getContent(url); 
            weather = getCity(content , weather);
            weather = getDateTemperature(content , weather);  
            weather = getWind3(content , weather);  
            weather = getWeather3(content , weather);  
            weather = getDate(content , weather);  
            weather = getTemperature(content , weather);  
            weather = getWind2(content , weather);  
            weather = getWeather2(content , weather);
            weather = this.getAirData(content, weather);
            long l2 = System.currentTimeMillis();  
            System.out.println("获取天气信息所用时间为:"+(l2 - l1)+" 毫秒");  
        }catch (Exception e){  
            JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,请输入正确的城市名!");
            return null;
        }  
        return weather;  
    }  
 
    /**  
     * 根据连接地址抓出页面内容  
     * @param 根据一个连接地址  
     * @return 页面内容  
     */  
    private String getContent(String strUrl){
        try{  
            URL url = new URL(strUrl);  
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); 
            String s = "";  
            StringBuffer sb=new StringBuffer();  
            while((s = br.readLine())!=null){  
                sb.append(s+"/r/n");  
            } 
            br.close();  
            return sb.toString();  
        }catch(Exception e){ 
            System.out.println("打开地址:" + strUrl+" 失败!");
            return "打开地址失败:" + strUrl;  
        }  
    }  
   
    /**
     * 抓取城市名称
     * @param content
     * @param weather
     * @return
     * @throws Exception
     */
    private Weather getCity(String content , Weather weather) throws Exception{ 
        String[] s = analysis(" <td width=/"222/" class=/"big-cn/">(.*?)</td>", content , 1); 
        weather.setCity(s[0].trim());
        return weather;  
    }
 
    /**  
     * 取当前3天内的温度和时间  <br>
     * 返回一个天气bean   <br>
     * 0和1 2个参数为第一天时间和温度 后面相同  <br> 
     * content 为页面内容  
     * weather 为天气bean  
     */  
    private Weather getDateTemperature(String content , Weather weather) throws Exception{  
        String[] s = analysis("<td width=/"215/" align=/"center/" valign=/"middle/"><span class=/"big-cn/">(.*?)</span></td>", content , 6);  
        weather.setDate1(s[0].trim());  
        weather.setTemperature1(s[1].trim());  
        weather.setDate2(s[2].trim());  
        weather.setTemperature2(s[3].trim());  
        weather.setDate3(s[4].trim());  
        weather.setTemperature3(s[5].trim());  
        return weather;  
    }  
 
    /**  
     * 取前3天的风向  
     * content 为页面内容  
     * weather 为天气bean  
     */  
    private Weather getWind3(String content , Weather weather){  
        String[] s = analysis("<td width=/"215/" align=/"center/" valign=/"middle/"><span class=/"cn/">(.*?)</span></td>", content , 3);  
        weather.setWind1(s[0].trim());  
        weather.setWind2(s[1].trim());  
        weather.setWind3(s[2].trim());  
        return weather;  
    }  
      
    /**  
     * 取前3天的天气情况  
     * content 为页面内容  
     * weather 为天气bean  
     */  
    private Weather getWeather3(String content , Weather weather){  
        String[] s = analysis("align=/"center/" valign=/"top/"><img src=/"../images/a(.*?).gif/" width=/"70/" height=/"65/"></td>" , content , 6);  
        s = ConversionWeather(s);  
        weather.setWeather1(s[0]);  
        weather.setWeather2(s[1]);  
        weather.setWeather3(s[2]);  
        return weather;  
    }  
      
    /**  
     * 取后2天的日期  
     */  
    private Weather getDate(String content , Weather weather){  
        String[] s = analysis("<td width=/"121/"><span class=/"cn/">(.*?)</span></td>" , content , 2);  
        weather.setDate4(s[0].trim());  
        weather.setDate5(s[1].trim());  
        return weather;  
    }  
      
    /**  
     * 取后天2天的温度  
     */  
    private Weather getTemperature(String content , Weather weather){  
        String[] s = analysis("<td width=/"86/" class=/"cn/"><span class=/"wendu/">(.*?)</span></td>" , content , 2);  
        weather.setTemperature4(s[0].trim());  
        weather.setTemperature5(s[1].trim());  
        return weather;  
    }  
      
    /**  
     * 取后天2天的风向  
     */  
    private Weather getWind2(String content , Weather weather){  
        String[] s = analysis("<td width=/"157/"><span class=/"cn/">(.*?)</span></td>" , content , 2);  
        weather.setWind4(s[0].trim());  
        weather.setWind5(s[1].trim());  
        return weather;  
    }  
      
    /**  
     *   
     */  
    private Weather getWeather2(String content , Weather weather){  
        String[] s = analysis("<img src=/"../images/b(.*?).gif/" width=/"50/" height=/"46/"></td>" , content , 4);  
        s = ConversionWeather(s);  
        weather.setWeather4(s[0]);  
        weather.setWeather5(s[1]);  
        return weather;  
    } 
   
    /**
     * 获取紫外线指数和空气净化指数
     */   
    private Weather getAirData(String content , Weather weather){
        String[] s=analysis("<td width=/"87/" align=/"left/" valign=/"middle/" class=/"cn/">(.*?)</td>",content,19);
        String[] s1=analysis("<td width=/"120/" align=/"left/" class=/"cn/">(.*?)</td>",content,16);
        weather.setUltravioletData(s[7]+":"+s1[6]);
        weather.setAirData(s[8]+":"+s1[7]);
        return weather;
    }
   
 
    /**  
     * 根据页面内容和正则表达式来分析页面,得到分析结果  
     * @param pattern 正则表达式  
     * @param match 页面内容  
     * @return content 结果  
     */  
    private String[] analysis(String pattern, String match , int i){  
        Pattern sp = Pattern.compile(pattern);  
        Matcher matcher = sp.matcher(match);  
        String[] content = new String[i];  
        for (int j = 0; matcher.find(); j++){  
            content[j] = matcher.group(1);  
        }  
        return content;  
    }  
 
    /**  
     * 转换天气情况  
     * @param s第1张图片  
     * @return 天气情况  
     */  
    private String[] ConversionWeather(String s[]){  
        String[] s2 = new String[s.length/2];  
        int i1 = 0;  
        for (int i = 0; i < s.length; i += 2){  
            if (s[i].trim().equals(s[i+1].trim())){  
                s2[i1] = s1[Integer.parseInt(s[i])];  
            }else {  
                s2[i1] = s1[Integer.parseInt(s[i])] + "转" + s1[Integer.parseInt(s[i+1])];  
            }  
            i1++;  
        }
        return s2;  
    }
   
    public int getWeatherNumber(String weatherName){
        int t=0;
        for(int i=0;i<s1.length;i++){
            if(weatherName.equals(s1[i])){
                t=i;
                break;
            }
        }
        return t;
    }


Weather.java:

package test;

public class Weather {
    // 第1天时间
    private String date1;

    // 第1天温度
    private String temperature1;

    // 第1天天气情况
    private String weather1;

    // 第1天风向
    private String wind1;

    // 第2天时间
    private String date2;

    // 第2天温度
    private String temperature2;

    // 第2天天气情况
    private String weather2;

    // 第2天风向
    private String wind2;

    // 第3天时间
    private String date3;

    // 第3天温度
    private String temperature3;

    // 第3天天气情况
    private String weather3;

    // 第3天风向
    private String wind3;

    // 第4天时间
    private String date4;

    // 第4天温度
    private String temperature4;

    // 第4天天气情况
    private String weather4;

    // 第4天风向
    private String wind4;

    // 第5天时间
    private String date5;

    // 第5天温度
    private String temperature5;

    // 第5天天气情况
    private String weather5;

    // 第5天风向
    private String wind5;

    // 紫外线指数
    private String ultravioletData;

    // 空气净化指数
    private String airData;
    private String city;

    public String getCity() {
        return city.trim().replace(">", ".").replace(" ", "");
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getDate1() {
        return date1;
    }

    public void setDate1(String date1) {
        this.date1 = date1;
    }

    public String getDate2() {
        return date2;
    }

    public void setDate2(String date2) {
        this.date2 = date2;
    }

    public String getDate3() {
        return date3;
    }

    public void setDate3(String date3) {
        this.date3 = date3;
    }

    public String getDate4() {
        return date4;
    }

    public void setDate4(String date4) {
        this.date4 = date4;
    }

    public String getDate5() {
        return date5;
    }

    public void setDate5(String date5) {
        this.date5 = date5;
    }

    public String getTemperature1() {
        return temperature1;
    }

    public void setTemperature1(String temperature1) {
        this.temperature1 = temperature1;
    }

    public String getTemperature2() {
        return temperature2;
    }

    public void setTemperature2(String temperature2) {
        this.temperature2 = temperature2;
    }

    public String getTemperature3() {
        return temperature3;
    }

    public void setTemperature3(String temperature3) {
        this.temperature3 = temperature3;
    }

    public String getTemperature4() {
        return temperature4;
    }

    public void setTemperature4(String temperature4) {
        this.temperature4 = temperature4;
    }

    public String getTemperature5() {
        return temperature5;
    }

    public void setTemperature5(String temperature5) {
        this.temperature5 = temperature5;
    }

    public String getWeather1() {
        return weather1;
    }

    public void setWeather1(String weather1) {
        this.weather1 = weather1;
    }

    public String getWeather2() {
        return weather2;
    }

    public void setWeather2(String weather2) {
        this.weather2 = weather2;
    }

    public String getWeather3() {
        return weather3;
    }

    public void setWeather3(String weather3) {
        this.weather3 = weather3;
    }

    public String getWeather4() {
        return weather4;
    }

    public void setWeather4(String weather4) {
        this.weather4 = weather4;
    }

    public String getWeather5() {
        return weather5;
    }

    public void setWeather5(String weather5) {
        this.weather5 = weather5;
    }

    public String getWind1() {
        return wind1;
    }

    public void setWind1(String wind1) {
        this.wind1 = wind1;
    }

    public String getWind2() {
        return wind2;
    }

    public void setWind2(String wind2) {
        this.wind2 = wind2;
    }

    public String getWind3() {
        return wind3;
    }

    public void setWind3(String wind3) {
        this.wind3 = wind3;
    }

    public String getWind4() {
        return wind4;
    }

    public void setWind4(String wind4) {
        this.wind4 = wind4;
    }

    public String getWind5() {
        return wind5;
    }

    public void setWind5(String wind5) {
        this.wind5 = wind5;
    }

    public String getUltravioletData() {
        return ultravioletData;
    }

    public void setUltravioletData(String ultravioletData) {
        this.ultravioletData = ultravioletData;
    }

    public String getAirData() {
        return airData;
    }

    public void setAirData(String airData) {
        this.airData = airData;
    }
}

MyCalculator.java(计算器的代码)

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyCalculator extends JPanel implements ActionListener{
    private static final long serialVersionUID = 1L;
    private JButton[] jbs;
    private JTextField jta;
    private JLabel label;
    private double firstNum,secondNum,res;
    private String operator;
    private static MyCalculator mc;
   
    public static MyCalculator getInstance(){
        if(mc==null)
            mc=new MyCalculator();
        return mc;
    }
   
    private MyCalculator(){
        //super("我的计算器v1.0");
        jbs=new JButton[20];
        jta=new JTextField(20);
        jta.setHorizontalAlignment(JTextField.RIGHT);
        label=new JLabel("算式提示:");
        firstNum=0;
        secondNum=0;
        operator="";       
       
        init();
        addActionHandle();
    }
   
    public void init(){
        JPanel north=new JPanel();
        JPanel center=new JPanel();
       
        this.setLayout(new BorderLayout());
       
        north.setLayout(new BorderLayout());
        center.setLayout(new GridLayout(5,4));
       
        jta.setFont(new Font("",Font.BOLD,18));
        jta.setEditable(false);
        label.setForeground(Color.BLUE);
        north.add(jta,BorderLayout.CENTER);
        north.add(label,BorderLayout.SOUTH);
       
        jbs[0]=new JButton("sqrt");
        jbs[1]=new JButton("1/x");
        jbs[2]=new JButton("+/-");
        jbs[3]=new JButton("清零");
        String str="123+456-789*0.=/";
        for(int i=4;i<jbs.length;i++){
            jbs[i]=new JButton(str.charAt(i-4)+"");
        }
        for(int i=0;i<jbs.length;i++)
            center.add(jbs[i]);
       
        this.add(north,BorderLayout.NORTH);
        this.add(center,BorderLayout.CENTER);
    }
   
    public void showMe(){
        //this.pack();
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void addActionHandle(){
        for(int i=0;i<jbs.length;i++){
            jbs[i].addActionListener(this);
        }
    }
   
    String la="算式提示:";
    public void actionPerformed(ActionEvent e) {
        String str=e.getActionCommand();
        if(!str.equals("sqrt") && !str.equals("1/x") && !str.equals("+/-") && !str.equals("清零") )
            la+=str;
       
        if(operator.equals("=")){
            operator="";
            firstNum=0;
            secondNum=0;
            jta.setText("");
        }
       
        if("0123456789.".indexOf(str) != -1){
            jta.setText(jta.getText()+str);
            label.setText(la);
        }
        if("+-*/".indexOf(str) != -1){
            operator=str;
            try {
                firstNum=Double.valueOf(jta.getText());
                jta.setText("");
                label.setText(la);
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,转换出错。/n"+e1.getMessage());
            }
        }
        if(str.equals("=")){
            secondNum=Double.valueOf(jta.getText());
            double s=cal(firstNum,secondNum);
            String result=takeOut0(firstNum)+operator+takeOut0(secondNum)+"="+takeOut0(s);
            jta.setText(takeOut0(s));
            label.setText("算式提示:"+result);
           
            operator="=";
            firstNum=0;
            secondNum=0;
            la="算式提示:";
        }
       
        if(str.equals("清零")){
            operator="";
            firstNum=0;
            secondNum=0;
            jta.setText("");
            la="算式提示:";
            label.setText(la);
        }
       
        if(str.equals("1/x")){
            try {
                Double s=Double.parseDouble(jta.getText());
                String st=String.valueOf(1/s);
                if(st.equals("Infinity"))
                    throw new NumberFormatException("该数没有倒数....");
                jta.setText(takeOut0(1/s));
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,运算出错。/n"+e1.getMessage());
            }
        }
       
        if(str.equals("sqrt")){
            try {
                Double s=Double.parseDouble(jta.getText());
                String st=String.valueOf(Math.sqrt(s));
                if(st.equals("NaN"))
                    throw new NumberFormatException("该数不能开根号....");
                jta.setText(takeOut0(Math.sqrt(s)));
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,运算出错。/n"+e1.getMessage());
            }
        }
       
        if(str.equals("+/-")){
            try {
                Double s=Double.parseDouble(jta.getText());
                jta.setText(takeOut0(-s));
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(WeatherPanel.getInstance(), "对不起,转换出错。/n"+e1.getMessage());
            }
        }

    }
   
    private String takeOut0(double d){
        String s=d+"";
        if(s.endsWith(".0"))
            s=s.substring(0, s.indexOf(".0"));
        return s;
    }
    private double cal(double f,double s){
        BigDecimal b1 = new BigDecimal(Double.toString(f));
        BigDecimal b2 = new BigDecimal(Double.toString(s));
        res=0;
        if(operator.equals("+")){
            res=b1.add(b2).doubleValue();
        }
        if(operator.equals("-")){
            res=b1.subtract(b2).doubleValue();
        }
        if(operator.equals("*")){
            res=b1.multiply(b2).doubleValue();
        }
        if(operator.equals("/")){
            res=f/s;
        }
        return res;
    }
   
    public static void main(String[] args) {
        new MyCalculator().showMe();
        System.out.println(">>>>>>>>>>>>");
    }

}

MyCalendar.java(万年历)

package test;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class MyCalendar extends JPanel {
    private static final long serialVersionUID = 1L;
    private JPanel panel = new JPanel(new BorderLayout());
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel(new GridLayout(7, 7));
    private JPanel panel3 = new JPanel();
    private JButton[] button = new JButton[49];
    private JLabel y_label = new JLabel("年份");
    private JLabel m_label = new JLabel("月份");
    private JComboBox com1 = new JComboBox();
    private JComboBox com2 = new JComboBox();
    private int re_year, re_month;
    private int x_size, y_size;
    private String year_num;
    private Calendar now = Calendar.getInstance(); // 实例化Calendar
    private static MyCalendar mf;
   
    public static MyCalendar getInstance(){
        if(mf==null)
            mf=new MyCalendar();
        return mf;
    }

    private MyCalendar() {
//      super("万年历");
        setSize(350, 350);
        x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
        y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        setLocation((x_size - 300) / 2, (y_size - 350) / 2);
//      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel1.add(y_label);
        panel1.add(com1);
        panel1.add(m_label);
        panel1.add(com2);
        for (int i = 0; i < 49; i++) {
            button[i] = new JButton("");// 将显示的字符设置为居中
            button[i].setBackground(new Color(211,233,243));
            panel2.add(button[i]);
        }
        panel.add(panel1, BorderLayout.NORTH);
        panel.add(panel2, BorderLayout.CENTER);
        panel.add(panel3, BorderLayout.SOUTH);
        Init();
        com1.addActionListener(new ClockAction());
        com2.addActionListener(new ClockAction());

        //setContentPane(panel);
        this.add(panel);
        setVisible(true);
        //setResizable(false);
    }

    class ClockAction implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            int c_year, c_month, c_week;
            c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
            c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
            c_week = use(c_year, c_month); // 调用函数use,得到星期几
            Resetday(c_week, c_year, c_month); // 调用函数Resetday
        }
    }

    public void Init() {
        Integer year, month_num, first_day_num;
        String log[] = { "日", "一", "二", "三", "四", "五", "六" };
        for (int i = 0; i < 7; i++) {
            button[i].setBackground(new Color(39,109,143));
            if(i==0 || i==6){
                button[i].setBackground(new Color(255,102,0));
                button[i].setForeground(new Color(255,255,255));
                button[i].setFont(new Font("",Font.BOLD,14));
            }
            button[i].setText(log[i]);
        }
        for (int i = 7; i < 49; i = i + 7) {
            button[i].setFont(new Font("",Font.BOLD,18));
            button[i].setForeground(new Color(255,102,0)); // 将星期日的日期设置为红色
        }
        for (int i = 13; i < 49; i = i + 7) {
            button[i].setFont(new Font("",Font.BOLD,18));
            button[i].setForeground(new Color(255,102,0));// 将星期六的日期设置为绿色
        }
        for (int i = 1900; i < 2100; i++) {
            com1.addItem("" + i);
        }
        for (int i = 1; i < 13; i++) {
            com2.addItem("" + i);
        }
        month_num = (Integer) (now.get(Calendar.MONTH)); // 得到当前时间的月份
        year = (Integer) (now.get(Calendar.YEAR)); // 得到当前时间的年份
        com1.setSelectedIndex(year - 1900); // 设置下拉列表显示为当前年
        com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
        first_day_num = use(year, month_num);
        Resetday(first_day_num, year, month_num);
    }

    public int use(int reyear, int remonth) {
        int week_num;
        now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
        week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
        return week_num;
    }

    @SuppressWarnings("deprecation")
    public void Resetday(int week_log, int year_log, int month_log) {
        int month_day_score; // 存储月份的天数
        int count;
        month_day_score = 0;
        count = 1;

        Date date = new Date(year_log, month_log + 1, 1); // now
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1); // 前个月
        month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

        for (int i = 7; i < 49; i++) { // 初始化按钮
            button[i].setText("");
        }
        week_log = week_log + 6; // 将星期数加6,使显示正确
        month_day_score = month_day_score + week_log;
        for (int i = week_log; i < month_day_score; i++, count++) {
            button[i].setText(count+"");
            now.set(Calendar.DAY_OF_MONTH, count);
            Lunar lunar = new Lunar(now);
            button[i].setToolTipText(lunar.toString());
        }
    }

    public static void main(String[] args) {
        //JFrame.setDefaultLookAndFeelDecorated(true);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new MyCalendar();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

Lunar.java

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Lunar {
    private int gyear;
    private int gmonth;
    private int gday;
    private static int year;
    private int month;
    private int day;
    private boolean leap;
    final static String chineseNumber[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    final static long[] lunarInfo = new long[]
    {0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
     0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
     0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
     0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
     0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
     0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
     0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
     0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
     0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
     0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
     0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
     0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
     0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
     0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
     0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0};

    //====== 传回农历 y年的总天数
    final private static int yearDays(int y) {
        int i, sum = 348;
        for (i = 0x8000; i > 0x8; i >>= 1) {
            if ((lunarInfo[y - 1900] & i) != 0) sum += 1;
        }
        return (sum + leapDays(y));
    }

    //====== 传回农历 y年闰月的天数
    final private static int leapDays(int y) {
        if (leapMonth(y) != 0) {
            if ((lunarInfo[y - 1900] & 0x10000) != 0)
                return 30;
            else
                return 29;
        } else
            return 0;
    }

    //====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
    final private static int leapMonth(int y) {
        return (int) (lunarInfo[y - 1900] & 0xf);
    }

    //====== 传回农历 y年m月的总天数
    final private static int monthDays(int y, int m) {
        if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
            return 29;
        else
            return 30;
    }

    //====== 传回农历 y年的生肖
    final public String animalsYear() {
        final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
        return Animals[(year - 4) % 12];
    }

    //====== 传入 月日的offset 传回干支, 0=甲子
    final private static String cyclicalm(int num) {
        final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
        final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        return (Gan[num % 10] + Zhi[num % 12]);
    }

    //====== 传入 offset 传回干支, 0=甲子
    final public String cyclical() {
        int num = year - 1900 + 36;
        return (cyclicalm(num));
    }

    /** *//**
     * 传出y年m月d日对应的农历.
     * yearCyl3:农历年与1864的相差数              ?
     * monCyl4:从1900年1月31日以来,闰月数
     * dayCyl5:与1900年1月31日相差的天数,再加40      ?
     * @param cal
     * @return
     */
    public Lunar(Calendar cal) {
        @SuppressWarnings("unused") int yearCyl, monCyl, dayCyl;
        int leapMonth = 0;
        Date baseDate = null;
        gyear=cal.get(Calendar.YEAR);
        gmonth=cal.get(Calendar.MONTH);
        gday=cal.get(Calendar.DAY_OF_MONTH);
        try {
            baseDate = chineseDateFormat.parse("1900年1月31日");
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }

        //求出和1900年1月31日相差的天数
        int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
        dayCyl = offset + 40;
        monCyl = 14;

        //用offset减去每农历年的天数
        // 计算当天是农历第几天
        //i最终结果是农历的年份
        //offset是当年的第几天
        int iYear, daysOfYear = 0;
        for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
            daysOfYear = yearDays(iYear);
            offset -= daysOfYear;
            monCyl += 12;
        }
        if (offset < 0) {
            offset += daysOfYear;
            iYear--;
            monCyl -= 12;
        }
        //农历年份
        year = iYear;

        yearCyl = iYear - 1864;
        leapMonth = leapMonth(iYear); //闰哪个月,1-12
        leap = false;

        //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
        int iMonth, daysOfMonth = 0;
        for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
            //闰月
            if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
                --iMonth;
                leap = true;
                daysOfMonth = leapDays(year);
            } else
                daysOfMonth = monthDays(year, iMonth);

            offset -= daysOfMonth;
            //解除闰月
            if (leap && iMonth == (leapMonth + 1)) leap = false;
            if (!leap) monCyl++;
        }
        //offset为0时,并且刚才计算的月份是闰月,要校正
        if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
            if (leap) {
                leap = false;
            } else {
                leap = true;
                --iMonth;
                --monCyl;
            }
        }
        //offset小于0时,也要校正
        if (offset < 0) {
            offset += daysOfMonth;
            --iMonth;
            --monCyl;
        }
        month = iMonth;
        day = offset + 1;
    }

    public static String getChinaDayString(int day) {
        String chineseTen[] = {"初", "十", "廿", "卅"};
        int n = day % 10 == 0 ? 9 : day % 10 - 1;
        if (day > 30)
            return "";
        if (day == 10)
            return "初十";
        else
            return chineseTen[day / 10] + chineseNumber[n];
    }
   
    public static String getChinaYearString(int day) {
        String[] y=new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        String yy=year+"";
        String rety="";
        for(int i=0;i<yy.length();i++){
            rety+=y[Integer.parseInt(yy.charAt(i)+"")];
        }
        return rety;
    }

    public String toString() {
        return "<html><b>详细信息</b>:<br>" +
                "<b>公历:</b>"+gyear+" 年 "+addZero(gmonth+1)+" 月 "+addZero(gday)+" 日<br>"+
                "<b>农历:</b>"+year+" 年 "+(leap ? "闰" : "")+addZero(month)+" 月 "+addZero(day)+ " 日<br>"+
                cyclical()+ "年【"+animalsYear()+"】<br>";
    }
   
    private String addZero(int i){
        String ret="";
        if(i<10)
            ret="0"+i;
        else
            ret=""+i;
        return ret;
    }

    public static void main(String[] args) throws ParseException {
        Calendar today = Calendar.getInstance();
      // today.setTime(chineseDateFormat.parse("2009年10月1日"));
        Lunar lunar = new Lunar(today);
        System.out.println("北京时间:" + chineseDateFormat.format(today.getTime()) + " " + lunar+lunar.cyclical());
    }
}

最后,TestWindow.java

package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestWindow extends JPanel  implements ActionListener {
    private JButton jb1 = new JButton("最大化");
    private JButton jb2 = new JButton("最小化");
    private JButton jb3 = new JButton("正常");
    private static TestWindow tw;
   
    public static TestWindow getInstance(){
        if(tw==null)
            tw=new TestWindow();
        return tw;
    }
   
    private TestWindow(){
        this.setLayout(new BorderLayout());
        this.add(jb1, BorderLayout.NORTH);
        this.add(jb2, BorderLayout.SOUTH);
        this.add(jb3, BorderLayout.CENTER);
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
       
    }
   
   
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        if (str.equals("最大化")) {
            MyFrame.getInstance().setExtendedState(JFrame.MAXIMIZED_BOTH);
        }
        if (str.equals("最小化")) {
            MyFrame.getInstance().setExtendedState(JFrame.ICONIFIED);
        }
        if (str.equals("正常")) {
            MyFrame.getInstance().setExtendedState(JFrame.NORMAL);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值