L
L
LazariusPaskalius2019-09-27 09:51:23
Java
LazariusPaskalius, 2019-09-27 09:51:23

Java frame won't close, how to fix?

I create a window in which a circle is displayed, but when I try to close the window, nothing happens. It can only be closed through the task manager. How can this misunderstanding be corrected?

import java.awt.*;
class CircleCanvas extends Canvas {
public void paint(Graphics g){
  Dimension d=this.getSize();
  int diam=Math.min(d.width-1,d.height-1)-60;
  g.drawOval(20,20,diam,diam);
  }
}
class MyFrame extends Frame {
  public MyFrame(){
    super("Painting");
    // setBackground(Color.grey);
    setLayout(new GridLayout(3,3));
    add(new CircleCanvas());
    setSize(500,400);
    setVisible(true);
  }
}
public class RunGn{
  public static void main(String[] u) {
    Frame f=new MyFrame();
    
            });
    
  }
}

For some reason, the background color is not set there (it is commented out in the code), if anyone knows how to fix this too, please let me know, I will be grateful for the information

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-09-27
@myjcom

https://www.tutorialspoint.com/awt/awt_window_list...

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class CircleCanvas extends Canvas 
{
  public void paint(Graphics g)
  {
    Dimension d = this.getSize();
    int diam = Math.min(d.width - 1, d.height - 1) - 60;
    g.drawOval(20, 20, diam, diam);
  }
}

class MyFrame extends Frame 
{
  public MyFrame()
  {
    super("Painting");
    setBackground(Color.gray); // GRAY а не GREY
    setLayout(new GridLayout(3, 3));
    add(new CircleCanvas());
    setSize(500, 400);
    setVisible(true);

    // обработчик событий
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent we) 
      {
        System.exit(0);
      }	
    });
  }
}

public class RunGn
{
  public static void main(String[] u) 
  {
    Frame f = new MyFrame();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question