简体   繁体   English

自定义外观中未显示RadioButton和CheckBox

[英]RadioButton and CheckBox not showing in Custom Look and Feel

I'm creating a custom Look and Feel for a company as my final trainee ship for graduation. 我正在为公司创建自定义外观,作为我毕业的最终实习船。 I've been stuck on this part for a few days now and me and my colleagues can't seem to fix it. 我已经在这部分上停留了几天,而我和我的同事似乎无法修复它。

I'm trying to create a custom radio button an checkbox for the Look and Feel. 我正在尝试为外观创建一个自定义单选按钮和一个复选框。 Now when I want to display the stock ones to the screen / JFrame it won't display. 现在,当我想在屏幕/ JFrame上显示库存商品时,它将不会显示。

Once I activate my Look and Feel the checkboxes and radio buttons dissapear. 激活外观后,复选框和单选按钮就会消失。 But the label of them does display on screen. 但是它们的标签确实显示在屏幕上。 When I create the checkbox / radio outside the look and feel or give a nimbus look and feel to them, it does display. 当我在外观之外创建复选框/单选框或为它们提供灵巧的外观时,它会显示。 So somehow my Look and Feel is breaking things. 因此,我的外观在某种程度上破坏了事情。 Can anyone help? 有人可以帮忙吗?

This is my code for the radio button; 这是我的单选按钮代码;

public class MyRadioBut extends BasicRadioButtonUI {


    public MyRadioBut()
    {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new MyRadioBut();
    }

    @Override
    public void installUI(final JComponent c) {
        super.installUI(c);
    }

    public static MyRadioBut createRadio()
    { 
        MyRadioBut radio = new MyRadioBut();

        return radio;
    }
    private boolean defaults_initialized = false;
    @Override
    public void installDefaults(AbstractButton b) {  
        super.installDefaults(b);

    }
}

Code for checkbox is the same. 复选框的代码是相同的。

Look and feel code; 外观代码;

package iac.lookandfeel;

import java.awt.Font;

import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicLookAndFeel;


public class MyLookAndFeel extends BasicLookAndFeel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    static MyButton btn = new MyButton();
    static MyProgressBar pb = new MyProgressBar();
    static MyLabel lbl = new MyLabel();
    static MyTab tab = new MyTab();
    static MyRadioBut radio = new MyRadioBut();
    static MyComboBox combo = new MyComboBox();

    public MyLookAndFeel() {
        super();

    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "IAC Look and Feel";
    }

    @Override
    public String getID() {
        // TODO Auto-generated method stub
        return "IAC Look and Feel";
    }

    @Override
    public String getDescription() {
        // TODO Auto-generated method stub
        return "IAC's Look And Feel";
    }

    @Override
    public boolean isNativeLookAndFeel() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isSupportedLookAndFeel() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean getSupportsWindowDecorations()
    {
        return true;
    }
    public static void setAllFonts() {
        Font defaultFont = UIManager.getDefaults().getFont("Button.font");
        int defaultSize = defaultFont.getSize(); 
        Font font = new Font("Serif", Font.PLAIN, 50); 

        UIManager.put("Button.font", font);

        UIManager.put("RadioButton.font", font);
        UIManager.put("CheckBox.font", font);

        UIManager.put("ComboBox.font", font);
        UIManager.put("Label.font", font);
        UIManager.put("TabbedPane.font", font);
        UIManager.put("TextField.font", font);
        UIManager.put("PasswordField.font", font);
        UIManager.put("TextArea.font", font);
        UIManager.put("ProgressBar.font", font);

    }

    @Override
    protected void initClassDefaults(UIDefaults table)
    { 
        setAllFonts();
        MyButton.createButton(); 
        MyProgressBar.createBar();
        MyLabel.createLabel();
        MyTab.createTab();
        MyRadioBut.createRadio();
        MyCheckBox.createCheckBox();
        MyComboBox.createCombo();


        super.initClassDefaults(table);
        String IACPackage = "iac.lookandfeel.";
        final String basicPackageName = "javax.swing.plaf.basic.";
        Object[] uiDefaults = {
                "ButtonUI", IACPackage + "MyButton",
                "CheckBoxUI", IACPackage + "MyCheckBox",
                "ColorChooserUI", basicPackageName + "BasicColorChooserUI",
                "FormattedTextFieldUI", basicPackageName + "BasicFormattedTextFieldUI",
                "MenuBarUI", basicPackageName + "BasicMenuBarUI",
                "MenuUI", basicPackageName + "BasicMenuUI",
                "MenuItemUI", basicPackageName + "BasicMenuItemUI",
                "CheckBoxMenuItemUI", basicPackageName + "BasicCheckBoxMenuItemUI",
                "RadioButtonMenuItemUI", basicPackageName + "BasicRadioButtonMenuItemUI",
                "RadioButtonUI", IACPackage + "MyRadioBut",
                "ToggleButtonUI", basicPackageName + "BasicToggleButtonUI",
                "PopupMenuUI", basicPackageName + "BasicPopupMenuUI",
                "ProgressBarUI", IACPackage + "MyProgressBar",
                "ScrollBarUI", basicPackageName + "BasicScrollBarUI",
                "ScrollPaneUI", basicPackageName + "BasicScrollPaneUI",
                "SplitPaneUI", basicPackageName + "BasicSplitPaneUI",
                "SliderUI", basicPackageName + "BasicSliderUI",
                "SeparatorUI", basicPackageName + "BasicSeparatorUI",
                "SpinnerUI", basicPackageName + "BasicSpinnerUI",
                "ToolBarSeparatorUI", basicPackageName + "BasicToolBarSeparatorUI",
                "PopupMenuSeparatorUI", basicPackageName + "BasicPopupMenuSeparatorUI",
                "TabbedPaneUI", IACPackage + "MyTab",
                "TextAreaUI", basicPackageName + "BasicTextAreaUI",
                "TextFieldUI", basicPackageName + "BasicTextFieldUI",
                "PasswordFieldUI", basicPackageName + "BasicPasswordFieldUI",
                "TextPaneUI", basicPackageName + "BasicTextPaneUI",
                "EditorPaneUI", basicPackageName + "BasicEditorPaneUI",
                "TreeUI", basicPackageName + "BasicTreeUI",
                "LabelUI", IACPackage + "MyLabel",
                "ListUI", basicPackageName + "BasicListUI",
                "ToolBarUI", basicPackageName + "BasicToolBarUI",
                "ToolTipUI", basicPackageName + "BasicToolTipUI",
                "ComboBoxUI", IACPackage + "MyComboBox",
                "TableUI", basicPackageName + "BasicTableUI",
                "TableHeaderUI", basicPackageName + "BasicTableHeaderUI",
                "InternalFrameUI", basicPackageName + "BasicInternalFrameUI",
                "DesktopPaneUI", basicPackageName + "BasicDesktopPaneUI",
                "DesktopIconUI", basicPackageName + "BasicDesktopIconUI",
                "FileChooserUI", basicPackageName + "BasicFileChooserUI",
                "OptionPaneUI", basicPackageName + "BasicOptionPaneUI",
                "PanelUI", basicPackageName + "BasicPanelUI",
                "ViewportUI", basicPackageName + "BasicViewportUI",
                "RootPaneUI", basicPackageName + "BasicRootPaneUI",
        };
        //   UIManager.put(MyLookAndFeel.UI_CLASS_ID,  MyButton.class.getName());
        table.putDefaults(uiDefaults);
    }
}

IACPackage is my own package which I use in all components. IACPackage是我自己的程序包,可在所有组件中使用。

Hope one of you guys got an idea. 希望你们中的一个有一个主意。

I have a test gui which I test the look and feel on: I create the checkbox and radio like this; 我有一个测试GUI,可以测试外观:我像这样创建复选框和单选按钮;

JCheckBox CBox = new JCheckBox("Checkbox"); JCheckBox CBox =新的JCheckBox(“ Checkbox”); JRadioButton Radio = new JRadioButton("Radio"); JRadioButton Radio =新的JRadioButton(“ Radio”);

It does display the string but not the icons. 它确实显示字符串,但不显示图标。

Fixed it myself. 自己修复。

Icon defaultIcon;  
defaultIcon = UIManager.getIcon("CheckBox.icon");
UIManager.put("CheckBox.icon", defaultIcon);

I used this to get the default icon from UI manager and after that I put it back with the put statement. 我用它来从UI管理器中获取默认图标,然后将其与put语句放回去。 Quite stupid solution but it works! 愚蠢的解决方案,但它可以工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM