FrameOne上的按钮应关闭FrameTwo。在JAVA中,ActionListener指的是另一个类的对象[英] Button on FrameOne shall close FrameTwo. ActionListener referes object of another class in JAVA

本文是小编为大家收集整理的关于FrameOne上的按钮应关闭FrameTwo。在JAVA中,ActionListener指的是另一个类的对象的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在单独的类(Frameone和Frametwo)框架中有两个帧有两个按钮.一个打开Frametwo,一个应关闭Frametwo.我知道如何打开Frametwo.但不是如何从框架上关闭的方法.如何编码以使其正常工作?谢谢. (我知道有类似的问题.我全部红色.但这并没有给我答案.GUI指南也没有帮助.)

public class Main {

    public static void main(String[] args) {

        FrameOne frame = new FrameOne();
    }
}

框架类:

    public class FrameOne extends JFrame implements ActionListener{

    private JButton btn1, btn2;

    FrameOne () {

        setVisible(true);
        setSize(400,400);
        setLayout(new FlowLayout());
        setTitle("Main");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        btn1 = new JButton("opens FrameTwo");
        btn2 = new JButton("close FrameTwo");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        add(btn1);
        add(btn2);
    }

    @Override
    public void actionPerformed (ActionEvent e) {
        if(e.getSource()== btn1) {  
            FrameTow frameTwo = new FrameTwo(); 
        }

        else if(e.getSource()== btn2) ;
        // {???.dispose(); }
    }
}

` frame2类:

    public class FrameTow extends JFrame {

    FrameTwo () {

        setVisible(true);
        setSize(400,400);
        setTitle("FrameTwo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(400, 400);

    }
}

推荐答案

以下任何解决方案都可以工作

frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));

frameTwo.setVisible(false);

其他推荐答案

简短的答案是修改FrameOne:

private JFrame frameTwo; //introduce a field  


@Override
public void actionPerformed (ActionEvent e) {
    if(e.getSource()== btn1) {
        frameTwo = new FrameTwo();  //use field in action listener 
    }

    else if(e.getSource()== btn2){
        frameTwo.dispose();         //use field in action listener 
    }
}

较长的答案:使用JDialog为第二帧是一个更好的做法:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {

       new FrameOne();
    }
}

class FrameOne extends JFrame implements ActionListener{

    private final JButton btn1, btn2;
    private JDialog frameTwo; //introduce a field

    FrameOne () {

        setSize(400,400);
        setLayout(new FlowLayout());
        setTitle("Main");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        btn1 = new JButton("opens FrameTwo");
        btn2 = new JButton("close FrameTwo");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        add(btn1);
        add(btn2);
        setVisible(true); //make it visible after construction is completed
    }

    @Override
    public void actionPerformed (ActionEvent e) {
        if(e.getSource()== btn1) {
            frameTwo = new FrameTwo();  //use field in action listener
        }

        else if(e.getSource()== btn2){
            frameTwo.dispose();         //use field in action listener
        }
    }
}

class FrameTwo extends JDialog {

    FrameTwo() {

        setSize(400,400);
        setTitle("FrameTwo");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocation(400, 400);
        setVisible(true); //make it visible after construction is completed
    }
}

其他推荐答案

在您显示的示例中,可以打开几个实例FrameTwo.第二个按钮应该关闭哪一个?

假设您只想有一个,则可以在框架中引入一个字段,最初设置为null.然后,btn1仅在字段为null时才打开帧,并将其分配给字段.然后btn2可以在字段上调用dispose()(并将其重置为null).

示例,基于您的尝试:

public class FrameOne extends JFrame implements ActionListener {

private JButton btn1, btn2;
private FrameTwo frameTwo = null;

FrameOne () {

setVisible(true);
setSize(400,400);
setLayout(new FlowLayout());
setTitle("Main");
setDefaultCloseOperation(EXIT_ON_CLOSE);

btn1 = new JButton("opens FrameTwo");
btn2 = new JButton("close FrameTwo");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);



}

@Override
public void actionPerformed (ActionEvent e)
{if(e.getSource()== btn1) 
{
    if (frameTwo == null) {    
        frameTwo = new FrameTwo();
    }
}

else if(e.getSource()== btn2) {

    frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
    frameTwo = null;

}}}

本文地址:https://www.itbaoku.cn/post/2091196.html

问题描述

I have two frames in separate classes (FrameOne and FrameTwo) FrameOne has two buttons. One to open FrameTwo and One shall close FrameTwo. How to open FrameTwo I know. But not how to close from Frame One. How do I code it to make it working? Thanks. (I know that there are similar questions. I red them all. But it didn't gave me the answer. Also GUI guides didn't helped.)

public class Main {

    public static void main(String[] args) {

        FrameOne frame = new FrameOne();
    }
}

FrameOne class:

    public class FrameOne extends JFrame implements ActionListener{

    private JButton btn1, btn2;

    FrameOne () {

        setVisible(true);
        setSize(400,400);
        setLayout(new FlowLayout());
        setTitle("Main");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        btn1 = new JButton("opens FrameTwo");
        btn2 = new JButton("close FrameTwo");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        add(btn1);
        add(btn2);
    }

    @Override
    public void actionPerformed (ActionEvent e) {
        if(e.getSource()== btn1) {  
            FrameTow frameTwo = new FrameTwo(); 
        }

        else if(e.getSource()== btn2) ;
        // {???.dispose(); }
    }
}

` Frame2 class:

    public class FrameTow extends JFrame {

    FrameTwo () {

        setVisible(true);
        setSize(400,400);
        setTitle("FrameTwo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(400, 400);

    }
}

推荐答案

Any of the below solutions will work

frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));

OR

frameTwo.setVisible(false);

其他推荐答案

The short answer is modify FrameOne :

private JFrame frameTwo; //introduce a field  


@Override
public void actionPerformed (ActionEvent e) {
    if(e.getSource()== btn1) {
        frameTwo = new FrameTwo();  //use field in action listener 
    }

    else if(e.getSource()== btn2){
        frameTwo.dispose();         //use field in action listener 
    }
}

The longer answer: using a JDialog for the 2nd frame is a better practice:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {

       new FrameOne();
    }
}

class FrameOne extends JFrame implements ActionListener{

    private final JButton btn1, btn2;
    private JDialog frameTwo; //introduce a field

    FrameOne () {

        setSize(400,400);
        setLayout(new FlowLayout());
        setTitle("Main");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        btn1 = new JButton("opens FrameTwo");
        btn2 = new JButton("close FrameTwo");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        add(btn1);
        add(btn2);
        setVisible(true); //make it visible after construction is completed
    }

    @Override
    public void actionPerformed (ActionEvent e) {
        if(e.getSource()== btn1) {
            frameTwo = new FrameTwo();  //use field in action listener
        }

        else if(e.getSource()== btn2){
            frameTwo.dispose();         //use field in action listener
        }
    }
}

class FrameTwo extends JDialog {

    FrameTwo() {

        setSize(400,400);
        setTitle("FrameTwo");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocation(400, 400);
        setVisible(true); //make it visible after construction is completed
    }
}

其他推荐答案

In the example you show, one can open several instances of FrameTwo. Which one should the second button close?

Assuming you want there to be only one, you could introduce a field in in FrameOne, initially set to null. btn1 would then only open a frame if the field is null, and assign it to the field. Then btn2 can call dispose() on the field (and reset it to null).

Example, based on your attempt:

public class FrameOne extends JFrame implements ActionListener {

private JButton btn1, btn2;
private FrameTwo frameTwo = null;

FrameOne () {

setVisible(true);
setSize(400,400);
setLayout(new FlowLayout());
setTitle("Main");
setDefaultCloseOperation(EXIT_ON_CLOSE);

btn1 = new JButton("opens FrameTwo");
btn2 = new JButton("close FrameTwo");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);



}

@Override
public void actionPerformed (ActionEvent e)
{if(e.getSource()== btn1) 
{
    if (frameTwo == null) {    
        frameTwo = new FrameTwo();
    }
}

else if(e.getSource()== btn2) {

    frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
    frameTwo = null;

}}}