java中JLabel添加监听事件_JAVA:JLabel没有改变事件

我尝试了一个使用GUI下载的HTTP链接,当CLicked开始下载按钮将开始下载时,下载正在工作,但在ActionListener中声明的下载开始时Label没有变化。请帮帮我。

这是代码

下载开始时和完成时标签应该改变。

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);

}

});

}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值