ユーザーがJButtonをクリックしてactionListenerのループに問題があるときに、GUI上を循環したい透明な画像のリストがあります。私は3つのJCheckBoxesを持っているので、どれが選択されているかによって、そのタイプが変更されます。今は1つ以上の画像を同時に表示することはできませんし、JButtonをもう一度クリックすると、次の画像に変更することはできません。また、faceImagesの画像は、JCheckBoxが選択されていないときに変更したいソリッドサークルですので、他の画像の背後に配置する方法を理解する必要があります。Jbuttonがクリックされたときにアイコンリストを反復するActionListenerのループを作成する
:)に私を助けるためには、ここに私のコードです:(私が働いているActionPerforedは一番下にあるが、私は参考のために残りの部分を残しておきます)setIconルーピング
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);
}
}
}
を私はループにそれをしたいリスト内の次へのJButtonがあるたびにクリックした – codeAllDay
私はそのようなものを試しましたが、タイプ、アイコンと互換性のない演算子で問題にぶつかっています。 – codeAllDay
あなたはどのオペレータを参照していますか?ここにテンプレートに従ってコードを書いてみてください。もしエラーが出たら、それを見直すことができます。 –