closing my jframe without using the close(X) butto

2019-07-24 16:01发布

i have a frame that instantiates another frame but i don't want to use the close(x) button on the instantiated frame., so i created a button. how do i code that this button can be used to close the instantiated frame without quitting the JVM.

标签: jframe
6条回答
Evening l夕情丶
2楼-- · 2019-07-24 16:41

You could try the setVisible() or dispose() functions

For your main class...

    public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    //new DemoTryCatch()
    new Frame1();
}

}

For your first frame class...

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Frame1 extends JFrame{
JButton newFrame=new JButton("Frame 2");
public Frame1() {
    // TODO Auto-generated constructor stub
    super("Frame 1");
    add(newFrame);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300,300);
    newFrame.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            new Frame2();\\instantiating your new Frame
        }
    });
}
}

For the instantiated frame...

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Frame2 extends JFrame{

JButton CloseFrame2=new JButton("CloseFrame2");
public Frame2() {
    // TODO Auto-generated constructor stub
    super("Frame 1");
    add(CloseFrame2);
    setVisible(true);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setSize(300,300);
    CloseFrame2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            setVisible(false);\\You could also use dispose()
        }
    });
}
}
查看更多
相关推荐>>
3楼-- · 2019-07-24 16:51

You could use setVisible(false) in order to just hide the instantiated JFrame from view

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-24 16:51

I agree with Tom, I think that in the code you wrote down:

[framename].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

EXIT_ON_CLOSE means that it will exit the application completely.

You can close the window by clicking on its X button by using:

[framename].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Here is a sample code:

package answers;

import javax.swing.JFrame;

public class Answers {

    public static void main(String[] args) {

        //frame 1
        JFrame frame1 = new JFrame("this is frame 1");
        frame1.setSize(500, 500);
        frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame1.setLocationRelativeTo(null); // ignore this its just to place frame 1 on the center

        //now this is the code for frame2
        //frame 2
        JFrame frame2 = new JFrame("this is frame 2");
        frame2.setSize(500, 500);
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);

    }

}
查看更多
Summer. ? 凉城
5楼-- · 2019-07-24 16:56

Having your own close button is weird UI and should be avoided.

To get rid of a frame when your own button is clicked you can just do:

jFrame.setVisible(false);

or

jFrame.dispose();

if you want to get rid of it completely.

On frames you don't want to exit the JVM on the close button being clicked specify:

jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

or

jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

Depending on which of the behaviour you want.

查看更多
看我几分像从前
6楼-- · 2019-07-24 16:58

Use this:

jFrame.setUndecorated(true);
jFrame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
查看更多
别忘想泡老子
7楼-- · 2019-07-24 16:59

Im not sure if im right but you could call dispose(). The javadoc suggests you can reopen it though using show().

查看更多
登录 后发表回答