首先,我是Java新手:/
我一直试图在我自己解决这个问题大约2天但是无法绕过它问题是我正在尝试实现变量监听器。我已经尝试过没有成功实现Observer和Observable到我的项目但是我最好没有成功通过将代码的一些元素包装到while循环中但是很好地失败了。
这是我的类的任何方式,如果你看一下我在构造函数之后定义了一些全局变量我需要监听所有这些全局变量的变化,如果一个变化我想执行一个方法。
我被告知JavaFX有可以监听变量的方法可以有人确认这一点。 无论如何,提前感谢您的帮助。
public class Tower_Controller {
public Tower_Controller() {
}
//Global variables
String isSelected = null;
int hasModules = 0;
int cap_s = 0;
int cpu_s = 0;
int cap = 0;
int cpu = 0;
int shield = 0;
int armor = 0;
double em = 00.00;
double th = 00.00;
double ki = 00.00;
double ex = 00.00;
public void invoke() {
Invoke_GUI runnable = new Invoke_GUI();
final JLabel tower_name = runnable.tower_name;
final JComboBox tower_select = runnable.tower_select;
final JTree module_browser = runnable.module_browser;
final JTree selected_modules = runnable.selected_modules;
final JProgressBar cap_bar = runnable.cap_bar;
final JProgressBar cpu_bar = runnable.cpu_bar;
final JLabel em_res = runnable.em;
final JLabel th_res = runnable.thermic;
final JLabel ki_res = runnable.kinetic;
final JLabel ex_res = runnable.explosive;
tower_select.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (isSelected != null) {
Events evt = new Events();
evt.towerSelected(isSelected);
} else {
tower_name.setText(tower_select.getSelectedItem().toString());
isSelected = tower_name.toString();
}
}
});
removeTower(tower_name);
runnable.setVisible(true);
}
public void updateValues(final JProgressBar cap_bar, final JProgressBar cpu_bar, final JLabel em_res,
final JLabel th_res, final JLabel ki_res, final JLabel ex_res){
cap_bar.setMaximum(cap);
cap_bar.setString(cap_s + " / " + cap);
cap_bar.setStringPainted(true);
cpu_bar.setMaximum(cpu);
cpu_bar.setString(cpu_s + " / " + cpu);
cpu_bar.setStringPainted(true);
String em_v = String.valueOf(em);
em_res.setText(em_v);
String th_v = String.valueOf(th);
th_res.setText(th_v);
String ki_v = String.valueOf(ki);
ki_res.setText(ki_v);
String ex_v = String.valueOf(ex);
ex_res.setText(ex_v);
}
public void updateList(final ArrayList<String> nodes, final JTree selected_modules) {
DefaultMutableTreeNode nod = new DefaultMutableTreeNode();
for (int i = 0; i < nodes.size(); i++) {
nod.add(new DefaultMutableTreeNode(nodes.get(i)));
}
selected_modules.setModel(new DefaultTreeModel(nod));
}
public void removeTower(final JLabel tower_name) {
tower_name.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (hasModules == 1 & isSelected != null) {
Events evt = new Events();
evt.towerHasModules();
} else if (isSelected == null) {
} else {
tower_name.setText("No Control Tower selected");
isSelected = null;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
public JLabel setTowerName(JLabel a, String name) {
a.setText(name);
return a;
}
}
答案 0 :(得分:12)
变更通知的一般程序如下:
setX()
,其中X
是变量名称),另一个检索其值({{1} })getX()
方法中,循环播放setX()
上的notifyObserver()
。你去吧!现在,每次更改变量时,都会通知已注册的Observer
。此解决方案的关键部分是变量必须是私有的,因此没有代码可以在不经过Observer
方法的情况下设置其值。