在我们的项目我的队友通知书单选按钮不寻常的行为,他的动作监听器里有SwingUtilites.invokeLater电话。 动作监听的Archetecture不允许避免这种呼叫,因为被设计成启动另一个线程,然后有一个切换回AWT线程。
有没有办法解决这个问题? 我的意思是改变显示组件的状态。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
public class RadioButtonTest {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.add(panel);
ButtonGroup group = new ButtonGroup();
JRadioButton b1 = new JRadioButton("Button 1");
final JRadioButton b2 = new JRadioButton("Button 2");
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Runnable action = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(action);
}
});
group.add(b1);
group.add(b2);
panel.add(b1);
panel.add(b2);
frame.setVisible(true);
}
}