public WFrame() { today = null; GridBagLayout layout = new GridBagLayout(); // , , setTitle("Weather"); setPreferredSize(new Dimension(350, 300)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); contentPane = getContentPane(); panel = new JPanel(); panel.setLayout(layout); idField = new JtextField(); idField.setColumns(6); // xml, // btn = new JButton(" "); // , GUI , label = new JLabel(" ID "); label.setFont(new Font("Verdana", Font.PLAIN, 14)); tLabel = new JLabel(" "); tLabel.setFont(new Font("Verdana", Font.PLAIN, 14)); iLabel = new JLabel(); panel.add(label, new GBC(0, 0, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(iLabel, new GBC(0, 1, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(btn, new GBC(0, 3, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(idField, new GBC(0, 2, 2, 1).setAnchor(GBC.NORTH) .setFill(GBC.HORIZONTAL).setIpad(50, 0)); panel.add(tLabel, new GBC(0, 4, 2, 1).setAnchor(GBC.NORTH) .setFill(GBC.HORIZONTAL).setIpad(50, 0)); add(panel); panel.setComponentPopupMenu(menu); pack(); }
JPopupMenu menu = new JPopupMenu(); menu.add(new AbstractAction("") { @Override public void actionPerformed(ActionEvent e) { setCity("27962"); } });
public class Weather { String city; String weatherType; String imgType; String humidity;// String tom, tomNight; int temperature; public String toString() { return "Weather[city= " + city + ", weatherType=" + weatherType + ", temperature= " + temperature + ",humidity= " + humidity + "]"; } }
Document doc = null; URL url = new URL("http://export.yandex.ru/weather-ng/forecasts/" + cityID + ".xml"); // , // URLConnection uc = url.openConnection(); InputStream is = uc.getInputStream();// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(is);// doc.getDocumentElement().normalize();
nl = doc.getElementsByTagName("forecast").item(0).getChildNodes();
try { Document doc = null; URL url = new URL("http://export.yandex.ru/weather-ng/forecasts/" + cityID + ".xml"); // , // URLConnection uc = url.openConnection(); InputStream is = uc.getInputStream();// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(is);// doc.getDocumentElement().normalize(); nl = doc.getElementsByTagName("forecast").item(0).getChildNodes(); // } catch (Exception ex) { JOptionPane.showMessageDialog(null, " "); ex.printStackTrace(); }
for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if (child instanceof Element) { if (child.getNodeName().equals("fact")) { Node childOfChild = null; for (int j = 0; j < child.getChildNodes().getLength(); j++) { childOfChild = child.getChildNodes().item(j); if ("station".equals(childOfChild.getNodeName())) { todayWeather.city = childOfChild.getTextContent(); } if ("temperature".equals(childOfChild.getNodeName())) todayWeather.temperature = Integer .parseInt(childOfChild.getTextContent()); if ("weather_type_short".equals(childOfChild .getNodeName())) { todayWeather.weatherType = childOfChild .getTextContent(); } if ("image".equals(childOfChild.getNodeName())) { todayWeather.imgType = childOfChild .getTextContent(); } if ("humidity".equals(childOfChild.getNodeName())) { todayWeather.humidity = childOfChild .getTextContent(); } } } if (child.getNodeName().equals("informer")) { Node childOfChild = null; for (int j =0; j<child.getChildNodes().getLength(); j++){ childOfChild = child.getChildNodes().item(j); if ((childOfChild.getNodeName().equals("temperature")) && (childOfChild.getAttributes().item(1).getTextContent().equals("night"))) { todayWeather.tomNight = childOfChild.getTextContent(); } if (childOfChild.getNodeName().equals("temperature") && (childOfChild.getAttributes().item(1).getTextContent().equals("tomorrow"))) { todayWeather.tom = childOfChild.getTextContent();} } } } } WFrame.today = todayWeather;
btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String cityID = idField.getText(); if (cityID == null) throw new IOException(" ID "); WeatherParser.parse(cityID); // label.setText(today.city + "(): " + today.temperature + " ; " + today.weatherType + " :" + today.humidity + " %"); iLabel = new JLabel(new ImageIcon(ImageIO.read(new URL( "http://img.yandex.net/i/wiz" + today.imgType + ".png")))); tLabel.setText(" /: "+ today.tomNight+"/"+ today.tom); // } catch (IOException e1) { e1.printStackTrace(); } contentPane.add(iLabel); } });
<code> public class WFrame extends JFrame { static Weather today; JPanel panel; JTextField idField; JButton btn; JLabel label; JLabel tLabel; JLabel iLabel; Box b; static String id; static int t; Container contentPane; public WFrame() { today = null; JPopupMenu menu = new JPopupMenu(); menu.add(new AbstractAction("") { @Override public void actionPerformed(ActionEvent e) { setCity("27962"); } }); //, , // http://weather.yandex.ru/static/cities.xml GridBagLayout layout = new GridBagLayout(); // , , setTitle("Weather"); setPreferredSize(new Dimension(350, 300)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); contentPane = getContentPane(); panel = new JPanel(); panel.setLayout(layout); idField = new JTextField(); idField.setColumns(6); idField.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent arg0) { } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { btn.doClick(); } } @Override public void keyPressed(KeyEvent arg0) { } }); // xml, // btn = new JButton(" "); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String cityID = idField.getText(); if (cityID == null) throw new IOException(" ID "); WeatherParser.parse(cityID); // label.setText(today.city + "(): " + today.temperature + " ; " + today.weatherType + " :" + today.humidity + " %"); iLabel = new JLabel(new ImageIcon(ImageIO.read(new URL( "http://img.yandex.net/i/wiz" + today.imgType + ".png")))); tLabel.setText(" /: "+ today.tomNight+"/"+ today.tom); // } catch (IOException e1) { e1.printStackTrace(); } contentPane.add(iLabel); } }); // , GUI , label = new JLabel(" ID "); label.setFont(new Font("Verdana", Font.PLAIN, 14)); tLabel = new JLabel(" "); tLabel.setFont(new Font("Verdana", Font.PLAIN, 14)); iLabel = new JLabel(); panel.add(label, new GBC(0, 0, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(iLabel, new GBC(0, 1, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(btn, new GBC(0, 3, 2, 1).setFill(GBC.NORTH).setWeight(100, 0)); panel.add(idField, new GBC(0, 2, 2, 1).setAnchor(GBC.NORTH) .setFill(GBC.HORIZONTAL).setIpad(50, 0)); panel.add(tLabel, new GBC(0, 4, 2, 1).setAnchor(GBC.NORTH) .setFill(GBC.HORIZONTAL).setIpad(50, 0)); add(panel); panel.setComponentPopupMenu(menu); pack(); } public static void main(String[] args) { // loginWithProxy(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { WFrame frame = new WFrame(); frame.setDefaultLookAndFeelDecorated(true); } }); } public void setCity(String id) { this.id = id; idField.setText(id); } }
public class Weather { String city; String weatherType; String imgType; String humidity;// String tom, tomNight; int temperature; public String toString() { return "Weather[city= " + city + ", weatherType=" + weatherType + ", temperature= " + temperature + ",humidity= " + humidity + "]"; } }
public class WeatherParser { static Weather tomorrowWeather; WeatherParser(WFrame f) { tomorrowWeather = new Weather(); } public static void parse(String cityID) { Weather todayWeather = new Weather(); NodeList nl = null; try { Document doc = null; URL url = new URL("http://export.yandex.ru/weather-ng/forecasts/" + cityID + ".xml"); // , // URLConnection uc = url.openConnection(); InputStream is = uc.getInputStream();// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(is);// doc.getDocumentElement().normalize(); nl = doc.getElementsByTagName("forecast").item(0).getChildNodes(); // } catch (Exception ex) { JOptionPane.showMessageDialog(null, " "); ex.printStackTrace(); } for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if (child instanceof Element) { if (child.getNodeName().equals("fact")) { Node childOfChild = null; for (int j = 0; j < child.getChildNodes().getLength(); j++) { childOfChild = child.getChildNodes().item(j); if ("station".equals(childOfChild.getNodeName())) { todayWeather.city = childOfChild.getTextContent(); } if ("temperature".equals(childOfChild.getNodeName())) todayWeather.temperature = Integer .parseInt(childOfChild.getTextContent()); if ("weather_type_short".equals(childOfChild .getNodeName())) { todayWeather.weatherType = childOfChild .getTextContent(); } if ("image".equals(childOfChild.getNodeName())) { todayWeather.imgType = childOfChild .getTextContent(); } if ("humidity".equals(childOfChild.getNodeName())) { todayWeather.humidity = childOfChild .getTextContent(); } } } if (child.getNodeName().equals("informer")) { Node childOfChild = null; for (int j =0; j<child.getChildNodes().getLength(); j++){ childOfChild = child.getChildNodes().item(j); if ((childOfChild.getNodeName().equals("temperature")) && (childOfChild.getAttributes().item(1).getTextContent().equals("night"))) { todayWeather.tomNight = childOfChild.getTextContent(); } if (childOfChild.getNodeName().equals("temperature") && (childOfChild.getAttributes().item(1).getTextContent().equals("tomorrow"))) { todayWeather.tom = childOfChild.getTextContent();} } } } } WFrame.today = todayWeather; } }
import java.awt.GridBagConstraints; public class GBC extends GridBagConstraints { public GBC(int gridx, int gridy) { this.gridx = gridx; this.gridy = gridy; } public GBC(int gridx, int gridy, int gridwidth, int gridheight) { this.gridx = gridx; this.gridy = gridy; this.gridwidth = gridwidth; this.gridheight = gridheight; } public GBC setAnchor(int anchor) { this.anchor = anchor; return this; } public GBC setFill(int fill) { this.fill = fill; return this; } public GBC setWeight(double weightx, double weighty) { this.weightx = weightx; this.weighty = weighty; return this; } public GBC setInsets(int distance) { this.insets = new java.awt.Insets( distance, distance, distance, distance); return this; } public GBC setInsets(int top, int left, int bottom, int right) { this.insets = new java.awt.Insets( top, left, bottom, right); return this; } public GBC setIpad(int ipadx, int ipady) { this.ipadx = ipadx; this.ipady = ipady; return this; } }
Source: https://habr.com/ru/post/164101/
All Articles