见下文:
/这是我的主要/
package br.com.general;
public class Main {
public static void main(String[] args) {
Wind w = new Wind();
w.start();
while(true){
//System.out.printf("%b\n", w.button());
if(w.button()){
System.out.printf("xx %b\n", w.button());
}
}
}
}
/这是我的一键式 JFrame 窗口/
package br.com.general;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Wind extends JFrame{
private static final long serialVersionUID = 1L;
Act a = new Act();
public Wind() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton B = new JButton("on");
getContentPane().setLayout(null);
B.setBounds(10, 10, 50, 30);
B.addActionListener(a);
add(B);
setSize(100, 100);
}
public void start() {
setVisible(true);
}
public boolean button(){
return(a.button());
}
public void buttonOk(){
a.zero();
}
}
/*最后这是我的按钮的 ActionListener */
package br.com.general;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Act implements ActionListener {
boolean s;
public void actionPerformed(ActionEvent ae) {
s = true;
}
public boolean button(){
return(s);
}
public void zero(){
s = false;
}
}
如果你运行,你会看到它不起作用,但如果你删除“//”并启用“System.out.printf(”%b\n", w.button() );" 它开始运行......为什么?有人可以帮助我吗?