到目前为止,我设法掌握它的工作原理我已经让每个模块都工作没问题了:
现在我的困境是你如何在这个设计模式中实现JProgressBar。问题是我像这样画我的JProgressBar:
bar = new JProgressBar(); bar.setString(消耗+" /" +总计); bar.setStringPainted(真);
无论如何,无论如何我选择将JProgressBar实现到这个设计模式中,如果产生某种问题,或者只是更新消耗的值,但不会绘制JProgressBar的消耗部分。这是我缩短的代码:
查看>>更新方法:
public void update(Observable servlet, Object argument)
{
if(argument.equals("cap-cons-add")) {
cap_bar.setValue(model.getConsumedCap());
}
}
控制器>>
class moduleBrowserAction implements MouseListener
{
public void mouseClicked(MouseEvent arg0)
{
if(arg0.getClickCount() == 2 && model.getTower() != null)
{
String module = view.getModuleTree().getLastSelectedPathComponent().toString();
model.addModule(module);
model.incModulesCount(1);
model.incConsumedCap(5000);
}
}
public void mouseEntered(MouseEvent arg0){}
public void mouseExited(MouseEvent arg0){}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
}
class towerBrowseAction implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
String tower = view.getTowerBrowser().getSelectedItem().toString();
String[] data = dataStream.serializeData(tower);
Integer s = Integer.valueOf(data[3]).intValue();
Integer a = Integer.valueOf(data[4]).intValue();
Double em = Double.valueOf(data[5]).doubleValue();
Double th = Double.valueOf(data[6]).doubleValue();
Double kn = Double.valueOf(data[7]).doubleValue();
Double ex = Double.valueOf(data[8]).doubleValue();
Integer cap = Integer.valueOf(data[1]).intValue();
if(model.getTower() == null && model.validateTower(tower) == true)
{
model.setTower(tower);
model.setCapacitorBar(view.getCapBar(), cap);
model.setHardners(em, th, kn, ex);
model.setSieldAndArmor(s, a);
} else {
integrity.towerSelectedWarning();
}
}
}
我的模特:
private int cap_total = 0;
private int cap_consumed = 0;
public void incConsumedCap(int amount)
{
this.cap_consumed += amount;
this.setChanged();
this.notifyObservers("cap-cons-add");
}
public int getConsumedCap()
{
return this.cap_consumed;
}
public void setCapacitorBar(JProgressBar bar, int max)
{
this.cap_total = max;
bar.setMaximum(max);
bar.setString(cap_consumed + " / " + max);
bar.setStringPainted(true);
}
因此应用程序的工作方式如下:并且从JComboBox中选择元素,它会更改少量标签并通过调用setCapacitorBar()来设置JProgressBar。现在,当将附加模块添加到主元素时,函数model.incConsumedCap(5000);增加消耗量并设置setChaned()标志并在update方法中调用notifyObservers(),直接设置所提到的JProgressBar设置消耗值,但不起作用。