Mixing look and feel

你离开我真会死。 提交于 2019-12-20 03:14:08

问题


So far I have this

public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            UIManager.getBorder("design");//change look and feel here?
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Nimbus isn't available");
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                design GUI = new design();
                GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GUI.setVisible(true);
            }
        });
    }

I'm trying to make the main look and feel nimbus, but change the titled border to windows.

My border is this:

contentPanel.setBorder(new TitledBorder("Downloader"));

Is it possible to do? If it is can someone point me where to look? I'm so confused right now. :\

Thanks.


回答1:


I've done it. You have to create parts of the UI, and then call UIManager.setLookAndFeel() to change the look & feel, and then create the other parts. It's more like a hack.




回答2:


When a Component is created, the current LookAndFeel will be used to display that Component.




回答3:


I know I am late but I think someone might be use this it is kind of hack that I use to put multi look and feel to the app: put this the look and feel chooser before initiating the item (before writing = new ...)

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Windows".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
    | UnsupportedLookAndFeelException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

and then return the UIManager look and feel to the look and feel that it was on before you do this after it as in the example below:

JButton test;
try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Windows".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
    | UnsupportedLookAndFeelException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
test = new JButton();
try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Mwtal".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
    | UnsupportedLookAndFeelException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Like this only the button test will have the look and feel of the windows and the rest will have look and feel of Metal.

Hope this hack help someone.



来源:https://stackoverflow.com/questions/4707716/mixing-look-and-feel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!