Есть код:
Код:
package javaapplication1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
interface Action{
public void exec(MouseEvent e);
}
class DefAction implements Action{
public void exec(MouseEvent e){
}
}
class MyButton implements MouseListener{
JButton btn;
Component btncomp;
String caption;
Action def,onclick,onpressed,onleave,onenter,onrelease;
MyButton(JFrame frm,String caption){
btn=new JButton(caption);
btncomp=frm.add(btn);
this.caption=caption;
btn.addMouseListener(this);
onclick=new DefAction();
onpressed=new DefAction();
onleave=new DefAction();
onenter=new DefAction();
};
public void SetSize(int w, int h){
btncomp.setSize(w, h);
};
public void SetPos(int x, int y){
btncomp.setLocation(x, y);
};
public void SetAction(String actiontype, Action act){
if(actiontype.equals("click")){
onclick=act;
}
if(actiontype.equals("press")){
onpressed=act;
}
}
public void mouseClicked(MouseEvent e) {
if(e.getComponent().equals(btncomp)){
onclick.exec(e);
}
}
public void mousePressed(MouseEvent e) {
if(e.getComponent().equals(btncomp)) onpressed.exec(e) ;
}
public void mouseReleased(MouseEvent e) {
if(e.getComponent().equals(btncomp)) onrelease.exec(e);
}
public void mouseEntered(MouseEvent e) {
if(e.getComponent().equals(btncomp)) onenter.exec(e);
}
public void mouseExited(MouseEvent e) {
if(e.getComponent().equals(btncomp)) onleave.exec(e);
}
}
class MainWindow implements ActionListener{
void Create(){
JFrame jfrm=new JFrame("qwerty");
jfrm.setSize(500, 500);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyButton btn;
btn=new MyButton(jfrm,"OneButton");
btn.SetSize(100, 20);
btn.SetPos(10, 10);
btn.SetAction("click", new Action(){
public void exec(MouseEvent e){
JOptionPane.showMessageDialog(null, "qwerty");
}
});
JPanel jpnl=new JPanel();
jfrm.getContentPane().add(jpnl);
jfrm.setResizable(false);
jfrm.setLocation(300, 300);
jfrm.setVisible(true);
}
void Run(){
Runnable rnbl=new Runnable(){
public void run(){
Create();
}
};
SwingUtilities.invokeLater(rnbl);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("1st button")) JOptionPane.showMessageDialog(null, "qwerty");
}
}
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e) { }
MainWindow window=new MainWindow();
window.Run();
}
}
Компилируется и работает нормально, но при исполении события mouseClicked в кнопке выдает такой меседж:
Код:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication1.MyButton.mouseReleased(Main.java:61)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
После изучения ситуации я понял что дело в:
Код:
btn.SetAction("click", new Action(){
public void exec(MouseEvent e){
JOptionPane.showMessageDialog(null, "qwerty");
}
});
собственно в замене DefAction на new Action(). Как поправить данную ошибку?