当前位置: 代码迷 >> J2SE >> 新手,求ActionListener的一个小疑点
  详细解决方案

新手,求ActionListener的一个小疑点

热度:68   发布时间:2016-04-24 02:19:23.0
新手,求ActionListener的一个小问题
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class windows extends JWindow implements ActionListener{
JLabel back=new JLabel(new ImageIcon("C:/Users/Administrator/Desktop/s.jpg"));
JProgressBar progressBar=new JProgressBar(1,100);
Timer timer;
int n=100;
public windows(){
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
progressBar.setStringPainted(true);
progressBar.setString("正在加载程序");
getContentPane().add(back,"Center");
getContentPane().add(progressBar,"South");
setSize(950,350);
//将窗口调整到屏幕的中央
toFront();
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-950)/2,(screen.height-350)/2);
setVisible(true);
timer=new javax.swing.Timer(100,this);
timer.addActionListener(this);
timer.start();
}
public void actionPerformed(ActionEvent e){
if(--n>0){
progressBar.setValue(100-n);
timer.restart();
}else{
timer.stop();
dispose();
}
}

public static void main(String[] args) {
windows windo=new windows();
}
}
这个timer=new javax.swing.Timer(100,this);
timer.addActionListener(this);
timer.start();
我看不明,为什么要this,怎么才能去除this,改换其他一个,这个怎么改才能将事件响应弄成内部匿名类呢,求指点一下,谢谢!

------解决方案--------------------
你看这样可以吗?


import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.Timer;

public class windows extends JWindow {
JLabel back = new JLabel(new ImageIcon("C:/Users/Administrator/Desktop/s.jpg"));

JProgressBar progressBar = new JProgressBar(1, 100);

Timer timer;

int n = 100;

public windows() {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
progressBar.setStringPainted(true);
progressBar.setString("正在加载程序...");
getContentPane().add(back, "Center");
getContentPane().add(progressBar, "South");
setSize(950, 350);
// 将窗口调整到屏幕的中央
toFront();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - 950) / 2, (screen.height - 350) / 2);
setVisible(true);
timer = new javax.swing.Timer(100, new MyAction());
timer.addActionListener(new MyAction());
timer.start();
}

class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(--n > 0) {
progressBar.setValue(100 - n);
timer.restart();
}else {
timer.stop();
dispose();
}
}
}

public static void main(String[] args) {
new windows();
}