1

你好我有一个用户可以改变外观的界面。这是界面:

http://www.hostingpics.net/viewer.php?id=579250themesApp.jpg

以及每个按钮的代码:

//The JDesktop Frame contains all the Internal Frames
JDesktop p = new JDesktop();
try {
            UIManager.setLookAndFeel( new javax.swing.plaf.metal.MetalLookAndFeel());

        SwingUtilities.updateComponentTreeUI(p);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(paramApparence.class.getName()).log(Level.SEVERE, null, ex);
        }

对于每个按钮,我只更改外观和感觉(金属/雨云等)

当我单击按钮更改外观时,它给了我:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

这是完整的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:217)
    at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:286)
    at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1012)
    at javax.swing.JComponent.getFontMetrics(JComponent.java:1624)
    at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:125)
    at javax.swing.plaf.synth.SynthMenuItemLayoutHelper.<init>(SynthMenuItemLayoutHelper.java:119)
    at javax.swing.plaf.synth.SynthGraphicsUtils.paint(SynthGraphicsUtils.java:498)
    at javax.swing.plaf.synth.SynthMenuUI.paint(SynthMenuUI.java:283)
    at javax.swing.plaf.synth.SynthMenuUI.update(SynthMenuUI.java:248)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:217)
    at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:286)
    at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1012)
    at javax.swing.JComponent.getFontMetrics(JComponent.java:1624)
    at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:126)
    at javax.swing.plaf.synth.SynthMenuItemLayoutHelper.<init>(SynthMenuItemLayoutHelper.java:119)
    at javax.swing.plaf.synth.SynthGraphicsUtils.paint(SynthGraphicsUtils.java:498)
    at javax.swing.plaf.synth.SynthMenuItemUI.paint(SynthMenuItemUI.java:283)
    at javax.swing.plaf.synth.SynthMenuItemUI.update(SynthMenuItemUI.java:247)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:217)
    at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:286)
    at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1012)
    at javax.swing.JComponent.getFontMetrics(JComponent.java:1624)
    at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:126)
    at javax.swing.plaf.synth.SynthMenuItemLayoutHelper.<init>(SynthMenuItemLayoutHelper.java:119)
    at javax.swing.plaf.synth.SynthGraphicsUtils.paint(SynthGraphicsUtils.java:498)
    at javax.swing.plaf.synth.SynthMenuItemUI.paint(SynthMenuItemUI.java:283)
    at javax.swing.plaf.synth.SynthMenuItemUI.update(SynthMenuItemUI.java:247)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
4

1 回答 1

1

例如(正确方法之一)

在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述

默认情况下需要JFrame.pack(),而不是按原样使用另一个woodoos setVisible(false) and then true,或者更改DecorationsTypeJFrame

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class TestInternalFrame {

    private JFrame frame = new JFrame("Test");

    public TestInternalFrame() {
        final JInternalFrame internal = new JInternalFrame("test");
        final JInternalFrame hidden = new JInternalFrame("test");
        hidden.setBounds(1000, 1000, 1, 1);
        hidden.setVisible(true);
        internal.setVisible(true);
        internal.setBounds(0, 0, 100, 100);
        JDesktopPane pane = new JDesktopPane();
        pane.add(internal);
        pane.add(hidden);
        pane.setPreferredSize(new Dimension(10000, 10000));
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new JScrollPane(pane,
                JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
        for (int i = 0; i < info.length; i++) {
            model.addElement(info[i].getName());
        }
        final JComboBox box = new JComboBox(model);
        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if (box.getSelectedItem().equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            UIManager.getLookAndFeel().uninitialize();
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(frame);
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(TestInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(TestInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(TestInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(TestInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        frame.add(box, BorderLayout.NORTH);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestInternalFrame tif = new TestInternalFrame();
            }
        });
    }
}
于 2013-03-27T13:47:41.600 回答