如何删除JButton矩阵中的JButton?

时间:2013-11-12 02:28:51

标签: java swing jpanel jbutton jlabel

我想从按钮矩阵中使用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;
    }

但没有任何事情发生,哈哈 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

在可见GUI中添加/删除组件时,基本代码为:

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

另外&#34; MyJPanel&#34;不是标准的Java变量名。 Java中的变量名称不应以大写字符开头。你没有用其他变量做到这一点,所以要保持一致!

答案 1 :(得分:1)

鉴于ij在鼠标侦听器方法中没有上下文这一事实,不,这可能不是一个好主意。

接下来的问题是,附加的鼠标监听器是什么?如果它附加到按钮,则最好使用ActionListener

在任何一种情况下,您都可以使用事件的来源......

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

<强>更新

更简单的解决方案可能是简单地将按钮和标签转储到List中,例如......

enter image description here

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();
            }
        }

    }

}

这样可以更轻松地查找已采取行动的内容以及需要更换的内容。

切换组件时,您还需要知道组件需要添加的位置,为此我在删除getComponentZOrder

之前只使用了JButton