gpt4 book ai didi

Java 文件复制和打开新的 jFrame

转载 作者:行者123 更新时间:2023-11-29 05:00:28 27 4
gpt4 key购买 nike

我正在创建一个程序,其中一个目录的内容被复制到另一个目录。在复制过程开始之前,我试图打开第二个 Jframe 来告诉用户复制正在进行中。问题是在复制进度完成之前,第二个 JFrame 不会完全加载。有谁知道在开始复制之前完全加载第二帧的方法吗?

第一帧代码

public class First {

private JFrame frmFirstFrame;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
First window = new First();
window.frmFirstFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public First() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFirstFrame = new JFrame();
frmFirstFrame.setTitle("First Frame");
frmFirstFrame.setBounds(100, 100, 450, 300);
frmFirstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFirstFrame.getContentPane().setLayout(null);

JButton btnCopy = new JButton("Copy");
btnCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String source = "D:\\";
String destination = "D:\\test\\";

Second second = new Second();

second.setVisible(true);

File s = new File (source);
File d = new File (destination);

try {

FileUtils.copyDirectory(s, d);
//second.setVisible(false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
btnCopy.setBounds(184, 111, 89, 23);
frmFirstFrame.getContentPane().add(btnCopy);
}}

第二帧代码

public class Second extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Second frame = new Second();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Second() {
setTitle("Second Frame");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblCopyCompleted = new JLabel("Copy in Progress...");
lblCopyCompleted.setBounds(76, 70, 217, 37);
contentPane.add(lblCopyCompleted);
}
}

这是我在复制过程中得到的。

enter image description here

复制时应该是这样的

enter image description here

最佳答案

  1. 第二个 JFrame 应该是 JDialog,因为您的应用程序应该只有一个 JFrame(主窗口)。有关更多信息,请参阅:The Use of Multiple JFrames, Good/Bad Practice?
  2. 你的问题是你的代码不遵守 Swing 线程规则,通过在 Swing 事件线程上执行长时间运行的任务,你占用了线程,阻止它执行必要的工作,包括绘制到窗口和与用户。
  3. 一个好的解决方案是使用后台线程,特别是从 SwingWorker 获得的线程。教程:Lesson: Concurrency in Swing

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;

import javax.swing.*;

@SuppressWarnings("serial")
public class First2 extends JPanel {
private static final int COLS = 15;
private JTextField sourceField = new JTextField(COLS);
private JTextField destField = new JTextField(COLS);
private JDialog dialog;
private DialogPanel dialogPanel = new DialogPanel();

public First2() {
setLayout(new GridBagLayout());
add(new JLabel("Source:"), createGbc(0, 0));
add(sourceField, createGbc(1, 0));

add(new JLabel("Destination:"), createGbc(0, 1));
add(destField, createGbc(1, 1));

add(new JButton(new CopyAction("Copy")), createGbc(1, 2));
}

private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(4, 4, 4, 4);
return gbc;
}

private class CopyAction extends AbstractAction {
public CopyAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
String src = sourceField.getText();
String dest = destField.getText();

MyWorker myWorker = new MyWorker(src, dest);
myWorker.addPropertyChangeListener(new WorkerListener());
myWorker.execute();

Window window = SwingUtilities.getWindowAncestor(First2.this);
dialog = new JDialog(window, "File Copy", ModalityType.APPLICATION_MODAL);
dialog.add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}

private class MyWorker extends SwingWorker<Void, Void> {
private String src;
private String dest;

public MyWorker(String src, String dest) {
this.src = src;
this.dest = dest;
}

@Override
protected Void doInBackground() throws Exception {
FileUtils.copyDirectory(src, dest);
return null;
}
}

private class WorkerListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
if (dialog != null && dialog.isVisible()) {
dialog.dispose();
}
try {
((MyWorker) evt.getSource()).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}

private static void createAndShowGui() {
First2 mainPanel = new First2();

JFrame frame = new JFrame("First2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class DialogPanel extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = 250;

public DialogPanel() {
setLayout(new GridBagLayout());
add(new JLabel("Copy in Progress..."));
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}

关于Java 文件复制和打开新的 jFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com