gpt4 book ai didi

java - JTabbedPane 中的 XML 解析和显示

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:45 25 4
gpt4 key购买 nike

我是 java 新手,我已经解析了 XML,现在我必须在相应的 JTabbedPane 中显示它。我在启动元素方法的属性对象上出现错误。

我哪里出错了?

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class JTabbedPaneDemo extends JPanel {

/*static String[] nameatt= {" sv_machineData "," Mould_data "," Core_1 "," Core_2 "};*/


public JTabbedPaneDemo() {
// ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");

JTabbedPane jtbExample = new JTabbedPane();

// Create First Tab.
JPanel jplInnerPanel1 = createInnerPanel();
jtbExample.addTab(" Sv_Machine_Data ", jplInnerPanel1);
jplInnerPanel1.setLayout(new BorderLayout());
JTable table1 = new JTable(5,3);
JScrollPane tableContainer = new JScrollPane(table1);
jplInnerPanel1.add(tableContainer,BorderLayout.CENTER);

//jtbExample.setSelectedIndex(0);

// Create Second Tab.
JPanel jplInnerPanel2 = createInnerPanel();
jtbExample.addTab(" Mould_data ", jplInnerPanel2);
jplInnerPanel2.setLayout(new BorderLayout());
JTable table2 = new JTable(5,3);
JScrollPane tableContainer1 = new JScrollPane(table2);
jplInnerPanel2.add(tableContainer1,BorderLayout.CENTER);

// Create Third Tab.
JPanel jplInnerPanel3 = createInnerPanel();
jtbExample.addTab(" Core_1 ", jplInnerPanel3);
jplInnerPanel3.setLayout(new BorderLayout());
JTable table3 = new JTable(5,3);
JScrollPane tableContainer3 = new JScrollPane(table3);
jplInnerPanel3.add(tableContainer3,BorderLayout.CENTER);

// Create Fourth Tab.
JPanel jplInnerPanel4 = createInnerPanel();
jtbExample.addTab(" Core_2 ", jplInnerPanel4);
jplInnerPanel4.setLayout(new BorderLayout());
JTable table4 = new JTable(5,3);
JScrollPane tableContainer4 = new JScrollPane(table4);
jplInnerPanel4.add(tableContainer4,BorderLayout.CENTER);


// Add the Tabbed Pane to this panel.
setLayout(new GridLayout(1, 1));
add(jtbExample);
}

protected JPanel createInnerPanel() {
JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel();
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane Source Demo");
frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

/*final JTabbedPaneDemo xm = new JTabbedPaneDemo();*/

try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean bfname = false;
boolean blname = false;
String nameAttribute;

public void startElement(String uri, String localName,String qName, Attributes attributes)
throws SAXException {

if (qName.equalsIgnoreCase("TYP")) {
bfname = true;
}

nameAttribute = attributes.getValue("Name");

if (qName.equalsIgnoreCase("VALUE")){
blname = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {

if (bfname) {
System.out.println("Type : "+ new String(ch, start, length));
String vtype = new String(ch, start, length);
/*xm.textArea.append(vtype+"\n");*/
bfname = false;
}

if (nameAttribute != null && !nameAttribute.equals("")) {
System.out.println("Name : " + nameAttribute);
/*vname+=nameAttribute+ ", " +"\n";
xm.textArea_3.setText(vname);
*/}

if (blname) {
String vvalue = new String(ch, start, length);
System.out.println("Value:" + Double.valueOf(vvalue));
/*xm.textArea_1.append(Double.valueOf(vvalue)+"\n");*/
blname = false;
}
}
};

saxParser.parse(new File("C:"path of xml file".xml"), handler);
}
catch (Exception e) {
e.printStackTrace();
}
frame.getContentPane().add(new JTabbedPaneDemo(),BorderLayout.CENTER);
frame.setSize(400, 500);
frame.setVisible(true);
}
}

下面是示例 XML

<?xml version="1.0" encoding="UTF-8"?>
<HMI_Data Version="1.0" MaschinenNR.="XXXXXX" Date="21-10-2009">
<VarGroup Name="VG_MachineData">
<Variable Name="Mold1.sv_rMoldStroke">
<Typ>REAL</Typ>
<Value>6.000000e+02</Value>
</Variable>
<Variable Name="Core1.sv_rMaxSpeedFwd">
<Typ>REAL</Typ>
<Value>5.000000e+01</Value>
</Variable>
<Variable Name="Core1.sv_rMaxSpeedBwd">
<Typ>REAL</Typ>
<Value>5.000000e+01</Value>
</Variable>
<Variable Name="Core1.sv_rMaxPressureFwd">
<Typ>REAL</Typ>
<Value>1.450000e+02</Value>
</Variable>
<Variable Name="Core1.sv_rMaxPressureBwd">
<Typ>REAL</Typ>
<Value>1.450000e+02</Value>
</Variable>

最佳答案

JavaDcos 大约 20 秒表明:

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

应该是:

import org.xml.sax.SAXException;
import org.xml.sax.Attributes; // import it!
import org.xml.sax.helpers.DefaultHandler;

关于java - JTabbedPane 中的 XML 解析和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16562649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com