有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA用户界面:JLabel在事件发生时未更改

我正在尝试使用GUI下载HTTP链接,当单击“开始下载”按钮时,该按钮将开始下载,下载正在工作,但在ActionListener中声明的下载启动时,标签不会更改。请帮帮我

这是密码

标签应在下载开始和完成时更改

public class SwingGUI2 extends javax.swing.JFrame {    
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jButton1;
private URLConnection connect;

private SwingGUI2() {
    setLayout(new FlowLayout());

    jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("");
    add(jLabel1);

    jButton1 = new javax.swing.JButton();
    jButton1.setText("Start Download");
    add(jButton1);

    jButton1.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            jButton1ActionPerformed(e);
        }
    });


    pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jLabel1.setText("Download Started");  //NOT WORKING
    try {
        String DownURL = "http://www.number1seed.com/music/flymetothemoon.mp3";
        URL url = new URL(DownURL);
        connect = url.openConnection();
        File file = new File("download");

        BufferedInputStream BIS = new BufferedInputStream(connect.getInputStream());
        BufferedOutputStream BOS = new BufferedOutputStream(new FileOutputStream(file.getName()));
        int current;
        while((current = BIS.read())>=0) BOS.write(current);
        BOS.close();
        BIS.close();

        jLabel1.setText("Completed");   //NOT WORKING

    } catch (Exception e) {}
}


public static void main(String[] args) throws ClassNotFoundException, InstantiationException {
    try {
        javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (IllegalAccessException ex) {
        Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedLookAndFeelException ex) {
        Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            SwingGUI2 gui = new SwingGUI2();
            gui.setVisible(true);
            gui.setSize(300,200);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    });
}

}


共 (2) 个答案

  1. # 1 楼答案

    您正在错误的事件分派线程下运行下载。这会阻止EDT线程执行下载以外的任何操作

    您应该在单独的线程中进行下载,标签将开始更新

  2. # 2 楼答案

    您正试图从事件调度器线程运行下载,导致GUI在执行任务时冻结

    请看一下SwingWorker以作为后台线程执行下载任务。你的actionPerformed方法应该是这样的:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jLabel1.setText("Download Started"); 
        SwingWorker sw = new SwingWorker() {
                public Object doInBackground(){
                    try {
                        // ...
                        // Do the whole download thing
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    jLabel1.setText("Completed");
                    return null;
                }
            };
        sw.execute();
    }
    

    注意:请确保您没有接受此捕获中的异常,否则您将无法查看是否发生了任何错误