如何在网格java中实现计时器

时间:2016-01-07 22:24:56

标签: java swing grid

我正在为网格制作一个计时器,所以每5分钟网格将填充一个完整的列,但是当我宣布计时器时它对我不起作用。有没有一种方法可以显示带有计时器的网格,并且每次都看到网格列被填充

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class TestGrid02 {

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

    public TestGrid02() {
        EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    frame.revalidate();
                    frame.repaint();
                    //frame.remove(0);
                }
            });
    }

    public class TestPane extends JPanel {
        //         Timer timer = new Timer(500, new ActionListener() {
        //                     @Override
        //                     public void actionPerformed(ActionEvent e) {
        // 
        //                         setLayout(new GridBagLayout());
        //                         GridBagConstraints gbc = new GridBagConstraints();
        //                         for (int row = 0; row < 70; row++) {
        //                             for (int col = 0; col < 60; col++) {
        //                                 gbc.gridx = col;
        //                                 gbc.gridy = row;
        //                                 int i = 0;
        // 
        //                                 CellPane cellPane = new CellPane();
        //                                 Border border = null;
        //                                 if (row < 1) {
        //                                     if (col < 1) {
        //                                         border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
        //                                     } else {
        //                                         border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
        //                                     }
        //                                 } else {
        //                                     if (col < 1) {
        //                                         border = new MatteBorder(1, 1, 1, 0, Color.RED);
        //                                     } else {
        //                                         border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
        //                                     }
        //                                 }
        // 
        //                                 cellPane.setBorder(border);
        //                                 add(cellPane, gbc);
        // 
        //                             }
        //                         }
        //                         timer.start();
        //                     }
        // 
        //                 });

        public TestPane() {

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            for(int row = 0; row < 70; row++) {
                for (int col = 0; col < 60; col++) {
                    gbc.gridx = col;
                    gbc.gridy = row;
                    int i = 0;

                    CellPane cellPane = new CellPane();
                    Border border = null;

                    if (row < col) {
                        if (col < row) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.RED);
                        }
                    } else {
                        if (col < row) {
                            border = new MatteBorder(1, 1, 1, 0, Color.RED);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }

                    i++;
                    cellPane.setBorder(border);
                    add(cellPane, gbc);

                }
            }

        }
    };   

    // 
    //     public class TestPane extends JPanel {
    //         public TestPane() {
    //             setLayout(new GridBagLayout());
    // 
    //             GridBagConstraints gbc = new GridBagConstraints();
    //             for (int row = 0; row < 70; row++) {
    //                 for (int col = 0; col < 60; col++) {
    //                     gbc.gridx = col;
    //                     gbc.gridy = row;
    // 
    //                     int increment = 1;
    // 
    //                     CellPane cellPane = new CellPane();
    //                     Border border = null;
    //                     if (row < 1) {
    //                         if (col < 1) {
    //                            
    //                             border = new MatteBorder(1, 1, 0, 0, Color.RED);
    //                             //increment = increment + 1;
    //                         } else {
    //                             
    //                             border = new MatteBorder(1, 1, 0, 1, Color.RED);
    //                             //increment = increment + 1;
    //                         }
    //                     } else {
    //                         if (col < 1) {
    //                             
    //                             border = new MatteBorder(1, 1, 1, 0, Color.RED);
    //                             //increment = increment + 1;
    //                         } else {
    //                             
    //                             border = new MatteBorder(1, 1, 1, 1, Color.RED);
    //                             //increment = increment + 1;
    //                         }
    //                     }
    //                     cellPane.setBorder(border);
    //                     add(cellPane, gbc);
    //                 }
    //             }
    //         }
    // 
    //     }

    public class CellPane extends JPanel {
        private Color defaultBackground;
        public CellPane() {
            addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        defaultBackground = getBackground();
                        setBackground(Color.BLUE);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        setBackground(defaultBackground);
                    }
                });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(50, 50);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

简单的回答,不要尝试从头开始重建UI,只需更新它。

此示例simple以行/列顺序创建List CellPaneList<List<CellPane>>)。然后,Timer跟踪row的每个标记上的当前colTimer,更新下一个单元格。

现在,此示例将每5秒填充一列(用于显示目的),但它将一次完成一个单元格。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class TestGrid02 {

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

    public TestGrid02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                //frame.remove(0);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<List<CellPane>> grid = new ArrayList<>(70 * 60);

        public TestPane() {

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            grid = new ArrayList<>(70);

            for (int row = 0; row < 70; row++) {
                List<CellPane> cols = new ArrayList<>(60);
                for (int col = 0; col < 60; col++) {
                    gbc.gridx = col;
                    gbc.gridy = row;
                    int i = 0;

                    CellPane cellPane = new CellPane();
                    cols.add(cellPane);
                    Border border = null;

                    if (row < col) {
                        if (col < row) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.RED);
                        }
                    } else if (col < row) {
                        border = new MatteBorder(1, 1, 1, 0, Color.RED);
                    } else {
                        border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                    }

                    i++;
                    cellPane.setBorder(border);
                    add(cellPane, gbc);

                }
                grid.add(cols);
            }

            Timer timer = new Timer(5000 / 70, new ActionListener() {
                private int row = 0;
                private int col = 0;
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("...");
                    CellPane cl = grid.get(row).get(col);
                    cl.setBackground(Color.RED);

                    row++;
                    if (row >= 70) {
                        row = 0;
                        col++;
                        if (col >= 60) {
                            ((Timer)e.getSource()).stop();
                        }
                    }
                }
            });
            timer.start();
        }
    }

    public class CellPane extends JPanel {

        private Color defaultBackground;

        public CellPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    defaultBackground = getBackground();
                    setBackground(Color.BLUE);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    setBackground(defaultBackground);
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(50, 50);
        }
    }

}

如果你想在Timer的每个刻度上填满整个列,你只需要跟踪当前列(在每个刻度上递增它)并循环遍历{{1}的每一行并更新相应列中的单元格

像...一样的东西。

List

例如......