问题描述
在JPanel中添加了一个JScrollPane,在JScrollPane中newJScrollPane(jTable),在从新点击按钮时,想要清除JPanel中的所有组件,我用了jPanel.removeAll(),但是却实现不了。这是怎么回事?
解决方案
解决方案二:
importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.SwingUtilities;publicclassJPanelExample{publicJPanelExample(){finalJFrameframe=newJFrame("JPanelExample");JTabletable=newJTable();finalJScrollPanescroll=newJScrollPane(table);finalJPanelpanel=newJPanel();panel.add(scroll);frame.getContentPane().add(panel);finalJButtonbutton=newJButton("RemoveComponentinJPanel");button.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){SwingUtilities.invokeLater(newRunnable(){@Overridepublicvoidrun(){frame.setVisible(false);panel.removeAll();panel.revalidate();//((JPanel)frame.getContentPane()).revalidate();button.setEnabled(false);frame.setVisible(true);}});}});frame.getContentPane().add(button,BorderLayout.SOUTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}publicstaticvoidmain(finalString[]args){SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){newJPanelExample();}});}}