我正在实现一个 javax.swing.timer 来调用 GUI 上的同步操作。但是,该操作需要一些时间才能完成,我想为该计时器调用提供一个中止选项。那可能吗?
谢谢!
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import org.omg.CosNaming.IstringHelper;
public class vtm extends JPanel implements ActionListener{
private JFrame frame;
private String children[];
private ConsoleApp hawk[];
public static String data[][];
public Timer timer;
private int count1, count2;
private int i;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
vtm window = new vtm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public vtm() {
timer = new Timer(25000, this);
//timer.setInitialDelay(190);
timer.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("VTM");
frame.setSize(1024, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon("config.png"));
lblNewLabel_1.setBounds(970, 0, 51, 55);
frame.getContentPane().add(lblNewLabel_1);
lblNewLabel_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
config x = new config();
x.main(null);
timer.stop();
frame.dispose();
}
});
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("bg.png"));
lblNewLabel.setBounds(0, 0, 1024, 720);
frame.getContentPane().add(lblNewLabel);
String dir = System.getProperty("user.dir");
File df = new File(dir);
FilenameFilter fi = new FilenameFilter(){
public boolean accept(File df, String name){
return name.startsWith("D-");
}
};
children = df.list(fi);
hawk=new ConsoleApp[children.length];
EventQueue.invokeLater(new Runnable(){
public void run(){
int count1 = 0; count2 = 0;
for(i=0;i<children.length;i++)
{
hawk[i]=new ConsoleApp(children[i]);
Iterator<String> it = hawk[i].p.iterator();
System.out.println("Hi:"+hawk[i].p.size());
while(it.hasNext())
{
hawk[i].call(it.next());
if(count1 == 3)
{
count2++;
count1=0;
}
System.out.println("Look: " + hawk[i].l.getText().length());
hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
frame.getContentPane().add(hawk[i].c,0);
frame.getContentPane().add(hawk[i].l,0);
count1++;
}
}
}
});
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("lol");
EventQueue.invokeLater(new Runnable(){
public void run(){
int count1 = 0; count2 = 0;
for(i=0;i<children.length;i++)
{
hawk[i]=new ConsoleApp(children[i]);
Iterator<String> it = hawk[i].p.iterator();
System.out.println("Hi:"+hawk[i].p.size());
while(it.hasNext())
{
hawk[i].call(it.next());
if(count1 == 3)
{
count2++;
count1=0;
}
hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
frame.getContentPane().add(hawk[i].c,0);
frame.getContentPane().add(hawk[i].l,0);
count1++;
}
}
}
});
}
}