如何用java中另一个JFrame类中的按钮关闭JFrame类?

时间:2015-06-25 07:06:37

标签: java swing jframe

我有两个JFramesframeAFrameB)。 frameB只能从frameA打开,当我打开frameB时,frameA必须保持打开状态。 frameB有一个按钮(Close_frameA)。我想点击按钮关闭frameA

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用以下两个类:TJFrame和OpenFrame,使用另一个JFrame中的按钮关闭JFrame类

 public class TJFrame {
public static OpenFrame openWindow;

public static void main(String[] args) {
    JFrame frame = new JFrame("Swing Frame");
    JButton button = new JButton("Open");
    frame.add(button);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            openWindow = new OpenFrame();
            openWindow.setVisible(true);
        }
    });
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setSize(350, 200);
    frame.setVisible(true);
    }}
  public class OpenFrame extends JFrame{

JPanel back_panel;
public JButton button = new JButton("Cross");

public OpenFrame() {
    back_panel = new JPanel();
    setContentPane(back_panel);
    this.setSize(350, 200);

    button.setBounds(380, 10, 20, 20);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dispose();

        }
    });

    back_panel.add(button);
}}

答案 1 :(得分:0)

首先,每个JFrame是由不同的类别制作的(我会​​这样认为,因为我不知道如何制作两个帧)。

尝试的可能解决方案: 在框架A中,创建一个"静态变量":

//lets call the class that create frameA ClassA
public class ClassA extends JFrame {
    static JFrame frameA;

而不是

JFrame frameA=new JFrame("Name of the frame");

在public static void main(String [] args)。然后,在public static void main(String [] args)程序中,做

//the static JFrame assigned before
frameA= new JFrmae("Nameof the frame");

这让frameB中的程序读取" frameA"使用ClassB中的以下代码(让我们调用构成frameB ClassB的类):

JFrame frameA= ClassA.frameA;

然后,仍然在ClassB中,我们可以做

frameA.dispose();

我希望你理解(如果你不理解,请评论你不明白的事情),我希望它有效。

代码:

import javax.swing.JFrame;

public class ClassA {
    static JFrame frameA;
    public ClassA(){
        //a useless constructor because I am not adding any Listeners(don't worry about it)
    }
    public static void main(String[] args){
        frameA=new JFrame("Name");

        //your ordinary things(some peiople put these in the constructor)
        frameA.setSize(300,300);
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameA.setVisible(true);
        //runs ClassB
        new ClassB();
    }
}

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

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


public class ClassB extends JFrame implements ActionListener{
    static JButton close=new JButton("close");
    public ClassB(){

        //your ordinary thigns
        add(close);
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        close.addActionListener(this);
        System.out.println("what?");
    }

    public static void main(String[] args){
        JFrame frameB=new JFrame("Clae Frame A");
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.equals("close")){
            JFrame frameA=ClassA.frameA;
            frameA.dispose();
        }

    }

}
相关问题