我是初学者。我想写一个天气应用程序,从网上获取一些信息并将其显示给用户。但是我无法将用户输入传递给JSON方法。有谁能够帮助我?
问题出在ActionListener部分。
import com.google.gson.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.imageio.*;
public class Weather extends JFrame implements ActionListener {
static JButton getWeather;
static JTextField inputZip, showCity, showState, showCondition, showTemp;
static JLabel enterZip, city, state, condition, temp, image;
public static double temp_f, temp_c;
static Image pic;
public static String zip, jCity, jState, jCondition, fORc;
public Weather() throws Exception {
//Button
getWeather = new JButton("Get Weather");
getWeather.addActionListener(this);
//TextFiels
inputZip = new JTextField(10);
showCity = new JTextField(10);
showState = new JTextField(10);
showCondition = new JTextField(10);
showTemp = new JTextField(10);
//Labels
enterZip = new JLabel ("Enter Zipcode:");
city = new JLabel ("City:");
state = new JLabel ("State:");
condition = new JLabel ("Condition:");
temp = new JLabel ("temp:");
//Radio Buttons
CheckboxGroup tUnit = new CheckboxGroup();
Checkbox f = new Checkbox ("f", tUnit, true);
Checkbox c = new Checkbox ("c", tUnit, false);
//Image
URL coldPicURL = new URL("https://cdn1.iconfinder.com/data/icons/xmas-color/512/snow_snowflake_winter_cold_weather-128.png");
URL hotPicURL = new URL("http://yowindow.com/img/yo_128.png");
URL picURL = new URL ("http://findicons.com/files/icons/2796/metro_uinvert_dock/128/the_weather_channel.png");
if (temp_f!=0 && temp_f<=60)
pic = ImageIO.read(coldPicURL);
else if (temp_f > 60)
pic = ImageIO.read(hotPicURL);
else
pic = ImageIO.read(picURL);
image = new JLabel(new ImageIcon(pic));
//Frame
JFrame weather = new JFrame ("Weather App");
weather.setVisible(true);
weather.setSize(500,250);
weather.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Panels
JPanel pInput = new JPanel();
JPanel pDisplay = new JPanel();
JPanel pDisplayInfo = new JPanel();
JPanel pTempUnits = new JPanel();
JPanel pImage = new JPanel();
//Panels' Layout
pInput.setLayout(new FlowLayout());
pDisplay.setLayout(new BorderLayout());
pDisplayInfo.setLayout(new GridLayout(5,2));
pTempUnits.setLayout(new FlowLayout());
//Frame Layout
weather.add(pInput, BorderLayout.NORTH);
weather.add(pDisplay, BorderLayout.CENTER);
pDisplay.add(pDisplayInfo, BorderLayout.NORTH);
pDisplay.add(pTempUnits, BorderLayout.CENTER);
weather.add(pImage, BorderLayout.EAST);
//Insertion the objects into the panels
pInput.add(enterZip);
pInput.add(inputZip);
pInput.add(getWeather);
pDisplayInfo.add(city);
pDisplayInfo.add(showCity);
pDisplayInfo.add(state);
pDisplayInfo.add(showState);
pDisplayInfo.add(condition);
pDisplayInfo.add(showCondition);
pDisplayInfo.add(temp);
pDisplayInfo.add(showTemp);
pTempUnits.add(c);
pTempUnits.add(f);
pImage.add(image);
}
public void actionPerformed(ActionEvent e) {
zip = Weather.inputZip.getText();
//HERE'S WHERE I'M STUCKED !!! :(
getJson (zip);
showCity.setText(jCity);
showState.setText(jState);
showCondition.setText(jCondition);
if (fORc.equals("f"))
showTemp.setText(Double.toString(temp_f));
if (fORc.equals("c"))
showTemp.setText(Double.toString(temp_c));
}
public static void getJson(String zip) throws Exception {
String json="", line;
JsonElement jse;
final String key = "7b86aadc43344a90";
JsonParser parser = new JsonParser();
URL url = new URL("http://api.wunderground.com/api/" +
key + "/conditions/q/" + zip + ".json");
InputStream is = url.openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
while ((line = rd.readLine()) != null)
json += line;
rd.close();
jse = parser.parse(json);
jCity=jse.getAsJsonObject().get("current_observation").getAsJsonObject().get("display_location").getAsJsonObject().get("city").getAsString();
jState=jse.getAsJsonObject().get("current_observation").getAsJsonObject().get("display_location").getAsJsonObject().get("state").getAsString();
jCondition=jse.getAsJsonObject().get("current_observation").getAsJsonObject().get("weather").getAsString();
temp_f=jse.getAsJsonObject().get("current_observation").getAsJsonObject().get("temp_f").getAsDouble();
temp_c=jse.getAsJsonObject().get("current_observation").getAsJsonObject().get("temp_c").getAsDouble();
}
public void itemStateChanged (ItemEvent ie){
Checkbox cb = (Checkbox)ie.getItemSelectable();
fORc = cb.getLabel();
}
public static void main(String[] args) throws Exception {
Weather w = new Weather();
}
}