簡體   English   中英

如何刪除JButton矩陣中的JButton?

[英]How to remove a JButton in a JButton matrix?

我想從一個Bottons矩陣中使用MouseListener刪除某個botton並在空中添加一個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;
    }

但沒有任何事情發生,哈哈,謝謝你的幫助。

從可見GUI添加/刪除組件時,基本代碼為:

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

“MyJPanel”也不是標准的Java變量名。 Java中的變量名稱不應以大寫字符開頭。 你沒有用其他變量做到這一點,所以要保持一致!

鑒於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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM