1

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

-Sayantan

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();
   }                 

注意:我使用的是 DesignGridLayout 包。

4

2 回答 2

1

这有效:

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);
    }

}
于 2013-03-21T15:29:33.860 回答
1

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

然后你需要类似的代码:

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

尽管我质疑任何我看到removeAll(). 您可能会考虑使用Card Layout

于 2013-03-21T15:39:13.310 回答