布尔表达式应该在运行时在计时器中进行评估,以响应 swing gui 中的变化
Boolean expression should be evaluated at runtime in a timer, reacting to changes in the swing gui
这是我正在尝试做的事情:我有一个带有两个 JFrames 的 swing gui。第一个有一个 JCheckBox,第二个显示一些文本。第二个还有一个 javax.swing.Timer 正在等待第一帧中的复选框被单击。单击后,将显示更多文本。如果我只有一个条件(单击复选框)并且条件直接在 if 语句中,它就可以工作,如下所示:
javax.swing.Timer timer = new javax.swing.Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ( otherGUI.jCheckBox.isSelected() ){
//add some text to second JFrame
timer.stop();
}
}
});
现在转折点:这不仅应该发生一次,而且应该发生多次。有一个计时器 ArrayList,每个计时器都有自己的文本和条件,一个接一个地开始。我的问题是:如果我将条件作为字符串存储在 ArrayList 中,它们似乎在程序开始时被评估一次,所以即使我单击复选框,上面的条件仍然为假。这是我的实际代码:
SomeGUI gui = new SomeGUI();
ArrayList<javax.swing.Timer> timer = new ArrayList<javax.swing.Timer>();
ArrayList<String> text = new ArrayList<String>();
ArrayList<String> cond = new ArrayList<String>();
text.add("some text");
cond.add("gui.jCheckBox.isSelected()");
text.add("some more text");
cond.add(new Condition("true"));
//etc.
for ( int i = 0; i < text.size() - 1; i++ ){
int j = i;//not sure why this trick is necessary. i doesn't work later on
timer.add( new javax.swing.Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean bool = false;
try{
bool = Boolean.parseBoolean( cond.get(j) );
}
catch(Exception ex){}
if ( bool ){
addText(p, text.get(j+1));
timer.get(j).stop();
timer.get(j+1).start();
}
}
}));
}
timer.get(0).start();
我已经尝试了一个 ArrayList<Boolean>
条件来达到同样的效果。上面的代码只是代表了我目前的试错状态。
我希望我能弄清楚我想要达到的目标。那么我怎样才能将布尔表达式存储在 list/array 中,并在运行时一次又一次地在 if 语句中对它们求值,而不仅仅是在程序启动时求值一次?
Java中没有简单的"evaluation"字符串。有可能,但真的没有"java style"。参见 Convert String to Code
另一种选择是您的字符串表示方法名称(存在于众所周知的对象上);然后您可以使用反射根据提供的字符串调用该方法。但是,也很丑。
这是我正在尝试做的事情:我有一个带有两个 JFrames 的 swing gui。第一个有一个 JCheckBox,第二个显示一些文本。第二个还有一个 javax.swing.Timer 正在等待第一帧中的复选框被单击。单击后,将显示更多文本。如果我只有一个条件(单击复选框)并且条件直接在 if 语句中,它就可以工作,如下所示:
javax.swing.Timer timer = new javax.swing.Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ( otherGUI.jCheckBox.isSelected() ){
//add some text to second JFrame
timer.stop();
}
}
});
现在转折点:这不仅应该发生一次,而且应该发生多次。有一个计时器 ArrayList,每个计时器都有自己的文本和条件,一个接一个地开始。我的问题是:如果我将条件作为字符串存储在 ArrayList 中,它们似乎在程序开始时被评估一次,所以即使我单击复选框,上面的条件仍然为假。这是我的实际代码:
SomeGUI gui = new SomeGUI();
ArrayList<javax.swing.Timer> timer = new ArrayList<javax.swing.Timer>();
ArrayList<String> text = new ArrayList<String>();
ArrayList<String> cond = new ArrayList<String>();
text.add("some text");
cond.add("gui.jCheckBox.isSelected()");
text.add("some more text");
cond.add(new Condition("true"));
//etc.
for ( int i = 0; i < text.size() - 1; i++ ){
int j = i;//not sure why this trick is necessary. i doesn't work later on
timer.add( new javax.swing.Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean bool = false;
try{
bool = Boolean.parseBoolean( cond.get(j) );
}
catch(Exception ex){}
if ( bool ){
addText(p, text.get(j+1));
timer.get(j).stop();
timer.get(j+1).start();
}
}
}));
}
timer.get(0).start();
我已经尝试了一个 ArrayList<Boolean>
条件来达到同样的效果。上面的代码只是代表了我目前的试错状态。
我希望我能弄清楚我想要达到的目标。那么我怎样才能将布尔表达式存储在 list/array 中,并在运行时一次又一次地在 if 语句中对它们求值,而不仅仅是在程序启动时求值一次?
Java中没有简单的"evaluation"字符串。有可能,但真的没有"java style"。参见 Convert String to Code
另一种选择是您的字符串表示方法名称(存在于众所周知的对象上);然后您可以使用反射根据提供的字符串调用该方法。但是,也很丑。