gpt4 book ai didi

java - 如何使deleteAction删除其他JPanel的JPanel中的按钮?

转载 作者:行者123 更新时间:2023-12-02 04:30:16 26 4
gpt4 key购买 nike

我有一个私有(private) JPanel :

私有(private) JPanel bigPanel;

在bigPanel内部,我还有另外十个JPanel(panel_1D,panel_2D ...)。每个按钮都有几个按钮。这些按钮是通过 deleteAction actionListener 添加的:

JPanel panel_1D = new JPanel();//Panel ONE

GridBagConstraints gbc_panel_1D = new GridBagConstraints();...
bigPanel.add(panel_1D, gbc_panel_1D);
GridBagLayout gbl_panel_1D = new GridBagLayout();...
panel_1D.setLayout(gbl_panel_1D);

JButton button_2 = new JButton("ECG");
GridBagConstraints gbc_button_2 = new GridBagConstraints();...
button_2.addActionListener(deleteAction);
panel_1D.add(button_2, gbc_button_2);
...

我在其他面板(bigPanel 之外)创建了一个 DELETE 按钮:

JButton button_38 = new JButton("DELETE");
button_38.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Delete any task by clicking it." , "INFO :
", JOptionPane.INFORMATION_MESSAGE);
deleteNow = true;
//allowOpen =false; }
});

然后我设置了deleteAction方法:

private ActionListener deleteAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (deleteNow)
{ //allowOpen=false;
bigPanel.remove(button);
bigPanel.revalidate();
bigPanel.repaint();
deleteNow = false;

}
else
{
System.out.println("My COMMAND IS : " + button.getActionCommand());
}
}
};

目前,如果我运行,我将单击“删除”按钮。然后,弹出消息“通过单击删除任何任务”。显示。因此,当我关闭弹出消息时,我单击了要删除的按钮。不幸的是,我点击的按钮没有被删除。

更新:更改后

bigPanel.remove(button);
bigPanel.revalidate();
bigPanel.repaint();

button.getParent().remove(button);
button.getParent().revalidate();
button.getParent().repaint();

我在控制台中收到警告:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UI2$1.actionPerformed(UI2.java:128)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

您需要在包含要删除的组件的容器上调用 remove(),因此:

bigPanel.remove(button);

您应该调用例如:

panel1_D.remove(button);

因为按钮并不直接位于bigPanel中(而是位于子面板中)。但是,如果您有多个带有多个按钮的面板,更好的解决方案是:

button.getParent().remove(button);

getParent() 方法返回给定组件的父容器,因此它应该适用于每个按钮,并且您不需要为每组按钮创建方法不同的面板。

关于java - 如何使deleteAction删除其他JPanel的JPanel中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com