在JavaSwing中设置文本

在JavaSwing中设置文本,java,swing,parsing,Java,Swing,Parsing,我必须解析一个xml文件并在GUI中显示所需的字段。我使用了sax解析器并在控制台中获得了所需的输出,但所有的值都没有显示在GUI中,除了vname,我在下面保留了我的代码 在这里输入代码 import java.awt.Dimension; import java.awt.Font; import java.awt.TextArea; import java.awt.TextField; import java.io.File; import javax.swing.JFrame; impor

我必须解析一个xml文件并在GUI中显示所需的字段。我使用了sax解析器并在控制台中获得了所需的输出,但所有的值都没有显示在GUI中,除了vname,我在下面保留了我的代码

在这里输入代码

import java.awt.Dimension;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;

public class exp1 {
    public static String vname = "";
    public static String vvalue;
    public static String vtype = null;
    private final JLabel lblName = new JLabel("Name");
    private final JLabel lblType = new JLabel("Type");
    private final JLabel lblValue = new JLabel("Value");
    private final JLabel lblNewLabel = new JLabel("Results");
    private final TextArea textArea_3 = new TextArea();
    private final TextArea textArea = new TextArea();
    private final TextArea textArea_1 = new TextArea();

public  exp1 (String a){

    JFrame frame = new JFrame();
    frame.getContentPane().setFont(new Font("Times New Roman", Font.BOLD, 13));

    frame.setSize(new Dimension(668, 517));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    this.lblName.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblName.setBounds(86, 60, 56, 17);

    frame.getContentPane().add(this.lblName);

    this.lblType.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblType.setBounds(519, 60, 56, 17);

    frame.getContentPane().add(this.lblType);
    this.lblValue.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblValue.setBounds(297, 61, 46, 14);

    frame.getContentPane().add(this.lblValue);
    this.lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 16));
    this.lblNewLabel.setBounds(34, 11, 86, 38);

    frame.getContentPane().add(this.lblNewLabel);
    this.textArea_3.setBounds(27, 110, 212, 230);

    frame.getContentPane().add(this.textArea_3);
    this.textArea.setBounds(438, 110, 193, 230);

    frame.getContentPane().add(this.textArea);

    this.textArea_1.setBounds(256, 110, 168, 230);

    frame.getContentPane().add(this.textArea_1);
    frame.setVisible(true);

}

public static void main(String argv[]) {



exp1 xm = new exp1(null);



try {

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

DefaultHandler handler = new DefaultHandler() {

boolean bfname = false;
boolean blname = false;
boolean bnname = 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;
        }

    if (qName.equalsIgnoreCase("VARIABLENAME")) {
        bnname = true;
        }
}

public void endElement(String uri, String localName,
String qName) throws SAXException {
}

public void characters(char ch[], int start, int length)
throws SAXException {

    if (bfname) {
        System.out.println("Type : "+ new String(ch, start, length));
        bfname = false;
        vtype = new String(ch, start, length);
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        }

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

    if (blname) {

        vvalue = new String(ch, start, length);
        System.out.println("Value:" + Double.valueOf(vvalue));
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        blname = false;
        }
}
};
saxParser.parse(new File("filepath.xml"), handler);  //for ex :-"New Folder\\VG_MachineData.xml"
xm.textArea_3.setText(vname);
xm.textArea.setText(vtype);
xm.textArea_1.setText(new Double(vvalue).toString());


} catch (Exception e) {
e.printStackTrace();
}
}


//xml tags
<?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>
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.TextArea;
导入java.awt.TextField;
导入java.io.File;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入javax.xml.parsers.SAXParser;
导入javax.xml.parsers.SAXParserFactory;
导入org.xml.sax.Attributes;
导入org.xml.sax.SAXException;
导入org.xml.sax.helpers.DefaultHandler;
导入javax.swing.JTextArea;
导入javax.swing.JEditorPane;
导入javax.swing.JPanel;
导入javax.swing.JButton;
导入javax.swing.JTextPane;
导入javax.swing.JTable;
导入javax.swing.JTabbedPane;
导入java.awt.BorderLayout;
公共类exp1{
公共静态字符串vname=“”;
公共静态字符串v值;
公共静态字符串vtype=null;
专用最终JLabel lblName=新JLabel(“名称”);
专用最终JLabel lblType=新JLabel(“类型”);
私有最终JLabel lblValue=新JLabel(“值”);
私人最终JLabel lblNewLabel=新JLabel(“结果”);
私有最终TextArea TextArea_3=新TextArea();
私有最终TextArea TextArea=新TextArea();
私有最终TextArea TextArea_1=新TextArea();
公共exp1(字符串a){
JFrame=新JFrame();
frame.getContentPane().setFont(新字体(“Times new Roman”,Font.BOLD,13));
框架设置尺寸(新尺寸(668517));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
this.lblName.setFont(新字体(“Times new Roman”,Font.BOLD,14));
这个.lblName.setBounds(86,60,56,17);
frame.getContentPane().add(this.lblName);
this.lblType.setFont(新字体(“Times new Roman”,Font.BOLD,14));
这个.lblType.setBounds(519,60,56,17);
frame.getContentPane().add(this.lblType);
this.lblValue.setFont(新字体(“Times new Roman”,Font.BOLD,14));
这个.lblValue.setBounds(297,61,46,14);
frame.getContentPane().add(此.lblValue);
this.lblNewLabel.setFont(新字体(“Times new Roman”,Font.BOLD,16));
这个.lblNewLabel.setBounds(34,11,86,38);
frame.getContentPane().add(this.lblNewLabel);
此.textArea_3.setBounds(27、110、212、230);
frame.getContentPane().add(this.textArea_3);
this.textArea.setBounds(438110193230);
frame.getContentPane().add(this.textArea);
这个.textArea_1.setBounds(256、110、168、230);
frame.getContentPane().add(this.textArea_1);
frame.setVisible(true);
}
公共静态void main(字符串argv[]){
exp1 xm=新的exp1(空);
试一试{
SAXParserFactory=SAXParserFactory.newInstance();
SAXParser SAXParser=factory.newSAXParser();
DefaultHandler=新的DefaultHandler(){
布尔bfname=false;
布尔blname=false;
布尔bnname=false;
字符串名称属性;
public void startElement(字符串uri、字符串localName、,
字符串(名称、属性)
抛出SAX异常{
if(qName.equalsIgnoreCase(“典型”)){
bfname=true;
}
nameAttribute=attributes.getValue(“名称”);
if(qName.equalsIgnoreCase(“值”)){
blname=true;
}
if(qName.equalsIgnoreCase(“VARIABLENAME”)){
bnname=true;
}
}
public void endElement(字符串uri、字符串localName、,
字符串(qName)引发异常{
}
公共无效字符(字符ch[],整数开始,整数长度)
抛出SAX异常{
if(bfname){
System.out.println(“类型:”+新字符串(ch,start,length));
bfname=false;
vtype=新字符串(ch、开始、长度);
//此处的值在JFRAME中仅显示一次
}
if(nameAttribute!=null&&!nameAttribute.equals(“”){
System.out.println(“名称:”+nameAttribute);
vname+=nameAttribute+”,“+”\n”;
}
如果(blname){
v值=新字符串(ch、开始、长度);
System.out.println(“值:”+Double.valueOf(vvalue));
//此处的值在JFRAME中仅显示一次
blname=false;
}
}
};
saxParser.parse(新文件(“filepath.xml”),handler);//对于ex:-“新文件夹\\VG_MachineData.xml”
xm.textArea_3.setText(vname);
xm.textArea.setText(vtype);
xm.textArea_1.setText(新的双精度(vvalue.toString());
}捕获(例外e){
e、 printStackTrace();
}
}
//xml标记
真实的
600万E+02
真实的
500万E+01
真实的
500万E+01
真实的
1.450000e+02

我实现了一个示例代码。请尝试这种解决方案

package sax;
导入javax.xml.parsers.SAXParser; 导入javax.xml.parsers.SAXParserFactory

导入org.xml.sax.Attributes; 导入org.xml.sax.SAXException; 导入org.xml.sax.helpers.DefaultHandler

公共类测试{

public static void main(String args[]) {

    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler() {
            String elementName = null;

            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {
                /*
                 * this method is called once the parser meets a start
                 * element and you should implement here how your code
                 * should behave when a start element is found.The name of
                 * the start element is the parameter qName and the
                 * attributes of that element is under attributes parameter.
                 */
                elementName = qName;
            }

            public void endElement(String uri, String localName,
                    String qName) throws SAXException {
                /*
                 * this method is called once the parser meets an end
                 * element and you should implement here how your code
                 * should behave when an end element is found.The name of
                 * the end element is the parameter qName.
                 */
            }

            public void characters(char ch[], int start, int length)
                    throws SAXException {
                /*
                 * this method is called once the parser encounters text
                 * under an element.
                 */
                if (elementName.equals("vname")) {
                 //set the vname textarea here
                }
                if (elementName.equals("vtype")) {
                    //set the vtype textarea here
                }
                if (elementName.equals("vvalue")) {
                    //set the vvalue textarea here
                }
            }
        };

        // for specifying the xml document
        saxParser.parse("/home/sanjaya/Desktop/a.xml", handler);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

主要问题是在
JTextArea
s上使用
setText
。这将覆盖字段中已有的任何文本,并将其替换为您传递的文本

由于大多数变量赋值都不涉及串联,因此只需设置变量的最后一个值

相反,您可以在方法中使用
append
直接更新字段

public void characters(char ch[], int start, int length)
        throws SAXException {

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

    if (nameAttribute != null && !nameAttribute.equals("")) {
        System.out.println("Name : " + nameAttribute);
        xm.textArea.append(nameAttribute + "\n");
    }

    if (blname) {

        vvalue = new String(ch, start, length);
        System.out.println("Value:" + Double.valueOf(vvalue));
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        xm.textArea_1.append(new Double(vvalue).toString() + "\n");
        blname = false;
    }
}

我可以猜到你的问题,但这只会浪费彼此的时间…我用完整的代码编辑了这个问题,下面是XML。问题是当所有的值都被解析时,显示的字符串只在vname中,而vtype和vvalue的值没有在GUI中设置。我保留了我的comp使用xml删除代码程序员感谢两次。我可以知道我的代码中有什么问题吗?当我根据您的要求进行更改时,它工作得非常好。主要问题是您对变量的赋值,您正在执行类似
vvalue=new String的操作