在一个Jframe中西边4个按钮,中间一个Panel ,我想点击按钮,那个panel能同时更新为新的,也就是覆盖原来的,我把主要的代码附上,高手帮我看看,现在的问题是,点击一个按钮,中间的panel即时显示了,但是点击其他的按钮,就不能再即时显示了,要几个按钮切换着点击才能达到我开始想要的效果,现在的问题就好想刷新跟不上。public class StatisticsManagerListener implements ActionListener {
private String popedom;
private Index index;
private StatisticManagerPanel stmp = new StatisticManagerPanel();
public StatisticsManagerListener(Index index) {
this.index = index;
}
public void actionPerformed(ActionEvent e) {
this.popedom = index.getPopedom();
index.getCenterPanel().add(stmp);
index.getCenterPanel().updateUI();
}
}
public class StudentManagerListener implements ActionListener {
private String popedom;
private Index index;
private StudentManagerPanel smp = new StudentManagerPanel();
public StudentManagerListener(Index index) {
this.index = index;
}
public void actionPerformed(ActionEvent e) {
this.popedom = index.getPopedom();
index.getCenterPanel().add(smp);
index.getCenterPanel().updateUI();
}}public class Index extends JFrame {
public Index(String title,String popedom){
this.popedom = popedom;
this.setTitle(title);
Dimension dimension = this.getToolkit().getScreenSize();
     this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     init();
     this.setResizable(false);
     this.setVisible(true);
}


private void init() {
this.addPanel();
this.addListener();
}private void addPanel() {
        /* 西部组建添加*/
gbc.addLayoutComponent(bw1, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,40,80,80),4,2));
gbc.addLayoutComponent(bw2, new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,40,80,80),4,2));
gbc.addLayoutComponent(bw3, new GridBagConstraints(0,2,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,40,80,80),4,2));
gbc.addLayoutComponent(bw4, new GridBagConstraints(0,3,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,40,80,80),4,2));
westpanel.add(bw1);
westpanel.add(bw2);
westpanel.add(bw3);
westpanel.add(bw4);
this.add(westpanel,BorderLayout.WEST);
 * 中部组建添加*/
centerPanel.setBackground(Color.BLUE);
this.add(centerPanel,BorderLayout.CENTER);
}
private void addListener() {
bw2.addActionListener(new StudentManagerListener(this));
bw3.addActionListener(new StatisticsManagerListener(this));
}

解决方案 »

  1.   

    在监听里,把原来的Panel Remove掉,将新得添加进去
    再调用updateUI
    不行再掉用repaint
    还不行就再调用validate
      

  2.   

    不要让centerPanel一直add.
    在两个Listener中分别先clear centerPanel:index.getCenterPanel().removeAll();
      

  3.   

    谢谢你  我马上就去调试  你能给我讲讲updateUI repaint validate的区别和意义吗
      

  4.   

    楼主是想刷新界面吧,给你一点代码参考一下吧Container c=getContentPane();//获得当前的容器
    c.remove(jl1);//删除原来的组件
    addD(intf.getText().trim(),"intype","intype");
    v2=new Vector();
    jlin=new JList(getData(v2,"intype"));//重新new一个组件
    jl1=new JScrollPane(jlin);
    jl1.setBounds(240, 20, 80, 100);
    c.add(jl1);//添加上
    c.repaint();//刷新
    c.validate();
      

  5.   

    Thank  you   这么多朋友帮助  真的好感谢
      

  6.   

    1楼2楼的朋友,能给我讲讲为什么要removeall 呢  确实解决问题了  但是我还想知道是为什么?
      

  7.   

    如果不removeall,那么你的centerPanel里会有无数个stmp (panel)和smp(panel),
    其实centerPanel是刷新了,但是却显示不了这么多的panel.
    肉眼看到的永远是第一个add上去的panel,
    所以要removeall以前的panel
      

  8.   

    private JPanel centerPanel = new JPanel(new BorderLayout()); 这是我在Idenx 中定义的中间那个主Panel,BorderLayout不是默认每次直接添加到中间把原来的覆盖掉了吗?
    还有就是原先的代码不是完全不能刷新,是要几个按钮切换的点一遍才能实现。
    小弟初学,还不是很清楚,能不能再给我讲一下啊  麻烦啦  谢谢^_^
      

  9.   

    看你是怎么添加的组件了
    如果是直接调用add方法,没有给要添加的组件设置标识符(layout时需要用),那么默认的标识符为null
    一直添加组件,所有的组件标识符都是一样的,都为null
    那么系统做layout时,可能会出现问题,所以刷新有可能不正确
      

  10.   

    我就是直接调用add 你看:index.getCenterPanel().add(stmp);  我是心想不指定Layout的属性,它都会加在中间,就会把原先的自动覆盖掉。“那么系统做layout时,可能会出现问题,所以刷新有可能不正确”
     是系统的问题吗?那我就这样理解。
      

  11.   

    应该不算系统的问题
    算调用者利用不正确
    ----------------------------------------------
    你可以调用带参数的add阿
    给每一次添加的组件都戴上标识
    这样,当你刷新之后还可以再使用以前的组件
    比如,先按A按钮,Panel A 添加上了
    再按B按钮, Panel B 添加上了
    再按A按钮,如果此时什么都不干的话,那么直接将Panel A 再刷到最前面,不用再新建一个对象
    总之,看你怎么用了,和业务相关,有时需要保留,有时需要dispose掉,看你的业务需要,有些时候不是不表示的东西都不存在