public class TestApplet extends java.applet.Applet {
我們提供的服務(wù)有:成都網(wǎng)站制作、成都做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、莒縣ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的莒縣網(wǎng)站制作公司
/** Initializes the applet TestApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(null);
jLabel1.setText("\u65701\uff1a");
jPanel1.add(jLabel1);
jLabel1.setBounds(70, 30, 30, 15);
jPanel1.add(jTextField1);
jTextField1.setBounds(110, 30, 90, 21);
jLabel2.setText("\u65702\uff1a");
jPanel1.add(jLabel2);
jLabel2.setBounds(70, 70, 30, 15);
jPanel1.add(jTextField2);
jTextField2.setBounds(110, 60, 90, 21);
jButton1.setText("\u5929\u6c14");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton1.setBounds(120, 110, 57, 23);
add(jPanel1, java.awt.BorderLayout.CENTER);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jTextField1.getText().equals(jTextField2.getText())) {
JOptionPane.showMessageDialog(null, "今天天氣很好", "天氣", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "今天天氣很壞", "天氣", JOptionPane.INFORMATION_MESSAGE);
}
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
//通過(guò)中國(guó)天氣api調(diào)用
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("");
ResponseHandlerString responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //溫度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天氣情況
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //風(fēng)向風(fēng)力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//溫度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天氣情況
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//風(fēng)向風(fēng)力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//溫度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天氣情況
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判斷是否有轉(zhuǎn)
if(weather1.indexOf("轉(zhuǎn)") 0){
//有
j_weather_part1 = weather1.split("轉(zhuǎn)")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("轉(zhuǎn)") 0){
//有
j_weather_part2 = weather2.split("轉(zhuǎn)")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "轉(zhuǎn)" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判斷是否有轉(zhuǎn)
if(weather2.indexOf("轉(zhuǎn)") 0){
//有
m_weather_part1 = weather2.split("轉(zhuǎn)")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("轉(zhuǎn)") 0){
//有
m_weather_part2 = weather3.split("轉(zhuǎn)")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "轉(zhuǎn)" + m_weather_part2;//明天
}
//風(fēng)向風(fēng)力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}
import?java.util.Scanner;
public?class?Weather?{
public?static?String?getWeatherInChinese(char?c)?{
switch?(c)?{
case?'D':
return?"干燥";
case?'M':
return?"潮濕";
case?'H':
return?"炎熱";
case?'R':
return?"下雨";
default:
return?"輸入錯(cuò)誤,無(wú)法轉(zhuǎn)換";
}
}
public?static?void?main(String[]?args)?{
boolean?tbd?=?true;
do?{
System.out.print("請(qǐng)輸入天氣情況的第一個(gè)英文字母:");
Scanner?scanner?=?new?Scanner(System.in);
String?input?=?scanner.next().trim();
if?(input.length()?!=?1)?{
System.out.println("輸入錯(cuò)誤,無(wú)法轉(zhuǎn)換!");
}?else?{
System.out.println(getWeatherInChinese(input.charAt(0)));
}
System.out.print("你想繼續(xù)嗎?(y/n)");
String?sta?=?scanner.next();
tbd?=?sta.equals("y")???true?:?false;
}?while?(tbd);
System.out.println("退出系統(tǒng)!");
}
}
運(yùn)行截圖:
天氣晴天:it's sunny today.
有風(fēng):it's windy outside.
多云是cloudy,下雨是rainy,也有sprinkle是小雨,storm 暴雨
hurricane颶風(fēng),tornado龍卷風(fēng)等等多種.
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String weather="";
String answer="";
do
{
System.out.println("請(qǐng)輸入第一個(gè)字母(d,m,h,r)");
//接收輸入的天氣
weather=input.next();
//根據(jù)輸入判斷
if(weather.equalsIgnoreCase("d"))
{
System.out.println("干燥");
}
if(weather.equalsIgnoreCase("m"))
{
System.out.println("潮濕");
}
if(weather.equalsIgnoreCase("h"))
{
System.out.println("炎熱");
}
if(weather.equalsIgnoreCase("r"))
{
System.out.println("下雨");
}
else
{
System.out.println("輸入錯(cuò)誤");
}
System.out.println("是否繼續(xù)輸入?");
//接收是否輸入為n
answer=input.next();
}while(!answer.equalsIgnoreCase("n"));
}
}
此處equalsIgnoreCase是不區(qū)分大小寫(xiě)的意思
如果要區(qū)分,則把他改成equals即可
①取出地址中的返回值(getWeatherReader方法)
②解析json格式的字符串
③形成你要展示的天氣預(yù)報(bào)效果
public static String getWeatherReader() {//取得接口字符串
String currentLine = "";
String strReturn = "";
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
BufferedReader buff = null;
try {
url = new URL("");
System.out.println(url.toURI());
//打開(kāi)地址鏈接
conn = (HttpURLConnection)url.openConnection();
conn.connect();
//接收數(shù)據(jù)
in = conn.getInputStream();
//如有亂碼注意編碼方式,如:UTF-8
buff = new BufferedReader(new InputStreamReader(in, "gb2312"));
while((currentLine = buff.readLine()) != null) {
strReturn += currentLine;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
buff.close();
} catch (IOException e) {
return "8EF0000";
}
}
return strReturn;
}