0

我试图“点亮”一个 JLabels 列表,就好像它是一串灯一样。无论如何,我有一个简单的 gui,它有一个 JPanel,它是 JLabel 的数组。我一直在使用计时器并将背景颜色从黑色更改为绿色,以使其看起来像“亮起”。我已经找到了点亮它们的代码,但是返回下来似乎是一个问题。

到目前为止的工作代码:

    this.timer = new Timer(100, new ActionListener() {
        private int sequence = 0;

        public void actionPerformed(ActionEvent evt) {

            if (sequence % 2 == 0) {
                labels[1].setBackground(Color.black);
                labels[3].setBackground(Color.black);
                labels[5].setBackground(Color.black);
                labels[7].setBackground(Color.black);
                labels[sequence].setBackground(Color.green);
                sequence++;
            } else if (sequence % 2 != 0) {
                labels[0].setBackground(Color.black);
                labels[2].setBackground(Color.black);
                labels[4].setBackground(Color.black);
                labels[6].setBackground(Color.black);
                labels[sequence].setBackground(Color.green);
                sequence++;
            }

            if (sequence > 7) {
                sequence = 0;
            }


        }

    });
    this.timer.start();

}
4

2 回答 2

2

我将使用一个计数变量来遍历我的数组或集合,并将索引标签右侧的标签设置为背景色,将索引标签设置为活动颜色,并推进索引。例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import javax.swing.*;

@SuppressWarnings("serial")
public class LarsonScanner extends JPanel {
   private static final int PANEL_COUNT = 50;
   private static final int TIMER_DELAY = 30;
   private static final Color BACKGROUND = Color.BLACK;
   private static final Color ACTIVE_COLOR = Color.GREEN;
   private static final Color PARTIAL_COLOR = new Color(0, 90, 0);
   private static final Dimension RIGID_AREA = new Dimension(14, 1);

   private JPanel[] panels = new JPanel[PANEL_COUNT];

   public LarsonScanner() {
      setLayout(new GridLayout(1, 0));
      for (int i = 0; i < panels.length; i++) {
         panels[i] = new JPanel();
         panels[i].add(Box.createRigidArea(RIGID_AREA));
         panels[i].setBackground(BACKGROUND);
         add(panels[i]);
      }
      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   private class TimerListener implements ActionListener {
      private int count = 0;

      @Override
      public void actionPerformed(ActionEvent e) {
         int index = (count - 2 + panels.length) % panels.length;
         panels[index].setBackground(BACKGROUND);
         int prior = (count - 1 + panels.length) % panels.length;
         int next = (count + 1) % panels.length;
         panels[count].setBackground(ACTIVE_COLOR);
         panels[prior].setBackground(PARTIAL_COLOR);
         panels[next].setBackground(PARTIAL_COLOR);
         count++;
         count %= panels.length;
      }
   }

   private static void createAndShowGui() {
      LarsonScanner mainPanel = new LarsonScanner();

      JFrame frame = new JFrame("LarsonScanner");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

编辑
程序的 2 迭代 2:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class LarsonScanner extends JPanel {
   private static final int PANEL_COUNT = 40;
   private static final int TIMER_DELAY = 20;
   private static final Color BACKGROUND = Color.BLACK;
   private static final Color ACTIVE_COLOR = Color.GREEN;
   private static final Color PARTIAL_COLOR = new Color(0, 90, 0);
   private static final Dimension RIGID_AREA = new Dimension(14, 1);

   private JPanel[] panels = new JPanel[PANEL_COUNT];

   public LarsonScanner() {
      setLayout(new GridLayout(1, 0));
      for (int i = 0; i < panels.length; i++) {
         panels[i] = new JPanel();
         panels[i].add(Box.createRigidArea(RIGID_AREA));
         panels[i].setBackground(BACKGROUND);
         add(panels[i]);
      }
      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   private class TimerListener implements ActionListener {
      private int count = 0;
      private boolean goingRight = true;

      @Override
      public void actionPerformed(ActionEvent e) {
         if (goingRight) {
            int index = (count - 2 + panels.length) % panels.length;
            panels[index].setBackground(BACKGROUND);
            int prior = Math.max((count - 1) % panels.length, 0);
            int next = Math.min(count + 1, panels.length - 1);
            panels[prior].setBackground(PARTIAL_COLOR);
            panels[next].setBackground(PARTIAL_COLOR);
            panels[count].setBackground(ACTIVE_COLOR);
            count++;
            if (count == panels.length) {
               count--;
               goingRight = false;
            }
         } else {
            int index = (count + 2) % panels.length;
            panels[index].setBackground(BACKGROUND);
            int prior = Math.max((count - 1) % panels.length, 0);
            int next = Math.min(count + 1, panels.length - 1);
            panels[prior].setBackground(PARTIAL_COLOR);
            panels[next].setBackground(PARTIAL_COLOR);
            panels[count].setBackground(ACTIVE_COLOR);
            count--;
            if (count == -1) {
               count++;
               goingRight = true;
            }
         }
      }
   }

   private static void createAndShowGui() {
      LarsonScanner mainPanel = new LarsonScanner();

      JFrame frame = new JFrame("LarsonScanner");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2013-11-03T22:02:43.890 回答
1

我扩展了我的评论。我使用 List 来简化来回扫描。

这是 LarsonScanner 测试的图片。

在此处输入图像描述

这是 LarsonScanner JPanel 类。

package com.ggl.larson.scanner;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LarsonScanner extends JPanel {
    private static final long   serialVersionUID    = 8213921815383053375L;

    private static int          index               = 0;

    private int                 width;
    private int                 height;
    private int                 widthInterval;
    private int                 gap;

    private int                 segmentCount;

    private long                displayInterval;

    private List<Integer>       sequence;

    public LarsonScanner() {
        this.height = 16;
        this.width = 0;

        this.segmentCount = 10;
        this.displayInterval = 200L;

        this.sequence = new ArrayList<Integer>();
        setSequence();
    }

    public void setSegmentCount(int segmentCount) {
        this.segmentCount = segmentCount;
        setSequence();
    }

    public void setDisplayInterval(long displayInterval) {
        this.displayInterval = displayInterval;
    }

    private void setSequence() {
        this.sequence.clear();

        for (int i = 0; i < segmentCount; i++) {
            this.sequence.add(Integer.valueOf(i));
        }

        for (int i = segmentCount - 2; i > 0; i--) {
            this.sequence.add(Integer.valueOf(i));
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        if (width <= 0) {
            width = (getSize().width - segmentCount * 3) / segmentCount;
            widthInterval = getSize().width / segmentCount;
            gap = (getSize().width - widthInterval * segmentCount + 3) / 2;
            new Thread(new SequenceRunnable()).start();
        }

        g.setColor(Color.BLUE);
        for (int i = 0; i < segmentCount; i++) {
            int x = i * widthInterval + gap;
            g.fillRect(x, 0, width, height);
        }

        g.setColor(Color.RED);
        int x = sequence.get(index) * widthInterval + gap;
        g.fillRect(x, 0, width, height);
        index = ++index % sequence.size();
    }

    public class SequenceRunnable implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(displayInterval);
                } catch (InterruptedException e) {
                }

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        repaint();
                    }
                });
            }
        }

    }
}

这是一个 TesterFrame 类,展示了如何使用 LarsonScanner 类。

package com.ggl.larson.scanner;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TesterFrame implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("Larson Scanner");

        LarsonScanner scanner = new LarsonScanner();
        scanner.setSegmentCount(12);
        scanner.setDisplayInterval(100L);
        frame.add(scanner);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600, 100));
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TesterFrame());
    }

}
于 2013-11-04T00:21:27.043 回答