Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为ActionListener创建循环,以便在单击Jbutton时遍历图标列表_Java_Loops_Icons_Jbutton_Jcheckbox - Fatal编程技术网

Java 为ActionListener创建循环,以便在单击Jbutton时遍历图标列表

Java 为ActionListener创建循环,以便在单击Jbutton时遍历图标列表,java,loops,icons,jbutton,jcheckbox,Java,Loops,Icons,Jbutton,Jcheckbox,我有一些透明图像的列表,当用户单击JButton并在actionListener中遇到循环问题时,我希望在GUI上循环浏览这些图像。我有3个JCheckbox,因此根据选择的JCheckbox,类型会发生变化。现在,我无法同时显示多张图片,并且无法在再次单击JButton时将其更改为下一张图片。此外,faceImages中的图像是实心圆,我希望在未选择任何JCheckbox时更改这些圆,因此我必须找出如何将其置于其他图像之后。。因此,请选择您希望在哪个问题上帮助我:) 以下是我的代码:(我正在处

我有一些透明图像的列表,当用户单击JButton并在actionListener中遇到循环问题时,我希望在GUI上循环浏览这些图像。我有3个JCheckbox,因此根据选择的JCheckbox,类型会发生变化。现在,我无法同时显示多张图片,并且无法在再次单击JButton时将其更改为下一张图片。此外,faceImages中的图像是实心圆,我希望在未选择任何JCheckbox时更改这些圆,因此我必须找出如何将其置于其他图像之后。。因此,请选择您希望在哪个问题上帮助我:)

以下是我的代码:(我正在处理的ActionPerfored位于最底层,但我将留下其余部分供参考)

公共色myColor;
私有JPanel内容窗格;
JLabel标签;
JCheckBox复选框是;
JCheckBox-checkBoxNose;
JCheckBox-checkBoxMouth;
JButton btnSubmit;
Icon plainFace=newimageicon(getClass().getResource(“plainCircle.png”);
Icon pinkFace=newimageicon(getClass().getResource(“pinkCircle.png”);
Icon redFace=新的图像图标(getClass().getResource(“redCircle.png”);
Icon greenFace=newimageicon(getClass().getResource(“greenCircle.png”);
Icon eyes1=新图像图标(getClass().getResource(“eyes1.png”);
Icon eyes2=新图像图标(getClass().getResource(“eyes2.png”);
Icon eyes3=新图像图标(getClass().getResource(“eyes3.png”);
Icon nose1=新的图像图标(getClass().getResource(“nose1.png”);
Icon nose2=新的图像图标(getClass().getResource(“nose2.png”);
Icon nose3=新的图像图标(getClass().getResource(“nose3.png”);
Icon mout1=新的图像图标(getClass().getResource(“mout1.png”);
Icon mout2=新的图像图标(getClass().getResource(“mout2.png”);
Icon mout3=新的图像图标(getClass().getResource(“mout3.png”);
图标面图像[]=
{pinkFace、greenFace、redFace};
图标图像[]=
{eyes1,eyes2,eyes3};
图标图像[]=
{nose1,nose2,nose3};
图标图像[]=
{嘴1,嘴2,嘴3};
public ArrayList eyeList=新的ArrayList();
public ArrayList noseList=new ArrayList();
public ArrayList moutlist=new ArrayList();
public ArrayList backgroundList=new ArrayList();
/**
*启动应用程序。
*/
公共静态void main(字符串[]args)
{
//事件队列
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
尝试
{
面框=新面();
frame.setVisible(true);
}捕获(例外e)
{
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公众形象()
{
片名(“面”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100550500);
contentPane=newjpanel();
contentPane.setBackground(颜色:灰色);
setSize(新维度(200300));
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel面板=新的JPanel();
嵌板.立根(0,31,97,218);
面板设置对齐Y(组件顶部对齐);
contentPane.add(面板);
checkBoxEyes=新的JCheckBox(“眼睛”);
checkBoxEyes.getBounds();
复选框Yes.setVerticalAlignment(SwingConstants.TOP);
复选框Yes.setSelected(假);
面板设置布局(新的migplayout(“,”[83px]”,“[45.00px][45.00px][46.00px][50.00px]”);
panel.add(选中框Yes,“单元格0,增长”);
checkBoxNose=新的JCheckBox(“Nose”);
复选框鼻。设置垂直对齐(SwingConstants.顶部);
checkBoxNose.setSelected(假);
panel.add(复选框nose,“单元格01,增长”);
checkBoxMouth=新的JCheckBox(“口”);
检查Boxmouth.设置垂直对齐(SwingConstants.顶部);
checkBoxMouth.setSelected(假);
添加(checkBoxMouth,“单元格02,生长”);
btnSubmit=新按钮(“提交”);
getBounds(新矩形(10,10,50,50));
btnSubmit.addActionListener(此);
面板添加(BTNSUPMIT,“单元格03”);
标签=新的JLabel(“”);
标签设置位置(102,31);
标签。设置图标(平面);
标签。设置首选尺寸(新尺寸(800500));
标签.设置尺寸(新尺寸(421381));
label.set不透明(true);
标签.背景(颜色.橙色);
contentPane.add(标签);
}
公共void createFace(图形g)
{
超级油漆(g);
g、 setColor(getColor());
g、 圆形(20,20,100,100);
}
公共颜色getColor()
{
返回颜色;
}
已执行的公共无效操作(操作事件e)
{
如果(checkBoxEyes.isSelected()==true)
{
用于(图标i:图像)
{
标签.设置图标(i);
}
}
if(checkBoxNose.isSelected()==true)
{
用于(图标i:图像)
{
标签.设置图标(i);
}
}
if(checkBoxMouth.isSelected()==true)
{
用于(图标i:图像)
{
标签.设置图标(i);
}
}否则
{
用于(图标i:faceImages)
{
标签.设置图标(i);
}
}
}
在setIcon()上循环将产生快速更改图标的效果,最终只会看到最后一个设置的图标。您要做的是在循环中插入一个延迟。但是,actionPerformed()由事件调度线程执行,该线程也负责绘制UI,因此当线程在其中(并更改图标)时,UI不会呈现。因此,以下是您应该尝试的:

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();
}
这就是它的作用:当事件分派线程调用actionPerformed()时,它会创建第二个线程。该线程遍历不同功能的各种ifs,并在图像上循环。对于每个图像,它设置图标并等待一段时间(100ms)。为了设置映像,不能从此线程直接调用setIcon(),因为在一般情况下,swing API必须由
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();
}
private int eyeIdx = 0;
public void actionPerformed(ActionEvent e) {
  if (checkBox.Eyes.isSelected()) {
    label.setIcon(eyesImages[eyeIdx]);
    eyeIdx++;
    if (eyeIdx >= eyesImages.length) {
      eyeIdx = 0;
    }
  }
  ...
}