swing.timer工作但有一些bug

时间:2015-02-05 11:59:03

标签: java swing timer

我有一个有效的应用程序,但非常快的计时器会让人感到困惑。

我们的想法是每隔100秒将数据从数据库加载到两个JTable,并且每隔10秒不间断地在它们之间切换。让一切正常,但经过几次。我发布了问题的简单示例,请帮我找到bug,这是我第一次使用swing.timers,我想这是微不足道的事情:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JScrollPane;

public class Test extends JFrame {

    private JPanel contentPane;
    private JTable pTable ;
    private JTable cTable;
    private Timer timer;
    private Timer worker;
    public int flag=0;
    public int flip=0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    initialize();
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        });
    }

    public static void initialize(){
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            Test frame = new Test();
            frame.setVisible(true);
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null, e1);
        }   
    }

    public Test() {
        try{

            setTitle("Warehouse stats");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new BorderLayout(0, 0));

            JScrollPane scrollPane = new JScrollPane();
            contentPane.add(scrollPane, BorderLayout.CENTER);

            ActionListener work = new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent event)
                {
                    flip=0;
                    // this is where i would load the tables from DB
                    pTable = new JTable(10,10);
                    cTable = new JTable(5,5);


              ActionListener action = new ActionListener()
                {   
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                       if(flip==0){
                           scrollPane.setViewportView(pTable);  
                           flip=1;
                       }else if (flip==1){
                           scrollPane.setViewportView(cTable);
                           flip=0;
                       }    

                    }
                };


                timer = new Timer(10*1000,action);
                timer.setInitialDelay(0);
                if(!timer.isRunning()){
                timer.start();
                }
                }
            };

            worker = new Timer(100*1000,work);
            worker.setInitialDelay(0);
            worker.start();

        }catch (Exception e2){
            JOptionPane.showMessageDialog(null, e2);
        }
    }
}

0 个答案:

没有答案