我有一个将xml转换为csv文件的过程。我的转换器代码运行良好。但是,我已经创建了一个ui窗口来获取输入文件和输出文件路径,并使用convert按钮来触发转换。现在我想在转换进行时添加一个进度指示器。我提出了一个简单的解决方案,可以在转换任务进行时在窗格上显示标签并更改其文本。但是标签文本没有改变。这是密码。谢谢你的帮助。
`导入java.awt.event.actionevent;导入java.awt.event.actionlistener;导入javax.swing.*;
公共类inputgui扩展jframe实现actionlistener{
JTextField xmlInputPath = new JTextField("C:\\InputFile.xml", 30);
JTextField csvOutputPath = new JTextField("C:\\OutputFile.csv", 30);
JTextField parentNode = new JTextField(20);
JButton convertButton = new JButton("Convert");
JLabel progressLabel = new JLabel("");
public InputGUI() {
super("XML to CSV Converter");
setBounds(400, 300, 500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JLabel xmlInputLabel = new JLabel("XML Input File Path: ", SwingConstants.LEFT);
JLabel csvInputLabel = new JLabel("CSV Output File Path: ", SwingConstants.LEFT);
JLabel parentNodeName = new JLabel("Parent Node Tag: ", SwingConstants.LEFT);
parentNodeName.setHorizontalTextPosition(SwingConstants.LEFT);
pane.add(xmlInputLabel);
pane.add(xmlInputPath);
pane.add(csvInputLabel);
pane.add(csvOutputPath);
pane.add(parentNodeName);
pane.add(parentNode);
pane.add(convertButton);
pane.add(progressLabel);
convertButton.addActionListener(this);
add(pane);
setVisible(true);
}
private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
setLookAndFeel();
new InputGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
try {
progressLabel.setText("Converting...");
String inputPath = xmlInputPath.getText();
String outputPath = csvOutputPath.getText();
String elementTagName = parentNode.getText();
inputPath = inputPath.replace("\\", "\\\\");
outputPath = outputPath.replace("\\", "\\\\");
XmlToCsvFileUsingSTAX convert = new XmlToCsvFileUsingSTAX();
convert.xmlToCsvFileConverter(inputPath, outputPath, elementTagName);
Message msg = new Message();
progressLabel.setText("");
msg.successMessage();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}`
暂无答案!
目前还没有任何答案,快来回答吧!