Loo*_*nin 0 java user-interface swing if-statement awt
我有一个开始按钮和一个弹出菜单选项,可以做同样的事情.是否可以在同一个if语句中测试两个按钮,或者我是否必须为它们编写两个单独的if语句?
我想做这样的事情:
public void actionPerformed(ActionEvent e){
// The start button and the popup start menu option
if (e.getSource() == start)||(e.getSource() == startPopup){
new Thread() {
@Override
public void run() {
GreenhouseControls.startMeUp();
}
}.start();
Run Code Online (Sandbox Code Playgroud)
这里唯一的问题是括号.一个if声明的形式为:
if (condition)
body
Run Code Online (Sandbox Code Playgroud)
目前你已经有了
if (condition1) || (condition2)
body
Run Code Online (Sandbox Code Playgroud)
这是无效的.你只想要:
if (e.getSource() == start || e.getSource() == startPopup)
Run Code Online (Sandbox Code Playgroud)
或者可能提取出共性:
Object source = e.getSource();
if (source == start || source == startPopup)
Run Code Online (Sandbox Code Playgroud)
如果您真的想要,可以添加额外的括号:
Object source = e.getSource();
if ((source == start) || (source == startPopup))
Run Code Online (Sandbox Code Playgroud)
......但是括号中只有一个整体表达式.
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |