Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:05:28+00:00 2026-06-04T17:05:28+00:00

I’m using two frames. In the first frame I have a button to open

  • 0

I’m using two frames. In the first frame I have a button to open the second frame. In the second frame there is also a button, but this one is for closing the second frame. But I don’t know how to do this, and I’m looking for some help to solve this?

GUI1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends JFrame implements ActionListener{
JButton btn1;
Container contentPane;
public GUI1()
{
    setTitle("GUI 1");
    setResizable(false);
    setSize(600,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn1 = new JButton("Open GUI 2 frame");
    contentPane.add(btn1);
    btn1.setFocusable(false);
    btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn1)
    {
        GUI2 frame2 = new GUI2();
        frame2.setVisible(true);
    }
}
public static void main(String[] args) {
    GUI1 frame = new GUI1();
    frame.setVisible(true);
}
}

GUI2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI2 extends JFrame implements ActionListener {
Container contentPane;
JButton btn2;
public GUI2()
{
    setTitle("GUI 2");
    setResizable(false);
    setSize(400,200);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn2 = new JButton("Close GUI 2 frame");
    contentPane.add(btn2);
    btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn2)
    {
        // Close GUI2 ??
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:05:31+00:00Added an answer on June 4, 2026 at 5:05 pm

    Simply call dispose() in the listener:

    public void actionPerformed(ActionEvent event){
        if(event.getSource() == btn2)
        {
            dispose();
        }
    

    Also, by clicking X, this will dispose the window since you have set the defaultCloseOperator(DISPOSE_ON_CLOSE);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.