2016-12-10 3 views
0

ユーザーが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); 
     } 
    } 
} 

答えて

2

()はアイコンをすばやく変更する効果があり、最後に設定したアイコンだけが表示されます。あなたがしたいことは、ループに遅延を挿入することです。しかし、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()は、第2のスレッドを作成し、イベントディスパッチスレッドによって呼び出されます。このスレッドは、さまざまな機能のさまざまなifsを通過し、画像上でループします。各画像について、アイコンを設定し、しばらく(100ms)待ちます。イメージを設定するには、一般的なスイングAPIはイベントディスパッチスレッドによって呼び出されなければならないので、このスレッドから直接setIcon()を呼び出すことはできません。したがって、新しいスレッドから、SwingUtilities.invokeLater()を呼び出します。このスレッドは、EDTの実行のために実行可能ファイルをエンキューします。

EDIT:

ああ、あなたは次の画像にボタンが押されるたびに移動する場合:

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

を私はループにそれをしたいリスト内の次へのJButtonがあるたびにクリックした – codeAllDay

+0

私はそのようなものを試しましたが、タイプ、アイコンと互換性のない演算子で問題にぶつかっています。 – codeAllDay

+0

あなたはどのオペレータを参照していますか?ここにテンプレートに従ってコードを書いてみてください。もしエラーが出たら、それを見直すことができます。 –

関連する問題