button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { if (ComboBoxname=="整个文档") { System.out.println(textField1.getText()); XmlHanding.searchFile(textField1.getText()); }else { XmlHanding.SearchXml(ComboBoxname, textField1.getText(),checkBox.isSelected()); } if (!textField1.getText().trim().equals("")) { text1.setText("以下"+XmlHanding.existStrings.size()+"个文件存在节点"+ComboBoxname+":"+textField1.getText()+"\n"); }else { text1.setText("以下"+XmlHanding.existStrings.size()+"个文件存在节点"+ComboBoxname+"\n"); } for(String str:XmlHanding.existStrings) { text1.append("\n"+str); } text1.setCaretPosition(0); if(XmlHanding.noFileStrings.size()!=0){ if(!textField1.getText().trim().equals("")) { text2.setText("以下"+XmlHanding.noFileStrings.size()+"个文件不存在:"+ComboBoxname+":"+textField1.getText()+"\n"); }else { text2.setText("以下"+XmlHanding.noFileStrings.size()+"个文件不存在:"+ComboBoxname+"\n"); } for(String str:XmlHanding.noFileStrings) { text2.append("\n"+str); } } text2.setCaretPosition(0); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); 这段怎么加实时显示的进度条
时间: 2024-04-08 16:34:48 浏览: 29
您可以通过在 `actionPerformed` 方法中添加一个进度条来实现实时显示进度条的效果。以下是一个示例代码:
```java
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 创建进度条
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
// 创建进度对话框
JDialog progressDialog = new JDialog();
progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
progressDialog.setTitle("进度");
progressDialog.setLayout(new BorderLayout());
progressDialog.add(progressBar, BorderLayout.CENTER);
progressDialog.setSize(300, 50);
progressDialog.setLocationRelativeTo(null);
progressDialog.setVisible(true);
// 创建线程执行耗时操作
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
if (ComboBoxname=="整个文档") {
System.out.println(textField1.getText());
XmlHanding.searchFile(textField1.getText());
} else {
XmlHanding.SearchXml(ComboBoxname, textField1.getText(),checkBox.isSelected());
}
if (!textField1.getText().trim().equals("")) {
text1.setText("以下" + XmlHanding.existStrings.size() + "个文件存在节点" + ComboBoxname + ":" + textField1.getText() + "\n");
} else {
text1.setText("以下" + XmlHanding.existStrings.size() + "个文件存在节点" + ComboBoxname + "\n");
}
for(String str : XmlHanding.existStrings) {
text1.append("\n" + str);
}
text1.setCaretPosition(0);
if (XmlHanding.noFileStrings.size() != 0) {
if (!textField1.getText().trim().equals("")) {
text2.setText("以下" + XmlHanding.noFileStrings.size() + "个文件不存在:" + ComboBoxname + ":" + textField1.getText() + "\n");
} else {
text2.setText("以下" + XmlHanding.noFileStrings.size() + "个文件不存在:" + ComboBoxname + "\n");
}
for(String str : XmlHanding.noFileStrings) {
text2.append("\n" + str);
}
}
text2.setCaretPosition(0);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
// 执行完毕后关闭进度对话框
progressDialog.dispose();
}
}
});
// 启动线程
thread.start();
// 更新进度条的值
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (thread.isAlive()) {
// 设置进度条的值,可以根据实际情况修改
progressBar.setValue((int) (progressBar.getMaximum() * progress)); // progress 为进度值,范围为 0~1
} else {
((Timer) e.getSource()).stop();
}
}
});
timer.start();
}
});
```
请注意,上述代码中的 `progress` 是一个进度值,可以根据实际情况进行修改。在耗时操作的代码中,您可以通过更新 `progress` 的值来更新进度条的显示。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)