当我使用 Nimbus Look and Feel 时,Nullpointerexception
我得到了一个结果。addPropertyChangeListener
当我使用 Linux 标准 LAF(金属)时,一切正常。
这是一个模拟问题的小测试项目!我有一个扩展 JPanel 并将内容提供给框架的类。框架上的 Finsih 按钮只有在满足某些条件时才会启用(在这种情况下,按钮 2 被按下)。
这是主类:
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 一起工作没有任何问题