簡體   English   中英

JRadioButton 選擇不會顯示在 GUI 上,直到在 Windows LaF 中可見

[英]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.AnimationController類中,有一個VISTA_ANIMATION_DISABLED字段被初始化為swing.disablevistaanimation屬性。 該字段確定是否paintSkin方法調用skin.paintSkinRaw如果動畫被禁用,否則開始進入動畫方法。

它適用於我的帶有 Java 8 (jdk1.8.0_65) 的 Windows 8.1 筆記本電腦,因此它的效果似乎不僅限於 Windows Vista。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM