gpt4 book ai didi

java - 创建一个循环,以便 ActionListener 在单击 Jbutton 时遍历图标列表

转载 作者:行者123 更新时间:2023-12-02 03:10:56 25 4
gpt4 key购买 nike

我有一些透明图像列表,当用户单击 JButton 时,我想在 GUI 上循环浏览这些透明图像,但在 actionListener 中的循环遇到问题。我有 3 个 JCheckBox,因此根据选择的 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);
}
}
}

最佳答案

循环 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() 时,它会创建第二个线程。该线程遍历不同功能的各种 if,并在图像上循环。对于每个图像,它设置图标并等待一段时间(100 毫秒)。为了设置图像,您不能从此线程直接调用 setIcon(),因为通常 swing API 必须由事件调度线程调用。因此,您可以在新线程中调用 SwingUtilities.invokeLater(),它将可运行项排入队列以供 EDT 执行。

编辑:

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

private int eyeIdx = 0;
public void actionPerformed(ActionEvent e) {
if (checkBox.Eyes.isSelected()) {
label.setIcon(eyesImages[eyeIdx]);
eyeIdx++;
if (eyeIdx >= eyesImages.length) {
eyeIdx = 0;
}
}
...
}

关于java - 创建一个循环,以便 ActionListener 在单击 Jbutton 时遍历图标列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com