2016-01-21 134 views
0

我試圖啓動jframe,但是這個Frame根本不打開。它在我運行程序時崩潰。我正在使用鼠標列表和鼠標移動監聽器接口。請提出任何修正.............................Jframe在打開時崩潰

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(); 

    } 

}

+0

崩潰如何?它會產生任何輸出嗎?你打電話過得怎麼樣? – MadProgrammer

回答

2

它不會崩潰。您只需忘了打電話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(); 

    } 

} 
+1

OP在'launchFrame'中做了哪些工作? – MadProgrammer

+0

啊耶。但他從來沒有打過電話^^ – JayC667

+1

啊,是的,但你怎麼知道?他們沒有提供任何關於他們如何使用班級的上下文 – MadProgrammer

1

你不能從你的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(); 
     } 
    }); 
}