我想在更新GUI,有一个事件触发,这个触发的时间就是在原来的 JFrame 的 center 的位置上,更新上面的JPanel。但是,我发现更新后,原来的JPanel上面的东西还有。我想问一下,应该怎么更新呢?我的更新语句如下,其中 ArrivingModel 是储存数据的一个类,ArrivingViewPanel 是 JPanel的子类。Ftp.getSimulationData()是一个数据源,数据源我看过了,是正确的,但是 Stp 也就是上文说的,要更新的JPanel,却在更新后还存有原来的东东,帮忙慢慢看看吧。
public void stateChanged(ChangeEvent arg0) {
ArrivingModel model = new ArrivingModel(Ftp.getSimulationData());
Stp.add(new ArrivingViewPanel(model),BorderLayout.CENTER);
Stp.updateUI();
break;
}class ArrivingViewPanel extends JPanel {
ArrivingModel model = null;
public ArrivingViewPanel(ArrivingModel _model){
super();
this.model = _model;
int size = _model.WorkflowInstance.size();
for(int i=0;i<size;i++){ JTextField order = new JTextField(Integer.toString(i+1), 2);
order.setEditable(false);
order.setHorizontalAlignment(JTextField.CENTER);
this.add(order);
JTextField wfinstance = new JTextField(_model.WorkflowInstance.elementAt(i), 11);
wfinstance.setBackground(model.WorkflowColor.elementAt(i));
wfinstance.setEditable(false);
this.add(wfinstance);
int t = _model.ArrbingInterval.elementAt(i);
String s = String.format("%02d:%02d:%02d", t/60/60,t/60%60,t%60);
JTextField time = new JTextField(s, 5);
time.setHorizontalAlignment(JTextField.CENTER);
// time.setEnabled(false);
time.setEditable(false);
this.add(time);
}
}
}class ArrivingModel {
public Vector<Integer> ArrbingInterval = new Vector<Integer>();
public Vector<String> WorkflowInstance = new Vector<String>();
public Vector<String> Workflow = new Vector<String>();
public Vector<Color> WorkflowColor = new Vector<Color>();
private RandomData randomData = new RandomDataImpl();
public ArrivingModel( /*Vector<String> _Workflow*/ ){
int size = 40;//_Workflow.size();
for(int i=0;i<size;i++){
int r = randomData.nextInt(-1, 255);
int g = randomData.nextInt(-1, 255);
int b = randomData.nextInt(-1, 255);
this.WorkflowColor.add(new Color(r,g,b));
this.ArrbingInterval.add(new Integer(randomData.nextInt(1, 30)));
this.WorkflowInstance.add("Workflow Instance");
}
}
public ArrivingModel( Object[][] _data ){
int size = _data.length;
for(int i=0;i<size;i++){
Boolean boo = (Boolean)_data[i][4];
if( boo.booleanValue() ){
Integer s = new Integer(((String)_data[i][3]));
int q = s.intValue();
for(int j=0;j<q;j++){
int r = randomData.nextInt(-1, 255);
int g = randomData.nextInt(-1, 255);
int b = randomData.nextInt(-1, 255);
this.WorkflowColor.add(new Color(r,g,b));
this.ArrbingInterval.add(new Integer(randomData.nextInt(1, 30)));
this.WorkflowInstance.add( (String)_data[i][0] + ".inst_" + (q+1) );
}
} }
}}class SecondTabPane extends JPanel implements ActionListener{
JRadioButton randmonArriving = new JRadioButton("random arriving");
JRadioButton historicalArriving = new JRadioButton("historical arriving");
JRadioButton customerArrving = new JRadioButton("customerize arriving");
private RandomData randomData = new RandomDataImpl();
ButtonGroup group = new ButtonGroup();
ArrivingModel aModel = new ArrivingModel();
public SecondTabPane(){ this.setLayout(new BorderLayout());
JPanel north = new JPanel();
north.setLayout(new GridLayout(1,0));
north.add(this.randmonArriving);
north.add(this.historicalArriving);
north.add(this.customerArrving);
this.randmonArriving.addActionListener(this);
this.historicalArriving.addActionListener(this);
this.customerArrving.addActionListener(this);
this.group.add(randmonArriving);
this.group.add(historicalArriving);
this.group.add(customerArrving);
this.add(north,BorderLayout.NORTH);
JPanel south = new JPanel();
this.add(south,BorderLayout.SOUTH);
} @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub }
}
public void stateChanged(ChangeEvent arg0) {
ArrivingModel model = new ArrivingModel(Ftp.getSimulationData());
Stp.add(new ArrivingViewPanel(model),BorderLayout.CENTER);
Stp.updateUI();
break;
}class ArrivingViewPanel extends JPanel {
ArrivingModel model = null;
public ArrivingViewPanel(ArrivingModel _model){
super();
this.model = _model;
int size = _model.WorkflowInstance.size();
for(int i=0;i<size;i++){ JTextField order = new JTextField(Integer.toString(i+1), 2);
order.setEditable(false);
order.setHorizontalAlignment(JTextField.CENTER);
this.add(order);
JTextField wfinstance = new JTextField(_model.WorkflowInstance.elementAt(i), 11);
wfinstance.setBackground(model.WorkflowColor.elementAt(i));
wfinstance.setEditable(false);
this.add(wfinstance);
int t = _model.ArrbingInterval.elementAt(i);
String s = String.format("%02d:%02d:%02d", t/60/60,t/60%60,t%60);
JTextField time = new JTextField(s, 5);
time.setHorizontalAlignment(JTextField.CENTER);
// time.setEnabled(false);
time.setEditable(false);
this.add(time);
}
}
}class ArrivingModel {
public Vector<Integer> ArrbingInterval = new Vector<Integer>();
public Vector<String> WorkflowInstance = new Vector<String>();
public Vector<String> Workflow = new Vector<String>();
public Vector<Color> WorkflowColor = new Vector<Color>();
private RandomData randomData = new RandomDataImpl();
public ArrivingModel( /*Vector<String> _Workflow*/ ){
int size = 40;//_Workflow.size();
for(int i=0;i<size;i++){
int r = randomData.nextInt(-1, 255);
int g = randomData.nextInt(-1, 255);
int b = randomData.nextInt(-1, 255);
this.WorkflowColor.add(new Color(r,g,b));
this.ArrbingInterval.add(new Integer(randomData.nextInt(1, 30)));
this.WorkflowInstance.add("Workflow Instance");
}
}
public ArrivingModel( Object[][] _data ){
int size = _data.length;
for(int i=0;i<size;i++){
Boolean boo = (Boolean)_data[i][4];
if( boo.booleanValue() ){
Integer s = new Integer(((String)_data[i][3]));
int q = s.intValue();
for(int j=0;j<q;j++){
int r = randomData.nextInt(-1, 255);
int g = randomData.nextInt(-1, 255);
int b = randomData.nextInt(-1, 255);
this.WorkflowColor.add(new Color(r,g,b));
this.ArrbingInterval.add(new Integer(randomData.nextInt(1, 30)));
this.WorkflowInstance.add( (String)_data[i][0] + ".inst_" + (q+1) );
}
} }
}}class SecondTabPane extends JPanel implements ActionListener{
JRadioButton randmonArriving = new JRadioButton("random arriving");
JRadioButton historicalArriving = new JRadioButton("historical arriving");
JRadioButton customerArrving = new JRadioButton("customerize arriving");
private RandomData randomData = new RandomDataImpl();
ButtonGroup group = new ButtonGroup();
ArrivingModel aModel = new ArrivingModel();
public SecondTabPane(){ this.setLayout(new BorderLayout());
JPanel north = new JPanel();
north.setLayout(new GridLayout(1,0));
north.add(this.randmonArriving);
north.add(this.historicalArriving);
north.add(this.customerArrving);
this.randmonArriving.addActionListener(this);
this.historicalArriving.addActionListener(this);
this.customerArrving.addActionListener(this);
this.group.add(randmonArriving);
this.group.add(historicalArriving);
this.group.add(customerArrving);
this.add(north,BorderLayout.NORTH);
JPanel south = new JPanel();
this.add(south,BorderLayout.SOUTH);
} @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub }
}
在你Stp.add(new ArrivingViewPanel(model),BorderLayout.CENTER);
之前最好将原先处于BorderLayout.CENTER位置的组件remove掉,并将之setVisible(false);
是不是先设置成false,在remove呢?
如何remove呢?
你是Stp.add(new ArrivingViewPanel(model),BorderLayout.CENTER);这样添加的
可能你无法获得这个对象的引用
所以不好remove那你可以这样改
在类里面先声明一个对象
ArrivingViewPanel newPanel;
然后这样改
public void stateChanged(ChangeEvent arg0) {
ArrivingModel model = new ArrivingModel(Ftp.getSimulationData());
newPanel.setVisible(false);
Stp.remove(newPanel);
newPanel = new ArrivingViewPanel(model);
Stp.add(newPanel,BorderLayout.CENTER);
Stp.updateUI();
break;
}