【问题标题】:Nullpointerexception only with LaF NimbusNullpointerexception 仅适用于 LaF Nimbus
【发布时间】:2012-05-06 14:29:15
【问题描述】:

当我使用 Nimbus 外观和感觉时,我会在 addPropertyChangeListener 收到 Nullpointerexception。当我使用 Linux 标准 LAF(金属)时,一切正常。

这是一个模拟问题的小测试项目!我有一个扩展 JPanel 并将内容提供给框架的类。框架上的 Finsih 按钮只有在满足某些条件时才会启用(在这种情况下是 button2 被按下)。

这是主类:

public class Main extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -3120562776241721109L;
    private JPanel contentPane;
    private JButton button;
    private PropertyChangeListener changeListener;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {


        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            System.out.println(UIManager.getLookAndFeel());
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        button = new JButton("BUTTON");
        button.setEnabled(false);
        changeListener = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("state")) {
                    button.setEnabled((boolean) evt.getNewValue());
                }
            }
        };
        contentPane.add(button, BorderLayout.SOUTH);
        Panel panel = new Panel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.addPropertyChangeListener(changeListener);
    }

这是面板类:

public class Panel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = -5036784576513445229L;
    private PropertyChangeSupport changes;
    private boolean state;
    /**
     * Create the panel.
     */
    public Panel() {

        this.changes = new PropertyChangeSupport(this);
        JButton button = new JButton("BUTTON2");
        add(button);
        state = false;
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                state = !state;
                changes.firePropertyChange("state", null, state);
            }
        });

    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changes.addPropertyChangeListener(listener);
    }
}

正如之前所说,它与 Metal LAF 一起工作没有任何问题

【问题讨论】:

  • 有堆栈跟踪,但代码在哪里?
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing look-and-feel nimbus


【解决方案1】:

看起来您覆盖了 addPropertyChangeListeners 并将侦听器存储在名为“更改”的列表中。由于 UI 是在执行其余构造函数之前安装的,因此您的更改变量尚未初始化。

在将侦听器添加到您的列表之前添加一个空测试,并在需要时实例化该列表(并在您的构造函数中执行相同的操作)。我不记得初始化变量 inline 是否可行(而且我这里没有 JDK 来测试并告诉你)。

或者考虑不覆盖该方法。你当初为什么这么做?看起来没有必要(但没有代码我无法准确判断)。

【讨论】:

  • 如果我没记错的话,内联初始化发生在调用超级构造函数之后
  • 感谢您的回答!我不明白 Nimbus LAF 和 Metal LAF 是如何发生这种情况的。
  • 一些 L&F 似乎注册了一个 PropertyChangeListener,而另一些可能不需要这样做。就是这样
  • 谢谢...这是问题所在!我创建了一个小测试项目,我尝试在初始化 GUI 之前设置 Nimbus,但失败了!当我在 GUI 初始化后设置 LAF 时,它可以工作!
  • @AndreasFreitag 正确的解决方案是摆脱您的覆盖和您的更改变量。当您需要触发属性更改事件时,请使用:Panel.this.firePropertyChange("state", null, state);(来自按钮 ActionListener)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2011-12-14
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多