我正在使用 IntelliJ 和内置的表单设计器制作一个简单的天气应用程序。我已经制作并设计了一个表格并相应地编辑了绑定的类文件。当我实际运行代码时,我设计的表单现在确实显示出来了,实际上只有最后一行 (maxtemp) 是唯一显示在空白屏幕上的值。
PS我是用Gradle搭建的,我也设置了GUI设计器用Java源码代替字节码(因为Gradle不支持字节码)
import org.json.JSONException;
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
// Get the API
private API api = new API();
// Form elements
public JPanel mainPanel;
public JLabel text;
public JLabel category;
public JLabel mintemp;
public JLabel maxtemp;
public JLabel link;
public Frame() throws Exception, JSONException {
initFrame();
}
public void initFrame() throws Exception, JSONException {
// Get API response
JSONObject wjson = api.connection();
// Filter response and get data
String[] data = api.respFilter(wjson);
// Swing components
mainPanel = new JPanel(new BorderLayout());
text = new JLabel();
category = new JLabel();
mintemp = new JLabel();
maxtemp = new JLabel();
link = new JLabel();
// Add to the frame
add(mainPanel);
add(text);
add(category);
add(mintemp);
add(maxtemp);
text.setText(data[0]);
category.setText(data[1]);
mintemp.setText(data[3]);
maxtemp.setText(data[4]);
}
public static void main(String[] args) throws Exception, JSONException {
JFrame app = new Frame();
app.setTitle("Java-WeatherApp");;
app.setSize(900, 600);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setLocationRelativeTo(null);
app.setVisible(true);
}
}
我对 Java 很陌生,对 IntelliJ 上的表单构建器也很陌生。不幸的是,我在表单生成器上找不到任何好的教程。任何帮助表示赞赏!