在 Windows LaF 中可见之前,JRadioButton 选择不会显示在 GUI 上

JRadioButton selection doesn't show on GUI until visible in Windows LaF

我目前正在做一个项目,该项目需要在更新正在查看的记录时更改 JRadioButton 的状态。

我们有一些客户向我们抱怨说,当记录更改时,如果 JRadioButton 不在屏幕上,则在屏幕显示之前不会更新。此行为似乎是使用 Windows 外观的结果,因为未设置时似乎不会发生。

下面的代码示例演示了这一点。 默认选择的 JRadioButton 是 'Cat',因此通过选择 'Dog' JButton 然后将选项卡更改为 'Question',您可以看到 JRadioButton 转换发生。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * Example of JRadioButton not updating until it's parent panel becomes visible.
 */
public class RadioButtonExample extends JPanel implements ActionListener {
    
    public static final String CAT  = "Cat";
    public static final String DOG  = "Dog";
    
    private final JRadioButton radCat;
    private final JRadioButton radDog;
    private final ButtonGroup grpAnimal;
    
    public RadioButtonExample() {
        super(new BorderLayout());
        
        JLabel lblQuestion = new JLabel("Are you a cat or dog person?");
        
        radCat = new JRadioButton(CAT);
        radCat.setActionCommand(CAT);
        radCat.setSelected(true);
        
        radDog = new JRadioButton(DOG);
        radDog.setActionCommand(DOG);
        
        grpAnimal = new ButtonGroup();
        grpAnimal.add(radCat);
        grpAnimal.add(radDog);
        
        JPanel pnlQuestion = new JPanel(new GridLayout(0, 1));
        pnlQuestion.add(lblQuestion);
        pnlQuestion.add(radCat);
        pnlQuestion.add(radDog);
        
        JButton btnSetCat = new JButton(CAT);
        btnSetCat.setActionCommand(CAT);
        btnSetCat.addActionListener(this);
        
        JButton btnSetDog = new JButton(DOG);
        btnSetDog.setActionCommand(DOG);
        btnSetDog.addActionListener(this);
        
        JPanel pnlButtons = new JPanel(new GridLayout(0, 1));
        pnlButtons.add(new JLabel("Update your choice of animal"));
        pnlButtons.add(btnSetCat);
        pnlButtons.add(btnSetDog);
        
        JTabbedPane tabPane = new JTabbedPane();
        tabPane.addTab("Buttons", pnlButtons);
        tabPane.addTab("Question", pnlQuestion);
        
        add(tabPane, BorderLayout.LINE_START);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }
    
    public void actionPerformed(ActionEvent evt) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                grpAnimal.clearSelection();
                if (CAT.equals(evt.getActionCommand())) {
                    grpAnimal.setSelected(radCat.getModel(), true);
                }
                else if (DOG.equals(evt.getActionCommand())) {
                    grpAnimal.setSelected(radDog.getModel(), true);
                }
            }
        });
        
    }
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("RadioButtonExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        JComponent newContentPane = new RadioButtonExample();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        // Comment out the line below to run using standard L&F
        setLookAndFeel();
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    private static void setLookAndFeel() {
        try {
            // Set Windows L&F
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } 
        catch (Exception e) {
           // handle exception
        }
    }
}

有没有办法阻止这种行为,甚至加快它的速度以减少它对我们的用户的影响?

您可以通过指定 swing.disablevistaanimation Java 系统来禁用动画 属性:

java -Dswing.disablevistaanimation="true" your-cool-application.jar

com.sun.java.swing.plaf.windows.AnimationControllerclass中,有一个VISTA_ANIMATION_DISABLED字段初始化为swing.disablevistaanimation属性。此字段确定 paintSkin 方法是否调用 skin.paintSkinRaw 方法,如果动画被禁用或开始进入动画。

它适用于我的 Windows 8.1 笔记本电脑 Java 8 (jdk1.8.0_65),所以它的效果似乎并不局限于 Windows仅限 Vista。