我为公司创造了一个定制的外观,作为我毕业的最终实习船。我已经被困在这个部分几天了,而我和我的同事似乎无法修复它。
我尝试为自定义单选按钮创建一个外观复选框。现在,当我想在屏幕/ JFrame上显示库存时,它不会显示。
一旦激活了我的外观,复选框和单选按钮就会消失。但它们的标签确实显示在屏幕上。当我在外观之外创建复选框/收音机或为它们提供灵气的外观和感觉时,它会显示。所以不知怎的,我的外观和感觉正在破坏事物。有人可以帮忙吗?
这是我的单选按钮代码;
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);
}
}
复选框的代码是相同的。
外观和感觉代码;
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是我自己的包,我在所有组件中使用它。
希望你们其中一个人有个主意。我有一个测试gui,我测试外观和感觉:我创建了这样的复选框和收音机;
JCheckBox CBox =新的JCheckBox(" Checkbox"); JRadioButton Radio =新的JRadioButton(" Radio");
它确实显示字符串,但不显示图标。
答案 0 :(得分:0)
自己修正了。
Icon defaultIcon;
defaultIcon = UIManager.getIcon("CheckBox.icon");
UIManager.put("CheckBox.icon", defaultIcon);
我用它从UI管理器获取默认图标,之后我用put语句将其放回去。相当愚蠢的解决方案,但它的工作原理!