在一个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));
}
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));
}
再调用updateUI
不行再掉用repaint
还不行就再调用validate
在两个Listener中分别先clear centerPanel:index.getCenterPanel().removeAll();
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();
其实centerPanel是刷新了,但是却显示不了这么多的panel.
肉眼看到的永远是第一个add上去的panel,
所以要removeall以前的panel
还有就是原先的代码不是完全不能刷新,是要几个按钮切换的点一遍才能实现。
小弟初学,还不是很清楚,能不能再给我讲一下啊 麻烦啦 谢谢^_^
如果是直接调用add方法,没有给要添加的组件设置标识符(layout时需要用),那么默认的标识符为null
一直添加组件,所有的组件标识符都是一样的,都为null
那么系统做layout时,可能会出现问题,所以刷新有可能不正确
是系统的问题吗?那我就这样理解。
算调用者利用不正确
----------------------------------------------
你可以调用带参数的add阿
给每一次添加的组件都戴上标识
这样,当你刷新之后还可以再使用以前的组件
比如,先按A按钮,Panel A 添加上了
再按B按钮, Panel B 添加上了
再按A按钮,如果此时什么都不干的话,那么直接将Panel A 再刷到最前面,不用再新建一个对象
总之,看你怎么用了,和业务相关,有时需要保留,有时需要dispose掉,看你的业务需要,有些时候不是不表示的东西都不存在