2012-10-03 115 views
1

嗨,大家好我正在用java编写一个小程序,这是我第一次尝试使用界面/图片。无法在java中关闭框架?

它使我的框架,但是当我点击关闭按钮X,它不关闭它只是把它当作没有发生......任何想法?

class Graph extends Canvas{ 

    public Graph(){ 
     setSize(200, 200); 
     setBackground(Color.white); 
    } 

    public static void main(String[] args){ 

     Graph gr = new Graph(); 

     Frame aFrame = new Frame(); 
     aFrame.setSize(300, 300);  
     aFrame.add(gr);  
     aFrame.setVisible(true); 

    } 
+0

你需要setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) – Shark

+0

我也建议看javax.swing.WindowConstants.DISPOSE_ON_CLOSE。 – JIV

回答

5

是java.awt.Frame?我认为你需要明确添加处理程序,以便:

frame.addWindowListener(new WindowAdapter(){ 
    public void windowClosing(WindowEvent we){ 
    System.exit(0); 
    } 
} 

我用this源等等。

如果它摆动它会是这样的jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

+0

它是一个java.awt.Frame – Butterflycode

1

添加aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

+0

这是如何工作的?... 'aFrame'是一个'awt框架'而不是'Jframe'。 –

0
class Graph extends Canvas{ 

public Graph(){ 
    setSize(200, 200); 
    setBackground(Color.white); 
    addWindowListener( 
       new java.awt.event.WindowAdapter() { 
       public void windowClosing(java.awt.event.WindowEvent e) { 
        System.out.println("Closing window!"); 
        dispose() ; 
        System.exit(0); 
       } 
       } 
      ); 
} 

public static void main(String[] args){ 

    Graph gr = new Graph(); 

    Frame aFrame = new Frame(); 
    aFrame.setSize(300, 300);  
    aFrame.add(gr);  
    aFrame.setVisible(true); 

}