FrameOne上的按钮应关闭FrameTwo。 ActionListener在JAVA中引用另一个类的对象

问题描述 投票:0回答:4

我在单独的类中有两个框架(FrameOne和FrameTwo)。FrameOne有两个按钮。一个打开第二个框架,一个打开第二个框架。我知道如何打开FrameTwo。但不是如何从第一帧关闭。如何对其进行编码以使其正常工作?谢谢。 (我知道也有类似的问题。我全部都答了。但是它没有给我答案。GUI指南也没有帮助。)

public class Main {

    public static void main(String[] args) {

        FrameOne frame = new FrameOne();
    }
}

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);

    }
}
java swing jbutton frame
4个回答
0
投票

以下任何一种解决方案都可以使用

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

OR

frameTwo.setVisible(false);

0
投票
public class FrameOne extends JFrame implements ActionListener{

    private JButton btn1, btn2;
    private Window frameTwo = null; // <---- here is all I could do. I don't know how to go on

    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)    {
            FrameTwo frameTwo = new FrameTwo();
        }

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

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

0
投票

简短的答案是修改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
    }
}

-1
投票

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

假设您只希望有一个,您可以在FrameOne中引入一个字段,该字段最初设置为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;

}}}
© www.soinside.com 2019 - 2024. All rights reserved.