AppletViewer窃听并尝试涉及计时器

时间:2017-01-24 21:07:12

标签: java swing timer

我正在制作一个自动更换灯光的Stoplight。

我拥有的内容:我目前有一个可以按下按钮的Stoplight代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 23.01.2017
  * @author 
  */

public class ampel extends JApplet {
  // Anfang Attribute
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JLabel jLabel4 = new JLabel();
  private JLabel jLabel5 = new JLabel();
  private JLabel jLabel6 = new JLabel();
  // Ende Attribute

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(null);
    cp.setBounds(0, 0, 314, 300);
    // Anfang Komponenten
    jButton1.setVisible(true);
    jButton2.setVisible(false);
    jButton3.setVisible(false);
    jButton4.setVisible(false);
    jLabel1.setBounds(16, 24, 75, 41);
    jLabel1.setText("");
    jLabel1.setOpaque(true);
    cp.add(jLabel1);
    jLabel2.setBounds(16, 88, 75, 33);
    jLabel2.setText("");
    jLabel2.setOpaque(true);
    cp.add(jLabel2);
    jLabel3.setBounds(16, 144, 75, 33);
    jLabel3.setText("");
    jLabel3.setOpaque(true);
    cp.add(jLabel3);
    jButton1.setBounds(112, 96, 73, 25);
    jButton1.setText("jButton1");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton1_ActionPerformed(evt);
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(112, 96, 73, 25);
    jButton2.setText("jButton2");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton2_ActionPerformed(evt);
      }
    });
    cp.add(jButton2);
    jButton3.setBounds(112, 96, 73, 25);
    jButton3.setText("jButton3");
    jButton3.setMargin(new Insets(2, 2, 2, 2));
    jButton3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton3_ActionPerformed(evt);
      }
    });
    cp.add(jButton3);
    jButton4.setBounds(112, 96, 73, 25);
    jButton4.setText("jButton4");
    jButton4.setMargin(new Insets(2, 2, 2, 2));
    jButton4.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton4_ActionPerformed(evt);
      }
    });
    cp.add(jButton4);
    cp.setBackground(new Color(0xC0C0C0));
    jLabel4.setBounds(224, 16, 75, 49);
    jLabel4.setText("");
    jLabel4.setBackground(Color.WHITE);
    jLabel4.setOpaque(true);
    cp.add(jLabel4);
    jLabel5.setBounds(224, 80, 75, 49);
    jLabel5.setText("");
    jLabel5.setBackground(Color.WHITE);
    jLabel5.setOpaque(true);
    cp.add(jLabel5);
    jLabel6.setBounds(224, 144, 75, 33);
    jLabel6.setText("");
    jLabel6.setBackground(Color.WHITE);
    jLabel6.setOpaque(true);
    cp.add(jLabel6);
    // Ende Komponenten

  } // end of init

  // Anfang Methoden
  public void jButton1_ActionPerformed(ActionEvent evt) {
    jButton2.setVisible(true);
    jButton1.setVisible(false);

    jLabel1.setBackground(new Color(255,0,0));
    jLabel2.setBackground(new Color(255,255,255));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,255,255));
    jLabel5.setBackground(new Color(255,255,255));
    jLabel6.setBackground(new Color(0,255,0));
  } // end of jButton1_ActionPerformed

  public void jButton2_ActionPerformed(ActionEvent evt) {
    jButton3.setVisible(true);
    jButton2.setVisible(false);
    jLabel1.setBackground(new Color(255,0,0));
    jLabel2.setBackground(new Color(255,255,0));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,255,255));
    jLabel5.setBackground(new Color(255,255,0));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton2_ActionPerformed

  public void jButton3_ActionPerformed(ActionEvent evt) {
    jButton4.setVisible(true);
    jButton3.setVisible(false);
    jLabel1.setBackground(new Color(255,255,255));
    jLabel2.setBackground(new Color(255,255,255));
    jLabel3.setBackground(new Color(0,255,0));

    jLabel4.setBackground(new Color(255,0,0));
    jLabel5.setBackground(new Color(255,255,255));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton3_ActionPerformed

  public void jButton4_ActionPerformed(ActionEvent evt) {
    jButton1.setVisible(true);
    jButton4.setVisible(false);
    jLabel1.setBackground(new Color(255,255,255));
    jLabel2.setBackground(new Color(255,255,0));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,0,0));
    jLabel5.setBackground(new Color(255,255,0));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton4_ActionPerformed

  // Ende Methoden

} // end of class ampel

现在我的问题:

  1. 看来我的appletviewer被窃听了。 (我链接的代码截图,按钮Stoplight)

  2. enter image description here

    enter image description here

    这两个Gif应该给你一个想法。

    1. 我已经阅读了几篇关于如何制作计时器和方法的文章,但我现在还不知道如何将它实现到我的源代码中或更改它以便它与计时器一起使用。
    2. 我理解为这样:

      制作一个说x,y,z ......

      的方法
      public void timer1_ActionPerformed(ActionEvent evt) {
      
          timer.setInitialDelay
      
          // tell him to do x
      
          Thread.sleep(5000); // or something similare so it will wait 5 seconds
      
          // Tell him to do y
          // wait
      
          // Tell him to do z
          // ...repeat
      }
      

1 个答案:

答案 0 :(得分:3)

您的代码中存在一些问题:

  1. 你正在使用null布局,这是一种不好的做法,因为它会导致一些特定于布局的问题,包括重新绘制(可能是因为第一个问题,你的“错误”GUI) ,请参阅Null layout is evilWhy is it frowned upon to use a null layout in Swing?,了解使用它们的原因。

    虽然像素完美的应用程序和setBounds()的使用似乎是为Swing新手构建复杂GUI的最佳方式,但是你做的越多,你将面临的问题就越多,你就会有很难尝试修复它们,而是尝试在组件之间使用布局管理器或combinations of them以及extra space的空边框。

  2. 您正在使用JButton的可见性,您可以使用Card Layout或使用一个JButton来管理所有这些事件。这样你只需要处理一个动作而不是很多(最终都是相同的)。

  3. 您正在使用JApplet,如前所述@MadProgrammer的comment

      

    Applet已被弃用(可能不在JDK中,但在生活中),几乎所有浏览器都会主动阻止它们或者已经放弃了对它们的支持,最好完全避免它们

    你可能想要使用JPanel而是真正检查interesting post,了解为什么CS教师应该停止教授Applet。

  4. 当您将程序从JApplet更改为基于JPanel的应用程序时,您应该将程序放在Event Dispatch Thread (EDT)上,这可能会导致线程问题。解决此问题的一种简单方法是使您的main方法如下:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Place your constructor here
            }
        });
    }
    
  5. 您没有关注Java naming conventions,因为您的课程名称为ampel,应以CapitalLetter Ampel开头。

  6. 在这些建议之后,您可以使用以下代码获得一个非常相似的GUI:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class StopLightSample {
    
        private JFrame frame;
        private JPanel pane;
        private JPanel leftPane;
        private JPanel centerPane;
        private JPanel rightPane;
        private JPanel[] leftLights;
        private JPanel[] rightLights;
        private JButton button;
        private int leftLightFocus = 0;
        private int rightLightFocus = 2;
        private Color[] leftColors = {Color.GREEN, Color.YELLOW, Color.RED};
        private Color[] rightColors = {Color.RED, Color.YELLOW, Color.GREEN};
        private Timer timer;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new StopLightSample().createAndShowGui();
                }
            });
        }
    
        private ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 3; i++) {
                    leftLights[i].setBackground(Color.WHITE);
                    rightLights[i].setBackground(Color.WHITE);
                }
    
                leftLights[leftLightFocus].setBackground(leftColors[leftLightFocus]);
                rightLights[rightLightFocus].setBackground(rightColors[rightLightFocus]);
    
                leftLightFocus++;
                rightLightFocus--;
    
                if (leftLightFocus > 2) {
                    leftLightFocus = 0;
                }
    
                if (rightLightFocus < 0) {
                    rightLightFocus = 2;
                }
            }
        };
    
        public void createAndShowGui() {
            frame = new JFrame("Stoplight sample");
            pane = new JPanel();
            pane.setLayout(new GridLayout(1, 3, 20, 5));
            leftLights = new JPanel[3];
            rightLights = new JPanel[3];
            button = new JButton("Button");
    
            leftPane = new JPanel();
            rightPane = new JPanel();
    
            centerPane = new JPanel();
            centerPane.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 100));
    
            button.addActionListener(actionListener);
    
            centerPane.add(button);
    
            leftPane.setLayout(new GridLayout(3, 1, 0, 25));
            rightPane.setLayout(new GridLayout(3, 1, 0, 25));
    
            for (int i = 0; i < 3; i++) {
                leftLights[i] = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 50);
                    }
                };
    
                rightLights[i] = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 50);
                    }
                };
    
                leftLights[i].setBackground(Color.WHITE);
                rightLights[i].setBackground(Color.WHITE);
    
                leftLights[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                rightLights[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
    
                leftPane.add(leftLights[i]);
                rightPane.add(rightLights[i]);
            }
    
            pane.add(leftPane);
            pane.add(centerPane);
            pane.add(rightPane);
    
            frame.add(pane);
    
            timer = new Timer(1000, actionListener);
            timer.setInitialDelay(0);
            timer.start();
    
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    

    enter image description here enter image description here enter image description here

    正如你所看到的,两个(按钮和Timer)都听同一个actionListener,因此不需要使用它们(如前面第2点所述)。

    我想我没有遗漏任何东西,并希望能帮助你,但是你应该在你的问题中发布你的代码(正如我在答案中所做的那样)。请阅读如何为您的下一个问题制作有效的Minimal, Complete and Verifiable Example (MCVE)Short, Self Contained, Correct Example (SSCCE),以便他们不会因此而被投票。