Java Swing在单击要删除的Jpanel中存在的Jbutton时删除Jpanel

Java Swing在单击要删除的Jpanel中存在的Jbutton时删除Jpanel,java,swing,Java,Swing,我正在使用Netbeans创建一个GUI,我的问题是“如何删除jpanel”…现在的层次顺序是Jframe->Jscrollpane->jpanel->Jbutton。通过单击Jbutton,我想删除jpanel的所有组件,包括Jbutton(我正在单击)和面板本身。请帮忙。紧急。提前谢谢 -萨扬坦 private void b4ActionPerformed(java.awt.event.ActionEvent evt) {

我正在使用Netbeans创建一个GUI,我的问题是“如何删除jpanel”…现在的层次顺序是Jframe->Jscrollpane->jpanel->Jbutton。通过单击Jbutton,我想删除jpanel的所有组件,包括Jbutton(我正在单击)和面板本身。请帮忙。紧急。提前谢谢

-萨扬坦

private void b4ActionPerformed(java.awt.event.ActionEvent evt) {                                   
final JPanel jp;
JTextField tf,of1,of2,xf,yf;
int i=1;
int m=(int)sp3.getValue();
JButton jb;
jp=new JPanel();
//jp.setLayout(new GridBagLayout());
 DesignGridLayout layout = new DesignGridLayout(jp); 
  jsp2.getViewport().add(jp);  
   while(i<=m){
     tf=new JTextField(5);
     of1=new JTextField(5);
     of2=new JTextField(5);
    layout.row().grid(new JLabel("Type:")).indent(9).add(tf).grid(new     JLabel("Length:")).indent().add(of1).grid(new JLabel("Breadth:")).indent().add(of2).empty();
     fields1.add(tf);
     fields1.add(of1);
      fields1.add(of2);        
       xf=new JTextField(5);
     yf=new JTextField(5);
  layout.row().grid(new JLabel("X-axis:")).indent(9).add(xf).grid(new JLabel("Y-axis:")).indent().add(yf).empty(2);
      fields1.add(xf);
      fields1.add(yf);
      i++;
  }
  jb=new JButton("Submit");
  jb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
        for(JTextField field: fields1){
        String s=field.getText();
        lbr.setText(lbr.getText()+s);
        }
         lb3.setVisible(false);
       sp3.setVisible(false);
        b4.setVisible(false);
       //jp.setVisible(false);
       //jp.removeAll();
      // jp.revalidate();

      //jp.repaint();

      // jsp2.remove(jp);
      //jsp2.revalidate();
      //jsp2.repaint();
      }
    });
    layout.emptyRow();
    layout.row().right().add(jb);
   jp.revalidate();
   jp.repaint();
   // jsp2.revalidate();
   //jsp2.repaint();
   }                 
actionperformed(java.awt.event.ActionEvent evt){
最终JPanel jp;
JTextField,of1,of2,xf,yf;
int i=1;
int m=(int)sp3.getValue();
按钮;
jp=新的JPanel();
//jp.setLayout(新GridBagLayout());
DesignGridLayout=新的DesignGridLayout(jp);
jsp2.getViewport().add(jp);
而(i这是有效的:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ScratchSpace {

    public static void main(String[] args) {
        final JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(Color.YELLOW);
        panel.add(new JButton(new AbstractAction("Kill me") {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.revalidate();
                panel.repaint();
            }
        }));

        final JFrame frame = new JFrame();
        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
我想删除jpanel的所有组件,包括Jbutton(我正在单击它)和面板本身

然后您需要类似以下代码:

JButton button = (JButtton)event.getSource();
JPanel grandparent = button.getParent().getParent();
grandparent.removeAll();
grandparent.revalidate();
grandparent.repaint();

尽管我对看到
removeAll()
的任何代码都有疑问。但您可能会考虑使用。

您可以对示例代码进行更多简化。当前正在发生(或未发生)什么告诉我们你目前的结果是什么,我们可以提供更好的帮助。谁在教授这些奇怪的用户界面概念?哈哈哈@gilbert…我在用own@steve:如果我使用的是jpanel.renovall();只有jbutton被删除,所有内容都保持不变。在哪里使用代码?@camickr我对removeAll()发表了评论因为它没有发生。因为你说你想移除按钮和面板。
没有发生
没有帮助。发布你的SSCCE来演示问题。当你不知道某个术语时,你可以随时在网上搜索。为什么要等我两个小时来回答问题?当你问问题时,应该在SSCCE上发布你的问题在上。如何删除面板本身?@steveDesign Grid layout妨碍了它的执行…我收到了以下错误:“线程“AWT-EventQueue-0”java.lang.IllegalArgumentException中的异常:不要在net.java.dev.designgridlayout.DesignGridLayoutManager.RemovelLayoutComponent(DesignGridLayoutManager.java:122)中使用此方法使用DesignGridLayout,您不允许直接向面板添加或删除组件;您必须使用DesignGridLayout API。DesignGridLayout不允许在添加组件后删除组件,但如果需要,您可以
hide()
行或行组(
RowGroup
)。