AppletViewer 被窃听并试图使用计时器 [英] AppletViewer bugged and trying to involve a timer

查看:23
本文介绍了AppletViewer 被窃听并试图使用计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一种自动改变灯光的交通灯.

我所拥有的:我目前有一个按按钮工作的红绿灯代码.

import java.awt.*;导入 java.awt.event.*;导入 javax.swing.*;导入 javax.swing.event.*;/**** Beschreibung** @version 1.0 vom 23.01.2017* @作者*/公共类ampel扩展JApplet {//暗房属性私有 JLabel jLabel1 = new JLabel();私有 JLabel jLabel2 = new JLabel();私有 JLabel jLabel3 = new JLabel();私有 JButton jButton1 = new JButton();私有 JButton jButton2 = new JButton();私有 JButton jButton3 = new JButton();私有 JButton jButton4 = new JButton();私有 JLabel jLabel4 = new JLabel();私有 JLabel jLabel5 = new JLabel();私有 JLabel jLabel6 = new JLabel();//Ende 属性公共无效初始化(){容器cp = getContentPane();cp.setLayout(null);cp.setBounds(0, 0, 314, 300);//安方组件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() {公共无效动作执行(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() {公共无效动作执行(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() {公共无效动作执行(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() {公共无效动作执行(ActionEvent evt){jButton4_ActionPerformed(evt);}});cp.add(jButton4);cp.setBackground(新颜色(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}//初始化结束//安方方法公共无效 jButton1_ActionPerformed(ActionEvent evt) {jButton2.setVisible(true);jButton1.setVisible(false);jLabel1.setBackground(新颜色(255,0,0));jLabel2.setBackground(新颜色(255,255,255));jLabel3.setBackground(新颜色(255,255,255));jLabel4.setBackground(新颜色(255,255,255));jLabel5.setBackground(新颜色(255,255,255));jLabel6.setBackground(新颜色(0,255,0));}//jButton1_ActionPerformed 结束公共无效 jButton2_ActionPerformed(ActionEvent evt) {jButton3.setVisible(true);jButton2.setVisible(false);jLabel1.setBackground(新颜色(255,0,0));jLabel2.setBackground(新颜色(255,255,0));jLabel3.setBackground(新颜色(255,255,255));jLabel4.setBackground(新颜色(255,255,255));jLabel5.setBackground(新颜色(255,255,0));jLabel6.setBackground(新颜色(255,255,255));}//jButton2_ActionPerformed 结束公共无效 jButton3_ActionPerformed(ActionEvent evt) {jButton4.setVisible(true);jButton3.setVisible(false);jLabel1.setBackground(新颜色(255,255,255));jLabel2.setBackground(新颜色(255,255,255));jLabel3.setBackground(新颜色(0,255,0));jLabel4.setBackground(新颜色(255,0,0));jLabel5.setBackground(新颜色(255,255,255));jLabel6.setBackground(新颜色(255,255,255));}//jButton3_ActionPerformed 结束公共无效 jButton4_ActionPerformed(ActionEvent evt) {jButton1.setVisible(true);jButton4.setVisible(false);jLabel1.setBackground(新颜色(255,255,255));jLabel2.setBackground(新颜色(255,255,0));jLabel3.setBackground(新颜色(255,255,255));jLabel4.setBackground(新颜色(255,0,0));jLabel5.setBackground(新颜色(255,255,0));jLabel6.setBackground(新颜色(255,255,255));}//jButton4_ActionPerformed 结束//结束方法}//类ampel结束

现在我的问题:

  1. 我的小程序查看器似乎被窃听了.(我的链接代码的截图,buttonpress Stoplight)

      • 这 2 个 Gif 应该会给你一个想法.

        1. 我已经阅读了几篇关于如何制作计时器和方法的文章,但现在我真的不知道如何将其实现到我的源代码中或对其进行更改以使其与计时器一起使用.

        我是这样理解的:

        创建一个方法,它说 do x,y,z...

        public void timer1_ActionPerformed(ActionEvent evt) {timer.setInitialDelay//告诉他做 x线程睡眠(5000);//或类似的东西,所以它会等待 5 秒//告诉他做 y//等待//告诉他做 z//...重复}

        解决方案

        您的代码存在一些问题:

        1. 您正在使用 null 布局,这是一个不好的做法,因为它会导致一些特定于布局的问题,包括重新绘制(可能您的有缺陷的"GUI 就是因为这个第一个问题),参见

          如您所见,(按钮和计时器)都侦听相同的 actionListener,因此无需使用很多(如前面第 2 点所述).

          我想我没有遗漏任何东西,希望对您有所帮助,但是您应该在问题中发布您的代码(就像我在回答中所做的那样).请阅读如何制作有效的最小、完整和可验证示例 (MCVE)简短的、自包含的、正确的示例 (SSCCE) 用于您的下一个问题,因此他们不会因此而被否决.

          I'm currently working on a Stoplight, which changes lights automatically.

          What I have: I currently have a code for a Stoplight that works with a button press.

          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
          

          Now to my Problems:

          1. Seemingly my appletviewer is bugged. (Screenshots of my linked Code, buttonpress Stoplight)

          These 2 Gif's should give you an idea.

          1. I've read through several articles in how to make a timer and methods, but i don't really know now How to implement it into my source code or change it so it works with a timer.

          I understood it like this:

          Make a Method which says do 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
          }
          

          解决方案

          There are some problems in your code:

          1. You're using a null layout, which is a bad practice, because it will lead to some layout specific problems, including repainting (probably your "buggy" GUI is because of this 1st problem), see Null layout is evil and Why is it frowned upon to use a null layout in Swing? to know why it's a bad practice to use them.

            While pixel perfect applications and the use of setBounds() might seem like the best way to build complex GUIs for Swing newbies, the more you do, the more problems you'll face due to this and you'll have a hard time trying to fix them, instead try using a layout manager or combinations of them along with empty borders for extra space between components.

          2. You're playing with the visibility of the JButtons, you could instead use a Card Layout or have a single JButton that manages all these events. This way you only need to handle a single action and not many (which in the end are the same).

          3. You're using a JApplet, which as said before in the comment of @MadProgrammer:

            Applets are deprecated (maybe not in the JDK, but in life), almost all browser either actively block them or have dropped support for them, better to just avoid them altogether

            You might want to use a JPanel instead and check this really interesting post about why CS teachers should stop teaching Applets.

          4. While you're changing your program from a JApplet to a JPanel based application, you should place your program on the Event Dispatch Thread (EDT) which could cause threading problems. An easy way to fix this is to make your main method as follows:

            public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        //Place your constructor here
                    }
                });
            }
            

          5. You're not following the Java naming conventions since your class is named ampel which should start with a CapitalLetter as Ampel.


          After those recommendations, you can get a really similar GUI with the following code:

          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);
              }
          }
          

          As you can see, both (the button and the Timer) listen to the same actionListener, so there's no need to use a lot of them (as said before on the point #2).

          I think I'm not missing anything and hope that helps you, however you should be posting your code in your question (as I did in my answer). Please read how to make a valid Minimal, Complete and Verifiable Example (MCVE) or a Short, Self Contained, Correct Example (SSCCE) for your next questions, so they don't get downvoted because of this.

          这篇关于AppletViewer 被窃听并试图使用计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆