如何将XML对象转换为Swing以向其添加属性

时间:2018-04-03 10:05:38

标签: xml swing user-interface jbutton

我是摇摆和XML的新手。我之前也尝试过询问这个问题,但事实上没有人能够回答它。任何人都可以告诉我是否有办法将XML(主要是JButton)的对象转换为swing以赋予它们属性?

感谢。

Swing Code:

XMLDecoder xmlDecoder = new XMLDecoder(new FileInputStream ("MasterXML.xml"));
Object frame = xmlDecoder.readObject();
System.out.println("It reached here");
Object button = xmlDecoder.readObject();
System.out.println("It reached here 2");
((JButton) button).addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("Siri");
    }
    });
System.out.println("It reached here 3");
xmlDecoder.close();

1 个答案:

答案 0 :(得分:0)

我认为你看起来像这样

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");

NodeList username = doc.getElementsByTagName("username");           
System.out.println("username ="+username.item(0).getTextContent());
NodeList password = doc.getElementsByTagName("password");           
System.out.println("password ="+password.item(0).getTextContent());