우리 팀원은 라디오 버튼에 대한 비정상적인 동작을 알았습니다. 내부 리스너에는 SwingUtilites.invokeLater 호출이 있습니다. 액션 청취자의 구조에서는, 다른 thread를 기동하도록 (듯이) 설계되어 있기 (위해) 때문에,이 호출을 피할 수가 없게되어, AWT thread 에의 전환이 다시 생깁니다.라디오 버튼으로 상태가 즉시 바뀌지 않습니다.
해결 방법이 있습니까? 표시되는 구성 요소의 상태를 변경하는 것을 의미합니다.
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);
}
}