试图延迟JDialog中JLabel的外观

时间:2014-07-29 21:58:36

标签: java swing timer delay jlabel

嗨我有一点问题:我想要的是JLabels JDialog-Window的出现,JLabels的第一行出来了Jlabels然后两秒钟后Windowlistener等的第二行 我已尝试使用doClick()Jdialog - 方法等,每当package footballQuestioner; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; public class attempter { public static void main(String[] args) throws InterruptedException { JDialog dialog = new Punkte(); } } class Punkte extends JDialog { private JPanel screenPanel = new JPanel(new GridLayout(4, 1)); private JButton button = new JButton(); private int i = 1; private class WindowHandler implements WindowListener { @Override public void windowActivated(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowIconified(WindowEvent e) { } @Override public void windowOpened(WindowEvent e) { button.doClick(1000); button.doClick(1000); button.doClick(1000); button.doClick(); // here im trying to delay the appearance of the // JLabels.... } } private class ButtonHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (i) { case 1: settingUpPanel(getPanelFromScreenPanel(i), "Right", new Color( 102, 205, 0)); settingUpPanel(getPanelFromScreenPanel(i), "Wrong", Color.RED); break; case 2: settingUpPanel(getPanelFromScreenPanel(i), "Trefferquote", Color.YELLOW); break; case 3: settingUpPanel(getPanelFromScreenPanel(i), "Ausgezeichnet", Color.BLUE); break; } System.out.println(i); i++; } } public Punkte() { button.addActionListener(new ButtonHandler()); addWindowListener(new WindowHandler()); setModal(true); setResizable(true); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); settingUpScreenPanel(); add(screenPanel); setSize(1200, 1000); centeringWindow(); setVisible(true); } private void settingUpScreenPanel() { JPanel titlePanel = new JPanel(new GridBagLayout()); JPanel rightWrongCountPanel = new JPanel(new GridLayout(1, 2)); JPanel shareOfRightQuestions = new JPanel(new GridBagLayout()); JPanel grade = new JPanel(new GridBagLayout()); settingUpPanel(titlePanel, "Result", Color.BLACK); // settingUpPanel(rightWrongCountPanel, // "Right: "+numberOfRightAnsers+"/6",new Color(102,205,0)); // settingUpPanel(rightWrongCountPanel, // "Wrong: "+(6-numberOfRightAnsers)+"/6", Color.RED); // settingUpPanel(shareOfRightQuestions, // "Trefferquote: "+(numberOfRightAnsers*100/6)+"%",Color.YELLOW); // settingUpPanel(summaSummarum, // getBufferedImage("footballQuestioner/Strich.png")); // settingUpPanel(grade,"Aushezeichnet", Color.BLUE); borderingJPanel(screenPanel, null, null); titlePanel.setOpaque(false); rightWrongCountPanel.setOpaque(false); shareOfRightQuestions.setOpaque(false); grade.setOpaque(false); screenPanel.add(titlePanel); screenPanel.add(rightWrongCountPanel); screenPanel.add(shareOfRightQuestions); screenPanel.add(grade); } private void settingUpPanel(JComponent panel, String string, Color color) { Font font = new Font("Rockwell Extra Bold", Font.PLAIN, 65); JPanel innerPanel = new JPanel(new GridBagLayout()); JLabel label = new JLabel(string); label.setForeground(color); label.setFont(font); innerPanel.add(label); innerPanel.setOpaque(false); panel.add(innerPanel); panel.validate(); panel.repaint(); } public JPanel getPanelFromScreenPanel(int numberOfPanel) { JPanel screenPanel = (JPanel) getContentPane().getComponent(0); JPanel labelPanel = (JPanel) screenPanel.getComponent(numberOfPanel); return labelPanel; } public void centeringWindow() { Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x; int y; x = (int) (dimension.getWidth() - getWidth()) / 2; y = (int) (dimension.getHeight() - getHeight()) / 2; setLocation(x, y); } public void borderingJPanel(JComponent panel, String jPanelname, String fontStyle) { Font font = new Font(fontStyle, Font.BOLD + Font.ITALIC, 12); if (jPanelname != null) { panel.setBorder(BorderFactory.createTitledBorder(BorderFactory .createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE), jPanelname, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, font)); } else if (jPanelname == null || fontStyle == null) { panel.setBorder(BorderFactory.createTitledBorder(BorderFactory .createEtchedBorder(EtchedBorder.LOWERED, Color.BLACK, Color.WHITE))); } panel.setOpaque(false); } } 重新验证其所有面板时都会尝试,并且不会延迟显示它们!

请帮助我(只需复制下面的代码并试用)!

{{1}}

1 个答案:

答案 0 :(得分:3)

这是javax.swing.Timer ...

的一个非常好的用例

这将允许您安全地在UI上安排定期回调,以便您可以安全地执行操作。

private class WindowHandler extends WindowAdapter {

    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println("...");
        Timer timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel panel = getPanelFromScreenPanel(1);
                panel.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                for (int index = 0; index < 100; index++) {
                    panel.add(new JLabel(Integer.toString(index)), gbc);
                }
                panel.revalidate();
            }
        });
        timer.start();
        timer.setRepeats(false);
    }

}

现在,如果您想要执行一系列操作(以间隔分隔),您可以使用counter来确定&#34; ticks&#34;已经发生并采取适当的行动......

private class WindowHandler extends WindowAdapter {

    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println("...");
        Timer timer = new Timer(2000, new ActionListener() {
            private int counter = 0;
            private int maxActions = 10;
            @Override
            public void actionPerformed(ActionEvent e) {
                switch (counter) {
                    case 0:
                        // Action for case 0...
                        break;
                    case 1:
                        // Action for case 1...
                        break;
                    .
                    .
                    .
                }
                counter++;
                if (counter >= maxActions) {
                    ((Timer)e.getSource()).stop();
                }
            }
        });
        timer.start();
    }

}

请查看How to use Swing Timers了解详情