Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JFrame Close Button

Status
Not open for further replies.

gabberatl

Technical User
Apr 1, 2015
1
US
Hi, I am new to Java. Better yet I'm new to programming PERIOD.

I have to write my Hello World program for my class and have it exit with an "Exit" button.

I cannot get it to work properly.

Can someone please help!!!!

This is what I have. It runs smoothly just cannot figure out how to add the button.

import javax.swing.*;

public class HelloWorldGUI

{

private static void createAndShowGUI() {

JFrame frame = new JFrame("");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Hello World!!!");

frame.getContentPane().add(label);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

createAndShowGUI();
}

}

 
Hi

That is simple : add a [tt]JButton[/tt] which listens for [tt]ActionEvent[/tt]s handled by your class, ( for this your class will have to implement [tt]ActionListener[/tt], ) then in [tt]actionPerformed()[/tt] method you just dispatch a [tt]WINDOW_CLOSING[/tt] event.

If you want to keep the current [tt]JLabel[/tt] too, add a layout manager of your choice which will make possible arranging multiple widgets the way you want.
Code:
[red]import[/red] java[teal].[/teal]awt[teal].*;[/teal]
[red]import[/red] java[teal].[/teal]awt[teal].[/teal]event[teal].*;[/teal]
[red]import[/red] javax[teal].[/teal]swing[teal].*;[/teal]

[b]public class[/b] [COLOR=mediumorchid]HelloWorldGUI[/color] [b]implements[/b] ActionListener [teal]{[/teal]
    [COLOR=orchid]JFrame[/color] frame[teal];[/teal]

    [b]private[/b] [maroon]void[/maroon] [COLOR=orange]createAndShowGUI[/color][teal]()[/teal]
    [teal]{[/teal]
        [COLOR=orchid]JLabel[/color] label [teal]=[/teal] [b]new[/b] [COLOR=orange]JLabel[/color][teal]([/teal][i][green]"Hello World!!!"[/green][/i][teal]);[/teal]
        [COLOR=orchid]JButton[/color] button [teal]=[/teal] [b]new[/b] [COLOR=orange]JButton[/color][teal]([/teal][i][green]"Run, Forrest, run !!!"[/green][/i][teal]);[/teal]

        button[teal].[/teal][COLOR=orange]addActionListener[/color][teal]([/teal][b]this[/b][teal]);[/teal]

        frame [teal]=[/teal] [b]new[/b] [COLOR=orange]JFrame[/color][teal]([/teal][i][green]""[/green][/i][teal]);[/teal]
        frame[teal].[/teal][COLOR=orange]setContentPane[/color][teal]([/teal][b]new[/b] [COLOR=orange]JPanel[/color][teal]([/teal][b]new[/b] [COLOR=orange]FlowLayout[/color][teal]()));[/teal]
        frame[teal].[/teal][COLOR=orange]setDefaultCloseOperation[/color][teal]([/teal]JFrame[teal].[/teal]EXIT_ON_CLOSE[teal]);[/teal]

        frame[teal].[/teal][COLOR=orange]getContentPane[/color][teal]().[/teal][COLOR=orange]add[/color][teal]([/teal]label[teal]);[/teal]
        frame[teal].[/teal][COLOR=orange]getContentPane[/color][teal]().[/teal][COLOR=orange]add[/color][teal]([/teal]button[teal]);[/teal]
        frame[teal].[/teal][COLOR=orange]pack[/color][teal]();[/teal]

        frame[teal].[/teal][COLOR=orange]setVisible[/color][teal]([/teal][b]true[/b][teal]);[/teal]
    [teal]}[/teal]

    [b]public[/b] [maroon]void[/maroon] [COLOR=orange]actionPerformed[/color][teal]([/teal][COLOR=orchid]ActionEvent[/color] e[teal])[/teal]
    [teal]{[/teal]
        frame[teal].[/teal][COLOR=orange]dispatchEvent[/color][teal]([/teal][b]new[/b] [COLOR=orange]WindowEvent[/color][teal]([/teal]frame[teal],[/teal] WindowEvent[teal].[/teal]WINDOW_CLOSING[teal]));[/teal]
    [teal]}[/teal]

    [b]public static[/b] [maroon]void[/maroon] [COLOR=orange]main[/color][teal]([/teal]String[teal][][/teal] args[teal])[/teal]
    [teal]{[/teal]
        [b]new[/b] [COLOR=orange]HelloWorldGUI[/color][teal]().[/teal][COLOR=orange]createAndShowGUI[/color][teal]();[/teal]
    [teal]}[/teal]

[teal]}[/teal]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top