【问题标题】:How to remove a JButton in a JButton matrix?如何删除 JButton 矩阵中的 JButton?
【发布时间】:2013-11-12 02:28:51
【问题描述】:

我想使用 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;
    }

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

【问题讨论】:

    标签: java swing jpanel jbutton jlabel


    【解决方案1】:

    当您从可见的 GUI 添加/删除组件时,基本代码是:

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

    “MyJPanel”也不是标准的 Java 变量名。 Java 中的变量名不应以大写字符开头。您没有对其他变量执行此操作,因此请保持一致!

    【讨论】:

    • 只需添加 Mypanel.remove... 还是一样的:/
    • 只是为了说明这个名字
    【解决方案2】:

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

    下一个问题是,鼠标监听器附加到什么上?如果它附加到按钮上,那么最好使用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();
                }
            }
    
        }
    
    }
    

    这使得查找正在执行的操作以及需要替换的内容变得更加容易。

    在切换组件的时候,你还需要知道组件需要添加到哪里,为此我在删除JButton之前简单地使用了getComponentZOrder

    【讨论】:

    • 对于未来的propuses,我将使用鼠标右键,在鼠标按下时,我的变量click在单击JButton时变为true。
    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多