繁体   English   中英

JFrame中的天气API显示

[英]Weather API display in JFrame

我试图创建一个显示天气的基于Java / Swing的应用程序。 到目前为止,我已经创建了背景和textfield +按钮来获取位置,但是我不知道如何连接它,因此它显示并将背景更改为另一幅图像。 如果这是一个菜鸟问题,我感到很抱歉,但是我之前从未做过Java(只是处理和arduino加上网页设计),而我的大学强迫我使用高级Java,但我以前从未做过类似的事情。

到目前为止,这是我的代码:

package AppPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ApplicationWidget extends JFrame implements ActionListener {

    ImageIcon basic;
    JLabel label1;
    JFrame frame;
    JLabel label;
    JTextField textfield;
    JButton button;




    public static void main (String[]args){        
        ApplicationWidget gui = new ApplicationWidget();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(320, 480);






    }

    public ApplicationWidget() {

        setLayout(new FlowLayout());
        WeatherAPI weather = new WeatherAPI("44418");
        System.out.println(WeatherAPI.theWeatherRSS);
        for(int i = 0; i < WeatherAPI.weatherForecastList.size(); i++)
        {
            System.out.println(WeatherAPI.weatherForecastList.get(i).lowTemp + " " +
            WeatherAPI.weatherForecastList.get(i).highTemp);
        }

        label = new JLabel("Welcome! Please Enter your location");
        add(label);

        textfield = new JTextField(15);
        add(textfield);
        for(int i = 0; i < WeatherAPI.weatherForecastList.size(); i++)
        {
            System.out.println(WeatherAPI.weatherForecastList.get(i).lowTemp + " " +
            WeatherAPI.weatherForecastList.get(i).highTemp);
        }


        button = new JButton("Check weather");

        add(button);



        basic = new ImageIcon(getClass().getResource("basicback.jpg"));
        label1 = new JLabel(basic);
        add(label1);

        /*add design here*/
        /*add mouse interaction*/
        /*add image capture*/   
}

    @Override
    public void actionPerformed(ActionEvent e){

        JButton button = (JButton) e.getSource();
        if (e.getSource() == button){
            String data = textfield.getText();
            System.out.println(data);
        }

    }





}

和WeatherAPI代码:

    package AppPackage;
import java.net.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.io.*;

public class WeatherAPI
{
    static String theWeatherRSS;
    static String theCity;
    static ArrayList<Forecast> weatherForecastList;

    //WeatherAPI(String string) {
   //     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   // }

    public class Forecast
    {
        String lowTemp;
        String highTemp;
    }

    /**
     *
     * @param city
     */
    public WeatherAPI(String city)
    {
        theCity = city;
        theWeatherRSS = getWeatherAsRSS(city);
        parseWeather(theWeatherRSS);
    }

    void parseWeather(String weatherHTML)
    {
        weatherForecastList = new ArrayList<Forecast>();
        int startIndex = 0;
        while(startIndex != -1)
        {
            startIndex = weatherHTML.indexOf("<yweather:forecast", startIndex);
            if(startIndex != -1)
            { // found a weather forecast
                int endIndex = weatherHTML.indexOf(">", startIndex);
                String weatherForecast = weatherHTML.substring(startIndex, endIndex+1);

                // get temp forecast                
                String lowString = getValueForKey(weatherForecast, "low");
                String highString = getValueForKey(weatherForecast, "high");

                Forecast fore = new Forecast();
                fore.lowTemp = lowString;
                fore.highTemp = highString;
                weatherForecastList.add(fore);

                // move to end of this forecast
                startIndex = endIndex;
            }
        }
    }

    String getValueForKey(String theString, String keyString)
    {
        int startIndex = theString.indexOf(keyString);
        startIndex = theString.indexOf("\"", startIndex);
        int endIndex = theString.indexOf("\"", startIndex+1);
        String resultString = theString.substring(startIndex+1, endIndex);
        return resultString;
    }

    String getWeatherAsRSS(String city)
    {
        try{
            /*
            Adapted from: http://stackoverflow.com/questions/1381617/simplest-way-to-correctly-load-html-from-web-page-into-a-string-in-java
            Answer provided by: erickson
            */
            URL url = new URL("http://weather.yahooapis.com/forecastrss?w="+city+"&u=c");
            URLConnection con = url.openConnection();
            Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
            Matcher m = p.matcher(con.getContentType());
            /* If Content-Type doesn't match this pre-conception, choose default and 
             * hope for the best. */
            String charset = m.matches() ? m.group(1) : "ISO-8859-1";
            Reader r = new InputStreamReader(con.getInputStream(), charset);
            StringBuilder buf = new StringBuilder();
            while (true) {
              int ch = r.read();
              if (ch < 0)
                break;
              buf.append((char) ch);
            }
            String str = buf.toString();
            return(str);
        }
        catch(Exception e) {System.err.println("Weather API Exception: "+e);}
        return null;
    }

}

感谢您的帮助,我真的很绝望,因为我混淆了提交日期,而且我没有多少时间了。

假设label1是您的背景标签,只需使用label1.setIcon(...) 您将传递给它的是一个new ImageIcon

另外,您还没有将ActionListener注册到您的按钮。 如果您没有为该按钮注册一个侦听器,它将不会执行任何操作。 做这个

button = new JButton("Check weather");
button.addActionListener(this);
add(button); 

您尚未指定新图像的来源,因此我真的无所不能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM