user3022819
user3022819

Reputation: 1

Activate the CLOSE button

After clicking the run button, an interface prompt up. The "Press me...." button that I created is working,however the CLOSE button at the top right corner is not working. The only way to close is to click the STOP ICON at the console area, which I felt troublesome. How can I use the close button? Am I short of any codes? or am I doing it wrongly?

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;

public class TestButton1 {
    private Frame F;
    private Button B;

    public TestButton1() {
        F = new Frame("Welcome!");
        B = new Button(
                "Press Me! \nSo that, you can genarate OUTPUT result at the CONSOLE");
        B.setActionCommand("ButtonPressed");
    }

    public void launchFrame() {
        B.addActionListener(new mainclass());
        F.add(B, BorderLayout.CENTER);
        F.pack();
        F.setVisible(true);
    }

    public static void main(String[] args) {
        TestButton1 guiApp = new TestButton1();
        guiApp.launchFrame();
    }
}

Upvotes: 0

Views: 802

Answers (2)

camickr
camickr

Reputation: 324118

When you create your frame you should use:

JFrame frame = new JFrame(...);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // or exit on close

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39457

You may add this code

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

before

F.add(B, BorderLayout.CENTER);

Upvotes: 0

Related Questions