0

ボットのマトリックスから MouseListener を使用して特定のボットを削除し、空の JLabel を追加したいので、次を使用します。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public MyClass(){
        object = new Object();
        bottons = new JButton[5][5];
        labels = new JLabel[5][5];
        myPanel = new JPanel();
        myPanel.setLayout(new GridLayout(5,5));
        click =false;

        for(int i = 0 ; i<5 ; i++){
            for(int j = 0; j<5 ; j++){
                myPanel.add(bottons[i][j] = new JButton());
            }
        }
}

public void mouseReleased(MouseEvent e)
    if(click){
                remove(bottons[object.getx][object.gety]);//this is correct? or is there another way?
                myJPanel.add(labels[object.getx][object.gety] = new JLabel("1"));

                click = false;
    }

しかし、何も起こりません、笑 助けてくれてありがとう。

4

2 に答える 2

1

表示されている GUI からコンポーネントを追加/削除するときの基本的なコードは次のとおりです。

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

また、「MyJPanel」は標準 Java 変数名ではありません。Java の変数名は大文字で始めるべきではありません。他の変数ではそうしなかったので、一貫性を持たせてください!

于 2013-11-12T02:33:46.680 に答える
1

iおよびjがマウス リスナ メソッドにコンテキストを持たないという事実を考えると、いいえ、おそらく良い考えではありません。

次の質問は、マウス リスナーが接続されているものは何かということです。ボタンにアタッチされている場合は、ActionListener.

どちらの場合でも、イベントのソースを使用できます...

Object source = e.getSource();
if (source instanceof JButton) {
    JButton btn = (JButton)source;
    //find index of button in array...
    remove(btn);
    //...
    revalidate();
}

更新しました

より簡単な解決策は、ボタンとラベルを単にダンプすることListです。たとえば...

ここに画像の説明を入力

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonUpdates {

    public static void main(String[] args) {
        new ButtonUpdates();
    }

    public ButtonUpdates() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements ActionListener {

        private List<JButton> btns = new ArrayList<>(25);
        private List<JLabel> lbls = new ArrayList<>(25);

        public TestPane() {

            setLayout(new GridLayout(5,5));
            for (int index = 0; index < 25; index++) {
                JButton btn = new JButton(Integer.toString(index));
                JLabel lbl = new JLabel(Integer.toString(index));
                btns.add(btn);
                lbls.add(lbl);
                btn.addActionListener(this);
                add(btn);
            }

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof JButton) {
                JButton btn = (JButton) source;
                int index = btns.indexOf(source);
                JLabel lbl = lbls.get(index);

                index = getComponentZOrder(btn);
                remove(btn);
                add(lbl, index);

                revalidate();
            }
        }

    }

}

これにより、何を実行し、何を置き換える必要があるかを簡単に調べることができます。

コンポーネントを切り替えるときは、コンポーネントを追加する必要がある場所も知っておく必要がありますgetComponentZOrderJButton

于 2013-11-12T02:37:34.797 に答える