How to remove a JButton in a JButton matrix?

故事扮演 提交于 2020-01-25 14:16:55

问题


I want to remove a certain botton using MouseListener from a matrix of bottons and add a JLabel in the empty, so I use:

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

But nothing happen, haha Thanks for the help.


回答1:


When you add/remove components from a visible GUI the basic code is:

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

Also "MyJPanel" is not a standard Java variable name. Variable names in Java should NOT start with an upper case character. You didn't do that with your other variables, so be consistent!




回答2:


Given the fact the i and j have no context in the mouse listener method, no, it's probably not a good idea.

The next question is, what is the mouse listener attached to? If it's attached to the button, then it might be better to use a ActionListener.

In either case you could use the source of the event...

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

Updated

A simpler solution might be to simply dump the buttons and labels into a List, for example...

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

    }

}

This makes looking up what has being actioned and what needs to be replaced easier.

When switching components, you also need to know where the component needs to be added, for this I simply used getComponentZOrder before I removed the JButton



来源:https://stackoverflow.com/questions/19919807/how-to-remove-a-jbutton-in-a-jbutton-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!