繁体   English   中英

Java GUI建议

[英]Java GUI Advice

我有一个Java文件,该文件使用SAX解析XML文件,似乎工作正常。

以下是我的SAX.java文件:

 public class SAX extends DefaultHandler{
     private final List<Student> studentList = new ArrayList<Student>();
     private String tempVal;
     private Student tempStudent;

public void runExample(){
    parseDocument();
    outputList();
}

private void parseDocument(){
    try {
        // get a factory object
        SAXParserFactory spf = SAXParserFactory.newInstance();

        // get an instance of the parser
        SAXParser sp = spf.newSAXParser();

        // parse the XML file and register this
        // class for callbacks
        sp.parse("Students.xml", this);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
private void outputList(){
    for(Student student : studentList){
        System.out.println(student);
    }
}

// the event handlers....
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    // reset
    tempVal = "";
    if(qName.equalsIgnoreCase("Student")){
        // create a new Employee object
        tempStudent = new Student();
        tempStudent.setTitle(attributes.getValue("Title"));
    }
 //        System.out.println(
 //                "startElement::qName is "+qName);
}

@Override
public void characters(char []ch, int start, int length) throws SAXException{       
    tempVal = new String(ch, start, length);
 //        System.out.println("tempVal is "+tempVal);
}

@Override
public void endElement(String uri, String localname, String qName) throws SAXException {
    if(qName.equalsIgnoreCase("Student")){
        studentList.add(tempStudent);
    } else if(qName.equalsIgnoreCase("Name")){
        tempStudent.setName(tempVal);
    } else if(qName.equalsIgnoreCase("Age")){
        tempStudent.setAge(Integer.parseInt(tempVal));
    } else if(qName.equalsIgnoreCase("College")){
        tempStudent.setCollege(tempVal);
    } else if(qName.equalsIgnoreCase("School")) {
        tempStudent.setSchool(tempVal);
    }
}

public static void main(String []args){
    SAX spe = new SAX();
    spe.runExample();
}
}

但是,有人要求我在GUI中显示此内容。 单击特定的单选按钮,并且用户单击分析后,将使用SAX解析XML文件,结果将显示在文本框中。 已经给了我GUI了,它已经编码了,我的问题是我对GUI的了解有限,而且我不知道如何集成它们两者。

public void actionPerformed(ActionEvent e) {
else if (e.getSource() == parseButton){
        if(saxRadioButton.isSelected()){
            // do SAX stuff
        }

我只是在寻找有人在这里向我指出正确的方向。 我应该单独制作SAX文件还是直接放入If语句中。 我完全迷路了。

从您的SAX类获取解析的XML,然后将其设置为所需的组件。 将gui与您的进程分开是有意义的,并且将更易于调试。 例如:

public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(parseButton)){
        if(saxRadioButton.isSelected()) {
            String result = SAX.yourParsingHere();
            yourTextArea.setText(result);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM