/*每次panel都是new的,为什么加上去以后原来组件还在?调用validate(),updateUI(),repaint()也没用。难道只有重新new  Dialog 使得刷新的感觉非常明显?求指导*/
package EditCollection;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;//简单主界面public class Edit extends JFrame implements ActionListener { private String s;
private JTextArea textarea;
private JButton jButton1, jButton2, jButton3;
private JLabel jLabel, jLabel1, jLabel2, jLabel3, jLabel4, jLabel5;
private JPanel jPanel, jPanel1, jPanel2, jPanel3, jPanel4, jPanel5,
jPanel6;
private JDialog dialog; // init方法,实例化各组件
public void initGUI() { // 各面板的实例化
jPanel = new JPanel();
jPanel1 = new JPanel();
jPanel2 = new JPanel();
jPanel3 = new JPanel();
jPanel4 = new JPanel();
jPanel5 = new JPanel();
jPanel6 = new JPanel();
// 清空各面板内的组件
jPanel.removeAll();
jPanel1.removeAll();
jPanel2.removeAll();
jPanel3.removeAll();
jPanel4.removeAll();
jPanel5.removeAll();
jPanel6.removeAll(); // 各标签的实例化
jLabel = new JLabel();
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel(); // 实例化Dialog,单列模式
if (dialog == null) { dialog = new JDialog();
} //else dialog.removeAll();                           !!!!!!!!!!!!/*这样写也不对*/ // 设置Dialog属性
dialog.setSize(400, 230);
dialog.setLocationRelativeTo(Edit.this);
dialog.setModalityType(ModalityType.APPLICATION_MODAL); } // 构造方法
public Edit() { // 设置JFrame各属性
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setBounds(dimension.width / 4, dimension.height / 4, 500, 300); // 设置Frame大小 jButton1 = new JButton("编辑"); // 实例化JButton
this.add(jButton1); // 添加button到Frame
jButton1.setBounds(184, 54, 81, 33); // 添加主界面按钮监听
jButton1.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { // 实例化第一个编辑框上jButton2,并绑定监听器
jButton2 = new JButton();
jButton2.addActionListener(Edit.this);
// 调用实例化方法
initGUI(); // 设置加入Dialog的面板布局
jPanel1.setLayout(new BorderLayout()); //
jPanel.add(jLabel); // 设置Label上的显示内容
jLabel1.setText("CardId:1");
jLabel.setText("*************"); // 设置panel2的south部分的面板属性
jPanel2.setLayout(new GridLayout(1, 2));
jPanel2.setSize(400, 100); // 设置按钮的属性
jButton2.setText("下一步");
jPanel1.setVisible(true);
jPanel2.setVisible(true);
jPanel2.setBackground(Color.cyan); // panel3,panel4为panel2的Grid布局填充面板
jLabel2.setBackground(Color.RED);
jLabel3.setBackground(Color.CYAN);
jLabel4.setBackground(Color.CYAN);
jLabel5.setBackground(Color.CYAN);
jLabel1.setSize(100, 30);
jPanel2.setSize(100, 30);
jPanel3.setSize(100, 30);
jPanel4.setSize(100, 30); jPanel2.add(jPanel3);
jPanel2.add(jPanel4);
jPanel4.add(jButton2); // 将JPanel加入Dialog中,实现自定义弹出窗口上内容
dialog.add(jPanel1); // 设置jPanel5的属性
jPanel5.setSize(200, 10000);
jPanel5.setLayout(new BorderLayout());
jPanel5.add(jLabel1, BorderLayout.NORTH);
jPanel5.add(jPanel6, BorderLayout.SOUTH); jPanel6.setLayout(new FlowLayout());
jPanel6.add(jLabel2);
jPanel6.add(jLabel3);
jPanel6.add(jLabel4);
jPanel6.add(jLabel5);
jPanel2.setSize(800, 500); // 将各组件放置入总面板中
jPanel1.add(jPanel5, BorderLayout.NORTH);
jPanel1.add(jPanel, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.SOUTH); jPanel.validate();
jPanel1.validate();
jPanel2.validate();
jPanel3.validate();
jPanel4.validate();
jPanel5.validate();
jPanel6.validate(); jPanel.updateUI();
jPanel1.updateUI();
jPanel2.updateUI();
jPanel3.updateUI();
jPanel4.updateUI();
jPanel5.updateUI();
jPanel6.updateUI(); jPanel.repaint();
jPanel1.repaint();
jPanel2.repaint();
jPanel3.repaint();
jPanel4.repaint();
jPanel5.repaint();
jPanel6.repaint(); // 最后设置dialog可见
dialog.setVisible(true);
}
}); } // 各按钮监听事项
@Override
public void actionPerformed(ActionEvent e) {
// 第一个编辑框上jButton2的按钮事件
if (e.getSource() == jButton2) { System.out.println("#################");
// 实例化第二个编辑框上jButton3,并绑定监听器
jButton3 = new JButton();
jButton3.addActionListener(this);
// 调用实例化方法
initGUI(); // 设置加入Dialog的面板布局
jPanel1.setLayout(new BorderLayout()); // 设置Label上的显示内容
jLabel1.setText("CardId:1");
jLabel.setText("确认事项"); // 设置panel2的south部分的面板属性
jPanel2.setLayout(new GridLayout(1, 2));
jPanel2.setSize(400, 100); // 设置按钮的属性
jButton3.setText("下一步2");
jButton3.setPreferredSize(new Dimension(100, 28));
jPanel1.setVisible(true);
jPanel2.setVisible(true);
jPanel2.setBackground(Color.cyan); // panel3,panel4为panel2的Grid布局填充面板
jLabel2.setBackground(Color.RED);
jLabel3.setBackground(Color.CYAN);
jLabel4.setBackground(Color.CYAN);
jLabel5.setBackground(Color.CYAN);
jPanel2.add(jPanel3);
jPanel2.add(jPanel4);
jPanel4.add(jButton3); // 将JPanel加入Dialog中,实现自定义弹出窗口上内容
dialog.add(jPanel1); // 设置jPanel5的属性
jPanel5.setSize(200, 600);
jPanel5.setLayout(new BorderLayout());
jPanel5.add(jLabel1, BorderLayout.NORTH);
jPanel5.add(jPanel6, BorderLayout.SOUTH); jPanel6.setLayout(new FlowLayout());
jPanel6.add(jLabel2);
jPanel6.add(jLabel3);
jPanel6.add(jLabel4);
jPanel6.add(jLabel5);
jPanel2.setSize(800, 500);
jPanel6.setBackground(Color.cyan); // 将各组件放置入总面板中
jPanel1.add(jPanel5, BorderLayout.NORTH);
jPanel1.add(jLabel, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.SOUTH); // 最后设置dialog可见
dialog.setVisible(true);
} // 第二个编辑框上jButton3的按钮事件
if (e.getSource() == jButton3) {
System.out.println("!!!@@@");
dialog.dispose();
}
} // main方法
public static void main(String args[]) { Edit edit = new Edit();
edit.setVisible(true);
}}

解决方案 »

  1.   

    懒得看代码咯 
    要刷新被移除和新增加的话
    你试试先把setVisible设为false 然后下一句再设回true看看
      

  2.   

    问题简单描述下:swing 中JFrame中放置一个单独的按钮,点击后产生一个JDailog,Dialog上放置了各层JPanel ,Dialog右下一个JPanel中放置了一个“下一步”按钮,点击后此按钮后Dailog不重新new,其他Panel等组件一律重新new并且添加至这个Dailog,并且条用各Panel的removeAll,validate以及repaint,updateUI方法均不能去除原有组件,与现有组件重叠,求解决方法。另外,我试过Dialog的removeAll(),会出现新的问题.