我正在构建一个非常小的Java Applet示例。通常,我使用C#而不是Java编程。不要责怪我没有使用布局管理器,我知道事情可能更有效率。 我的问题是我无法在标签或文本字段上显示保存在名为“weerAttribuutValueLabelList”的ArrayList中的XML值。 当我检查ArrayList时,所有从XML文件接收的值都存在。
它必须是一个小小的愚蠢的东西,它与C#或我认为的类似。我希望有人可以帮助我,我已经做了很多努力找到原因,但现在我已经没有想法了。非常感谢你。
代码块:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class ReadXml extends Applet implements ActionListener
{
NodeList xmlValueList;
ArrayList<Label> weerAttribuutLabelList;
ArrayList<Label> weerAttribuutValueLabelList;
int weerAttribuutLabelMargin = 20;
int weerAttribuutValueLabelMargin = 20;
public void init()
{
// Location for every generated label
weerAttribuutLabelList = new ArrayList <Label>();
weerAttribuutValueLabelList = new ArrayList <Label>();
// Generate the static labels with defined text label at first
staticLabel();
// Download the xml, put the xml values in a label and save them in the ArrayList
readXmlFile();
labelAligning();
}
// Every static label gets a defined text label from the array
public void staticLabel()
{
String[] regentHetInXmlNames = {"Datum:", "Tijd:", "Temperatuur (°C):", "Trend (/uur):", "Max. temperatuur:", "Min. temperatuur:", "Gevoelstemperatuur:", "Min. gevoelstemp.:", "Dauwpunt:", "Trend (/uur):", "Max. dauwpunt:", "Min. dauwpunt:", "Luchtvochtigheid:", "Trend (/uur):", "Max. luchtvochtigheid:", "Min. luchtvochtigheid:", "Neerslag:", "Luchtdruk:", "Trend (/uur):", "Max. luchtdruk:", "Min. luchtdruk", "Windsnelheid:", "Windsnelheid (10 min):", "Max. windstoot:", "Windrichting:", "UV:", "Max. UV:"};
for(String name : regentHetInXmlNames)
{
weerAttribuutLabelList.add(new Label(name));
}
}
// Read xml
public void readXmlFile()
{
try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document regenthetinXml = dBuilder.parse(new URL("http://regenthet.in/data/regenthetin.xml").openStream());
regenthetinXml.getDocumentElement().normalize();
String[] regentHetInXmlNodes = {"stationDate", "stationTime", "outsideTemp", "tempChangeLastHour", "hiOutsideTemp", "lowOutsideTemp", "windchill", "lowWindchill", "outsideDewPt", "dewChangeLastHour", "hiDewpoint", "lowDewpoint", "outsideHumidity", "humChangeLastHour", "hiHumidity", "lowHumidity", "dailyRain", "pressure", "pressChangeLastHour", "hiPressure", "lowPressure", "windSpeed", "wind10Avg", "hiWindSpeed", "windDirection", "uv", "hiUV"};
xmlValueList = regenthetinXml.getElementsByTagName("station_almelo");
Node nNode = xmlValueList.item(0);
Element eElement = (Element) nNode;
for(int i = 0; i <= 26; i++)
{
weerAttribuutValueLabelList.add(new Label(eElement.getElementsByTagName(regentHetInXmlNodes[i]).item(0).getTextContent()));
//System.out.println(eElement.getElementsByTagName(regentHetInXmlNodes[i]).item(0).getTextContent());
}
}
catch(Exception e)
{}
}
public void labelAligning()
{
// Visual default aspects
setLayout(null);
setBackground(Color.lightGray);
for(Label item : weerAttribuutLabelList)
{
item.setBounds(20,weerAttribuutLabelMargin,150,20);
add(item);
weerAttribuutLabelMargin = weerAttribuutLabelMargin + 20;
//System.out.println(item);
}
for(Label item : weerAttribuutValueLabelList)
{
item.setBounds(250,weerAttribuutValueLabelMargin,150,20);
add(item);
weerAttribuutValueLabelMargin = weerAttribuutValueLabelMargin + 20;
System.out.println(item);
}
}
public void actionPerformed(ActionEvent event)
{
}
}
答案 0 :(得分:0)
您想要的布局需要两列,所以至少可以使用GridLayout(26,2)或类似的并添加标签。看看会发生什么,然后你可以在学习时使用其他布局。