-2
           for(i=0; i<16; i++){

                     images[i]=new ImageIcon(getClass()
    .getResource("/images/1 ("+i+").jpg"));

            }

 for( i=0; i<buttons.length; i++){
                for (j=0; j<buttons[i].length;j++){ 
                    n=i*buttons.length+buttons[i].length;
                    buttons[i][j]=new JButton();
                    label[n].setIcon((images[i*buttons.length+j]));
                    buttons[i][j].add(label[n]);
                    label[n].setVisible(false);


                    panel.add(buttons[i][j]);
                    buttons[i][j].addActionListener(this);

                }
            }




    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
            JButton pressedButton = (JButton) e.getSource();
            opens[open]=(JButton) e.getSource(); //i want to put label[n] into array?
            if((pressedButton.getIcon() == null)){

                label[n].setVisible(true);

                open=open++;
            } else {   
                //pressedButton.setIcon(null);
            }

            }
        if (open==1){
            opens[0].setVisible(false);
            opens[1].setVisible(false);
        }
    }

こんにちはフレンズ。私は記憶ゲームを作っています。ボタンが同じアイコンの場合、それらは開いたままになります。

パネルにフレーム、ボタンにフレームを作成し、各ボタンにラベルを付けました。向きが true でユーザーがクリックすると、 setvisible(true) によって開きます

しかし、無効なアクションが実行された場合、どうすればよいですかlabel[n]? Not button[][]?

label[n].setIcon((images[i*buttons.length+j]));

エラーだと思います。正しくありませんか?実行しないからです。

提案後に編集:

 for(i=0; i<16; i++){

             images[i]=new ImageIcon(getClass().getResource("/images/1 ("+i+").jpg"));

    } //adding images to local variables

            for( i=0; i<buttons.length; i++){
                for (j=0; j<buttons[i].length;j++){ 
                    n=i*buttons.length+buttons[i].length;
                    buttons[i][j]=new JButton();
                //buttons[i][j].setIcon((images[i*buttons.length+j]));
    //if i make this code, all icons are displayed at first?

                    panel.add(buttons[i][j]);
                    buttons[i][j].addActionListener(this);

                }
            }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
            JButton pressedButton = (JButton) e.getSource();
            if(pressedButton.getIcon() == null){
                pressedButton.setIcon((images[i*buttons.length+j]));
            } else {
                pressedButton.setIcon(null);
            }
        }
    }

あなたが提案した方法でこのコードを作成しました。しかし、今は画像が表示されず、クリックも機能しません。

4

2 に答える 2

2

この投稿はこの質問に関連していると思います。もしかして任務?

いくつかのヒント:

label[n].setIcon((images[i*buttons.length+j]));

images配列にはいくつの画像がありますか? 簡単に計算すると、たとえば 4x4 配列がある場合i*buttons.lenght+j == 4*4+4 == 20、最後の反復で画像が必要になります。i*j/2 == 8そして、ペアマッチゲームだとすれば、必要なのは画像だけです。

ボタンが同じアイコンの場合、それらは開いたままになります。パネルにフレーム、ボタンにフレームを作成し、各ボタンにラベルを付けました。

なぜラベルが必要なのですか?actionPerformed2 つのボタンが一致する場合は、それらのボタンを無効にして、ユーザーがそれらを再度クリックしてイベントをディスパッチできないようにすることができると思います。

厳密に配列を使用する必要がない場合は、ArrayList代わりに使用してそのメソッドの利点を得ることができます。

アップデート

まだ配列を使用していると思うので、このコードを投稿します。参照を保持するためにそれらを使用する必要がないことを示したいだけです。オブジェクトパラダイムの下でもう少し考えて、このタスクを適切なオブジェクトに委任する必要があります。

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;


public class Demo {    
    /* 
     * This variable will point to a pressed button to keep reference.
     */
    JButton _alreadyPressedButton = null;

    /**
     * Initializes the GUI
     */
    private void initGUI(){
        /*
         * Create needed icons - As this example uses 6 buttons, then I need 3 icons 
         */
        ImageIcon icon1 = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
        ImageIcon icon2 = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
        ImageIcon icon3 = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
        /*
         * Make a list with 6 icons (add each icon twice)
         */
        List<ImageIcon> iconsList = new ArrayList<>();
        iconsList.add(icon1);
        iconsList.add(icon1);
        iconsList.add(icon2);
        iconsList.add(icon2);
        iconsList.add(icon3);
        iconsList.add(icon3);
        Collections.shuffle(iconsList); /* Shuffle the list */

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(e.getSource() instanceof JButton){                    
                    final JButton pressedButton = (JButton) e.getSource();              
                    /* 
                     * Execute this code in a SwingWorker to prevent block EDT at "Thread.sleep(500)" line
                     * Google for EDT (Event Dispatch Thread)
                     */
                    SwingWorker sw = new SwingWorker() {
                        @Override
                        protected Object doInBackground() throws Exception {
                            if(_alreadyPressedButton != pressedButton){
                                pressedButton.setIcon(pressedButton.getPressedIcon());

                                if(_alreadyPressedButton != null){ 
                                    Thread.sleep(500);
                                    if(_alreadyPressedButton.getIcon() == pressedButton.getIcon()){
                                        _alreadyPressedButton.setEnabled(false);
                                        pressedButton.setEnabled(false);
                                    } else {
                                        _alreadyPressedButton.setIcon(null);
                                        pressedButton.setIcon(null);
                                    }
                                    _alreadyPressedButton = null;
                                } else {
                                    _alreadyPressedButton = pressedButton;
                                }

                            }
                            return null;
                        }

                    };

                    sw.execute();
                }
            }
        };

        JPanel panel = new JPanel(new GridLayout(3, 2));
        panel.setPreferredSize(new Dimension(200, 200));

        for(ImageIcon icon : iconsList){
            JButton button = new JButton();
            button.setPressedIcon(icon);
            button.addActionListener(actionListener);
            panel.add(button);
        }

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 new Demo().initGUI();
            }
        });        

    }
}
于 2013-09-23T18:34:21.290 に答える
1

JLabel を JButton に追加しているように見えますか?? これが実際にあなたがしていることである場合は、しないでください。代わりに、ボタンが表示するイメージを表示または変更したい場合は、JButton の ImageIcon を単純に設定しないでください。これは、 を介して簡単に実行できますmyButton.setIcon(fooIcon)



コメントであなたの状態を編集します:

jbuttonの画像をどのように表示して非表示にしますか?

アイコンを簡単に入れ替えることができます。画像を表示したい場合は、その ImageIcon を 経由で JButton のアイコンとして設定しsetIcon(myIcon)ます。setIcon(otherIcon)完了したら、またはのいずれかで交換しますsetIcon(null)

あなたが提供したリンクは、あなたがやろうとしているように見えるJButtonの上にJLabelsを配置していないため、この情報を伝えています。

于 2013-09-23T18:27:09.913 に答える