Jframe 在打开时崩溃

Jframe crashing upon opening

我正在尝试启动 jframe,但此框架根本打不开。它在我 运行 程序时崩溃。我正在使用 mouselistner 和鼠标运动侦听器 interfaces.Please 建议任何修复................................

 public class TwoListener implements MouseMotionListener, MouseListener{

    private JFrame f;
    private TextField tf;

    public TwoListener(){
        f = new JFrame("Two Listeners Example");
        tf = new TextField(30);


    }

    public void launchFrame(){
        Label label= new Label("Click and Drag the mouse");

        // add components to the frame
        f.add(label,BorderLayout.NORTH);
        f.add(tf,BorderLayout.SOUTH);

        //add this object to a listener

        f.addMouseMotionListener(this);
        f.addMouseListener(this);

        //set size of the frame

        f.setSize(300,200);
        f.setVisible(true);
    }


    @Override
    public void mouseEntered(MouseEvent e) {
        String s = "Mouse moved";
        tf.setText(s);          
    }



    @Override
    public void mouseDragged(MouseEvent e) {

            String s = "Mouse Dragging: X " +e.getX() + " Y: " +e.getY();
            tf.setText(s);
    }



    @Override
    public void mouseExited(MouseEvent e) {
        String s = "mouse has exited the building";
        tf.setText(s);
    }



    //unimplemented methods
    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}


    public static void main(String[] args){
        new TwoListener();

    }

}

它不会崩溃。您只是忘记调用 launchFrame(感谢 MadProgrammer)

public class TwoListener implements MouseMotionListener, MouseListener {

    private final JFrame    f;
    private final TextField tf;

    public TwoListener() {
        f = new JFrame("Two Listeners Example");
        tf = new TextField(30);

        launchFrame();
    }

    public void launchFrame() {
        final Label label = new Label("Click and Drag the mouse");

        // add components to the frame
        f.add(label, BorderLayout.NORTH);
        f.add(tf, BorderLayout.SOUTH);

        //add this object to a listener

        f.addMouseMotionListener(this);
        f.addMouseListener(this);

        //set size of the frame

        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // SOMEWHAT IMPORTANT
        f.setSize(300, 200);
        f.setVisible(true);
    }


    @Override public void mouseEntered(final MouseEvent e) {
        final String s = "Mouse moved";
        tf.setText(s);
    }

    @Override public void mouseDragged(final MouseEvent e) {

        final String s = "Mouse Dragging: X " + e.getX() + " Y: " + e.getY();
        tf.setText(s);
    }

    @Override public void mouseExited(final MouseEvent e) {
        final String s = "mouse has exited the building";
        tf.setText(s);
    }

    //unimplemented methods
    @Override public void mouseClicked(final MouseEvent e) {}
    @Override public void mousePressed(final MouseEvent e) {}
    @Override public void mouseReleased(final MouseEvent e) {}
    @Override public void mouseMoved(final MouseEvent e) {}

    public static void main(final String[] args) {
        new TwoListener();

    }

}

您没有从 main 方法中调用 launchFrame 方法,我建议您更改 main 方法使其看起来更像...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            new TwoListener().launchFrame();
        }
    });
}