1

Hi guys I am writing a little program in java and it's my first try at anything with an interface/picture.

It makes the frame for me but when I click the close button X, it doesn't close it just treats it like nothing happened...any ideas?

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

    }
4

3 に答える 3

6

それはjava.awt.Frameですか?そのためのハンドラーを明示的に追加する必要があると思います。

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

私はそのためにこのソースを使用しました。

スイングだったらjframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

于 2012-10-03T11:44:05.520 に答える
1

add aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

于 2012-10-03T11:48:41.100 に答える
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);

}
于 2015-02-22T20:26:51.913 に答える