Timer 如何让他延迟3秒呢?迷茫.差一句代码.添上去有错
==================================
下面代码可以不看,只是为了保证代码的完整
==================================
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2006 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Auto {
boolean packFrame = false;
/**
* Construct and show the application.
*/
public Auto() {
Auto_Frame frame = new Auto_Frame();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Auto();
}
});
}
}
=============================
上面的代码不是重点.可以不看....
=============================
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTree;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.border.TitledBorder;
import java.io.*;
import javax.swing.Timer;
public class Auto_Frame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jBuAuto = new JButton();
JSplitPane jSplitPane1 = new JSplitPane();
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JTree jTree1 = new JTree();
ImageIcon icon=new ImageIcon( "F:/代码区/image/01.jpg ");
JLabel jLabel1 = new JLabel(icon);
static Timer AutoTimer;
public Auto_Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle( "Auto ");
jBuAuto.setIcon(icon);
jBuAuto.setText( "Auto ");
jBuAuto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jBuAuto_actionPerformed(e);
}
});
jLabel1.setText( "jLabel1 ");
contentPane.add(jPanel1, java.awt.BorderLayout.SOUTH);
jPanel1.add(jBuAuto);
contentPane.add(jSplitPane1, java.awt.BorderLayout.CENTER);
jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
jScrollPane1.getViewport().add(jTree1);
jSplitPane1.add(jScrollPane2, JSplitPane.RIGHT);
jScrollPane2.getViewport().add(jLabel1);
AutoTimer.addActionListener(new ActionListener(){
int i=0;
File f=new File( "F:/代码区/image/ ");
String[] files=f.list();
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==AutoTimer)
{
if(i==files.length)
i=0;
icon=new ImageIcon( "F:/代码区/image/ "+files[i]);
jLabel1.setIcon(icon);
jLabel1.repaint();
i++;
while(i> =0&&i <files.length){
}
}
}
});
}
public void jBuAuto_actionPerformed(ActionEvent e) {
AutoTimer.start();
}
}
[解决办法]
Thread.sleep(3000);
[解决办法]
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run()
{
MainProcess();
}
},0,1*3000);
這個可以!
[解决办法]
Timer timer = new Timer();
timer.scheduleAtFixedRate(notetimer, 1000, 1000 * 3);
[解决办法]
一楼
Thread.sleep(3000);
[解决办法]
java.util.Timer tmr = new java.util.Timer();
java.util.GregorianCalendar now = null;
tmr.schedule(new java.util.TimerTask(){
public void run(){
now = new GregorianCalendar();
//do something
}
},now.getTime(),3000);
[解决办法]
up
[解决办法]
怎么来求人帮忙还这么多牢骚 现在的人都怎么了
[解决办法]
这么长的代码谁看呀
[解决办法]
写的乱点,上面好多正解,楼主不采用,那也没法啊!!!
[解决办法]
就是,代码又长,又乱,你叫你怎么帮你看啊,写代码,一定要规范.要不我找几千行代码,叫你先帮我看看,看你会有什么感受,整个上下,注释都没有.一楼的叫你用线程,那是一种不错的解决方法.
还在那说没人看你代码.郁闷......
[解决办法]
没看代码,你的那个this是什么?如果不四、是TimerTask就是不行的,你查查Timer 的api 构造方法
