错误 UIDefaults.getUI() 在我的外观中失败

发布于 2024-12-22 22:47:32 字数 3523 浏览 2 评论 0原文

我正在用 Java 开发外观。当我将其应用到我的程序时,我收到此异常/错误:

UIDefaults.getUI() failed: no ComponentUI class for: javax.swing.JButton[,0,0,0x
0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize
=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=null,pa
intBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,
rolloverSelectedIcon=,selectedIcon=,text=test1111,defaultCapable=true]
java.lang.Error
        at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
        at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
        at javax.swing.UIDefaults.getUI(UIDefaults.java:741)
        at javax.swing.UIManager.getUI(UIManager.java:989)
        at javax.swing.JButton.updateUI(JButton.java:128)
        at javax.swing.AbstractButton.init(AbstractButton.java:2149)
        at javax.swing.JButton.<init>(JButton.java:118)
        at javax.swing.JButton.<init>(JButton.java:91)
        at com.mypackage.mylaf.MyLookAndFeelTest.main(MyLookAndFeelTest.java:7)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
        at java.lang.Thread.run(Thread.java:619)

My code:

MyButtonUI class

package com.mypackage.mylaf;

public class MyButtonUI extends BasicButtonUI{
    public static ComponentUI createUI(JComponent c) {
        return new MyButtonUI();
    }

    public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            //paint Button

    }   
}//class

MyLookAndFeel class

package com.mypackage.mylaf;
public class MyLookAndFeel extends BasicLookAndFeel{
    public boolean isSupportedLookAndFeel() {
        return true;
    }

    public boolean isNativeLookAndFeel() {
        return false;
    }

    public String getDescription() {
        return "This is My Look and Feel";
    }

    public String getID() {
        return "MyLookAndFeel";
    }

    public String getName() {
        return "My Look And Feel";
    }


    protected void initClassDefaults(UIDefaults table) {
        super.initClassDefaults(table);

        String myPkg="my.";
        Object[] classes= {"ButtonUI",myPkg+"MyButtonUI"};

        table.putDefaults(classes);
    }//initClassDefaults

    public void initComponentDefaults(UIDefaults table) {
        super.initComponentDefaults(table);

        Object[] defaults= {"Button.font", new Font("Tahoma", Font.PLAIN, 10),
                            "Button.light", new Color(127, 41, 26)
                           };

        table.putDefaults(defaults);
    }//initComponentDefaults
}//MyLookAndFeel

MyLookAndFeelTest class

com.mypackage.mylaf; //1

public class MyLookAndFeelTest{ //2
public static void main(String args[]){//3
try{ UIManager.setLookAndFeel(new MyLookAndFeel()); //4
catch(Exception e){}//5
JFrame f=new JFrame();//6
JButton b=new JButton();//7 , this line is in exception

f.add(b); //8
}

当我设置 Look 和感觉主代码中

UIManager.setLookAndFeel(new MyLookAndFeel());

抛出了上面的异常。我该如何修复它?

I am developing a Look and Feel in Java. When I apply it to my program, I get this exception/error:

UIDefaults.getUI() failed: no ComponentUI class for: javax.swing.JButton[,0,0,0x
0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize
=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=null,pa
intBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,
rolloverSelectedIcon=,selectedIcon=,text=test1111,defaultCapable=true]
java.lang.Error
        at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
        at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
        at javax.swing.UIDefaults.getUI(UIDefaults.java:741)
        at javax.swing.UIManager.getUI(UIManager.java:989)
        at javax.swing.JButton.updateUI(JButton.java:128)
        at javax.swing.AbstractButton.init(AbstractButton.java:2149)
        at javax.swing.JButton.<init>(JButton.java:118)
        at javax.swing.JButton.<init>(JButton.java:91)
        at com.mypackage.mylaf.MyLookAndFeelTest.main(MyLookAndFeelTest.java:7)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
        at java.lang.Thread.run(Thread.java:619)

My code:

MyButtonUI class

package com.mypackage.mylaf;

public class MyButtonUI extends BasicButtonUI{
    public static ComponentUI createUI(JComponent c) {
        return new MyButtonUI();
    }

    public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            //paint Button

    }   
}//class

MyLookAndFeel class

package com.mypackage.mylaf;
public class MyLookAndFeel extends BasicLookAndFeel{
    public boolean isSupportedLookAndFeel() {
        return true;
    }

    public boolean isNativeLookAndFeel() {
        return false;
    }

    public String getDescription() {
        return "This is My Look and Feel";
    }

    public String getID() {
        return "MyLookAndFeel";
    }

    public String getName() {
        return "My Look And Feel";
    }


    protected void initClassDefaults(UIDefaults table) {
        super.initClassDefaults(table);

        String myPkg="my.";
        Object[] classes= {"ButtonUI",myPkg+"MyButtonUI"};

        table.putDefaults(classes);
    }//initClassDefaults

    public void initComponentDefaults(UIDefaults table) {
        super.initComponentDefaults(table);

        Object[] defaults= {"Button.font", new Font("Tahoma", Font.PLAIN, 10),
                            "Button.light", new Color(127, 41, 26)
                           };

        table.putDefaults(defaults);
    }//initComponentDefaults
}//MyLookAndFeel

MyLookAndFeelTest class

com.mypackage.mylaf; //1

public class MyLookAndFeelTest{ //2
public static void main(String args[]){//3
try{ UIManager.setLookAndFeel(new MyLookAndFeel()); //4
catch(Exception e){}//5
JFrame f=new JFrame();//6
JButton b=new JButton();//7 , this line is in exception

f.add(b); //8
}

When I set Look and Feel in main code with

UIManager.setLookAndFeel(new MyLookAndFeel());

the above exception is thrown. How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-12-29 22:47:32

我解决了我的问题,

MyLookAndFeel 类中的 initClassDefaults 方法而不是这一行 String myPkg="my.";
我输入了这段代码:String myPkg="com.mypackage.mylaf" 这是我的包。

i resolved my problem,

in initClassDefaults method in MyLookAndFeel class instead of this line String myPkg="my.";
i put this code:String myPkg="com.mypackage.mylaf" that is my package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文