[英]JLabel's text is not changing
EDIT: Huge code re-organisation, old question here: http://pastebin.com/Mbg4dYiY 编辑:巨大的代码重组,旧问题在这里: http : //pastebin.com/Mbg4dYiY
I have created a basic program that is designed to show the weather in a window using Swing. 我创建了一个基本程序,该程序旨在使用Swing在窗口中显示天气。 I am developing using IntelliJ and I have used the UI builder in that.
我正在使用IntelliJ进行开发,并且在其中使用了UI构建器。 I am attempting to fetch some information from the Weather Underground servers and then make a JLabel called
weatherlabel
display this information. 我试图从Weather Underground服务器获取一些信息,然后使一个名为
weatherlabel
的JLabel显示此信息。 However, the JLabel doesn't actually change in the window; 但是,JLabel实际上并未在窗口中更改; it just stays as 'Weather Will Go Here'.
它只是停留在“天气会在这里”。 How do I fix this?
我该如何解决?
Here is my main.java: 这是我的main.java:
public class main {
public static void main(String[] args) {
System.out.println("Hello World!");
Display d = new Display();
d.getandsetWeather();
}
}
Here is my Display.java: 这是我的Display.java:
public class Display {
Display disp = this;
public JPanel myPanel;
public JLabel weatherfield;
private JButton button1;
public void init() {
JFrame frame = new JFrame("Display");
frame.setContentPane(new Display().myPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(480, 234));
frame.pack();
frame.setVisible(true);
}
public void getandsetWeather() {
String editedline = null;
init();
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Send data
URL url = new URL("http://api.wunderground.com/api/772a9f2cf6a12db3/geolookup/conditions/q/UK/Chester.json");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
if( line.contains("\"weather\":")) {
System.out.println(line);
editedline = line.replace("\"weather\":\"", "");
editedline = editedline.replace("\",", "");
System.out.println(editedline);
weatherfield.setText(editedline);
}
}
wr.close();
rd.close();
weatherfield.setText(editedline);
System.out.println(weatherfield.getText());
weatherfield.repaint();
weatherfield.revalidate();
} catch (Exception e) {
System.out.println("Error!" + e);
}
}
}
When I run the program, this is printed to the log: 当我运行程序时,这将打印到日志中:
Hello World!
"weather":"Scattered Clouds",
Scattered Clouds
Scattered Clouds
You have to main
methods, one of which you are trying to call the other main
method. 您必须使用
main
方法,您要尝试调用其中一个main
方法。 Don't do that. 不要那样做 A program should only have one
main
method. 一个程序应该只有一种
main
方法。 You should never have to this 你永远都不必这样做
Display.main(new String[]{});
Why even have this ChangeWeatherLabelText
class? 为什么还要设置
ChangeWeatherLabelText
类? There is only one method, that doesn't seem to be needed in it's own class. 只有一种方法,在它自己的类中似乎并不需要。 Your instantiation if
Display
in that method does nothing to the rest of the program. 如果用该方法
Display
,则实例化对程序的其余部分没有任何作用。 So you call has no effect on the label. 因此,您的呼叫对标签没有影响。
GetWeather
just seems like a helper class with a helper method. GetWeather
似乎就像是带有帮助程序方法的帮助程序类。 "Helper" class methods are useless if they don't return something. UPDATE with OP update of code 更新的代码OP更新
Test this out and be sure to read the comments so you can see what I did. 测试一下,并确保阅读注释,以便您可以看到我的所作所为。 Let me know if you have any question.
如果您有任何问题,请告诉我。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class main {
public static void main(String[] args) {
System.out.println("Hello World!");
new Display(); // <-- just instantiate
}
}
class Display {
Display disp = this;
public JPanel myPanel; // <--------- Haven't been initialized
public JLabel weatherfield;
private JButton button1;
public Display() { // you need constructor to call init
init();
}
public void init() {
myPanel = new JPanel(new BorderLayout()); // initialize
weatherfield = new JLabel(" "); // initialize
button1 = new JButton("Button"); // initialize
myPanel.add(weatherfield, BorderLayout.CENTER);
myPanel.add(button1, BorderLayout.SOUTH);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
getandsetWeather(); // <-------- add listener to call getandsetweather
}
});
JFrame frame = new JFrame("Display");
frame.setContentPane(myPanel); // <--------------------- fix 1
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(480, 234));
frame.pack();
frame.setVisible(true);
}
public void getandsetWeather() {
String editedline = null;
init();
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Send data
URL url = new URL("http://api.wunderground.com/api/772a9f2cf6a12db3/geolookup/conditions/q/UK/Chester.json");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
if( line.contains("\"weather\":")) {
System.out.println(line);
editedline = line.replace("\"weather\":\"", "");
editedline = editedline.replace("\",", "");
System.out.println(editedline);
weatherfield.setText(editedline);
}
}
wr.close();
rd.close();
weatherfield.setText(editedline);
System.out.println(weatherfield.getText());
weatherfield.repaint();
weatherfield.revalidate();
} catch (Exception e) {
System.out.println("Error!" + e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.