首页javajframeJava Swing - 如何使用代码关闭JFrame

Java Swing - 如何使用代码关闭JFrame

我们想知道如何使用代码关闭JFrame。
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {
    JButton close = new JButton("Close me programmatically");
    final JFrame f = new JFrame("Close Me");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(close);
    close.addActionListener(e -> {
      f.dispose();
    });
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
  }
}