当Jbutton单击时,为ActionListener创建一个循环以迭代Icon列表

时间:2016-12-10 01:36:16

标签: java loops icons jbutton jcheckbox

我有透明图像列表,当用户点击JButton并且在actionListener中遇到循环问题时,我想在GUI上循环。我有3个JCheckBox,因此根据选择的那个,这是改变的类型。现在,我无法同时显示多张图片,而且当再次点击JButton时,我无法将其更改为下一张图片。此外,faceImages中的图像是实心圆圈,当我没有选择任何JCheckBox时我想要更改它,所以我必须弄清楚如何将它放在其他图像之后..所以请你选择哪个问题你我想帮助我:)

这是我的代码:(我正在处理的ActionPerfored位于最底层,但我将剩下的作为参考)

public Color myColor;
private JPanel contentPane;
JLabel label;
JCheckBox checkBoxEyes;
JCheckBox checkBoxNose;
JCheckBox checkBoxMouth;
JButton btnSubmit;

Icon plainFace = new ImageIcon(getClass().getResource("plainCircle.png"));
Icon pinkFace = new ImageIcon(getClass().getResource("pinkCircle.png"));
Icon redFace = new ImageIcon(getClass().getResource("redCircle.png"));
Icon greenFace = new ImageIcon(getClass().getResource("greenCircle.png"));

Icon eyes1 = new ImageIcon(getClass().getResource("eyes1.png"));
Icon eyes2 = new ImageIcon(getClass().getResource("eyes2.png"));
Icon eyes3 = new ImageIcon(getClass().getResource("eyes3.png"));

Icon nose1 = new ImageIcon(getClass().getResource("nose1.png"));
Icon nose2 = new ImageIcon(getClass().getResource("nose2.png"));
Icon nose3 = new ImageIcon(getClass().getResource("nose3.png"));

Icon mouth1 = new ImageIcon(getClass().getResource("mouth1.png"));
Icon mouth2 = new ImageIcon(getClass().getResource("mouth2.png"));
Icon mouth3 = new ImageIcon(getClass().getResource("mouth3.png"));

Icon faceImages[] =
{ pinkFace, greenFace, redFace };
Icon eyesImages[] =
{ eyes1, eyes2, eyes3 };
Icon noseImages[] =
{ nose1, nose2, nose3 };
Icon mouthImages[] =
{ mouth1, mouth2, mouth3 };

public ArrayList<Shape> eyeList = new ArrayList<Shape>();
public ArrayList<Shape> noseList = new ArrayList<Shape>();
public ArrayList<Shape> mouthList = new ArrayList<Shape>();
public ArrayList<Shape> backgroundList = new ArrayList<Shape>();

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    // EventQueue
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                Face frame = new Face();
                frame.setVisible(true);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Face()
{
    setTitle("Face");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 550, 500);
    contentPane = new JPanel();
    contentPane.setBackground(Color.GRAY);
    contentPane.setSize(new Dimension(200, 300));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 31, 97, 218);
    panel.setAlignmentY(Component.TOP_ALIGNMENT);
    contentPane.add(panel);

    checkBoxEyes = new JCheckBox("Eyes");
    checkBoxEyes.getBounds();
    checkBoxEyes.setVerticalAlignment(SwingConstants.TOP);
    checkBoxEyes.setSelected(false);

    panel.setLayout(new MigLayout("", "[83px]", "[45.00px][45.00px][46.00px][50.00px]"));
    panel.add(checkBoxEyes, "cell 0 0,grow");

    checkBoxNose = new JCheckBox("Nose");
    checkBoxNose.setVerticalAlignment(SwingConstants.TOP);
    checkBoxNose.setSelected(false);

    panel.add(checkBoxNose, "cell 0 1,grow");

    checkBoxMouth = new JCheckBox("Mouth");
    checkBoxMouth.setVerticalAlignment(SwingConstants.TOP);
    checkBoxMouth.setSelected(false);

    panel.add(checkBoxMouth, "cell 0 2,grow");

    btnSubmit = new JButton("Submit");
    btnSubmit.getBounds(new Rectangle(10, 10, 50, 50));
    btnSubmit.addActionListener(this);

    panel.add(btnSubmit, "cell 0 3");

    label = new JLabel("");
    label.setLocation(102, 31);
    label.setIcon(plainFace);
    label.setPreferredSize(new Dimension(800, 500));
    label.setSize(new Dimension(421, 381));
    label.setOpaque(true);
    label.setBackground(Color.ORANGE);

    contentPane.add(label);
}

public void createFace(Graphics g)
{
    super.paint(g);
    g.setColor(getColor());
    g.fillOval(20, 20, 100, 100);
}

public Color getColor()
{
    return myColor;
}


public void actionPerformed(ActionEvent e)
{

    if (checkBoxEyes.isSelected() == true)
    {
        for(Icon i : eyesImages)
        {
            label.setIcon(i);
        }
    }

    if (checkBoxNose.isSelected() == true)
    {
        for(Icon i : noseImages)
        {
            label.setIcon(i);
        }

    }

    if (checkBoxMouth.isSelected() == true)
    {
        for(Icon i : mouthImages)
        {
            label.setIcon(i);

        }
    } else
    {
        for(Icon i : faceImages)
        {
            label.setIcon(i);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在setIcon()上循环会产生如此快速地更改图标的效果,最终只能看到您设置的最后一个图标。你想要做的是在循环中插入一个延迟。但是,actionPerformed()由事件调度线程执行,该线程也是负责绘制UI的线程,因此当线程在其中(并更改图标)时,不会呈现UI。所以,这是你应该尝试的:

j

这是这样做的:当事件调度线程调用actionPerformed()时,它会创建第二个线程。该线程遍历不同功能的各种ifs,并在图像上循环。对于每个图像,它设置图标并等待一段时间(100毫秒)。为了设置图像,你不能直接从这个线程调用setIcon(),因为一般的swing API必须由事件调度线程调用。因此,从新线程中调用SwingUtilities.invokeLater(),它将可执行文件排入EDT执行。

编辑:

啊,如果你想在每次按下按钮时转到下一张图像:

public void actionPerformed(ActionEvent e) {
  final boolean eyes = checkBoxEyes.isSelected();
  /* add booleans for other features here */
  Thread t = new Thread() {
    public void run() {
      if (eyes) {
        for(Icon i : eyesImages) {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                label.setIcon(i)); 
              }
            });
            try {
              Thread.sleep(100);
            }catch(ThreadInterruptedException ex){}
          }
        }
      }
      // add cases for other features
    }        
  }
  t.start();
}