首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除JButton矩阵中的JButton?

如何删除JButton矩阵中的JButton?
EN

Stack Overflow用户
提问于 2013-11-12 10:28:51
回答 2查看 599关注 0票数 0

我想使用MouseListener从botton矩阵中删除某个botton,并在空格中添加一个JLabel,因此我使用:

代码语言:javascript
复制
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;
    }

但是什么也没发生,哈哈,谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2013-11-12 10:33:47

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

代码语言:javascript
复制
panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

"MyJPanel“也不是标准的Java变量名。Java中的变量名不应以大写字符开头。对于其他变量,您没有做到这一点,所以要保持一致!

票数 1
EN

Stack Overflow用户

发布于 2013-11-12 10:37:35

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

下一个问题是,鼠标监听器连接到什么?如果它连接到按钮上,那么使用ActionListener可能会更好。

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

代码语言:javascript
复制
Object source = e.getSource();
if (source instanceof JButton) {
    JButton btn = (JButton)source;
    //find index of button in array...
    remove(btn);
    //...
    revalidate();
}

已更新

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

代码语言:javascript
复制
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();
            }
        }

    }

}

这使得查找正在操作的内容和需要替换的内容变得更容易。

在切换组件时,您还需要知道需要将组件添加到何处,为此,我在删除JButton之前只使用了getComponentZOrder

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19919807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档