0% found this document useful (0 votes)
36 views2 pages

009-Java Open A New GUI Window

This Java code opens a new GUI window. It first creates a main LaunchPage window with a button. When the button is clicked, it disposes of the main window and opens a new NewWindow, displaying the text "Hello!". Both windows use Swing components like JFrame and JLabel and handle basic window properties and events.

Uploaded by

Momed Groove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

009-Java Open A New GUI Window

This Java code opens a new GUI window. It first creates a main LaunchPage window with a button. When the button is clicked, it disposes of the main window and opens a new NewWindow, displaying the text "Hello!". Both windows use Swing components like JFrame and JLabel and handle basic window properties and events.

Uploaded by

Momed Groove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

9 – Java open a new GUI window

// ********************************************

public class Main


{

public static void main(String[] args)


{
LaunchPage launchPage = new LaunchPage();
}
}

// ********************************************

import java.awt.event.*;
import javax.swing.*;

public class LaunchPage implements ActionListener


{
JFrame frame = new JFrame();
JButton myButton = new JButton("New Window");

LaunchPage()
{
myButton.setBounds(100,160,200,40);
myButton.setFocusable(false);
myButton.addActionListener(this);

frame.add(myButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setVisible(true);

}
@Override
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==myButton)
{
frame.dispose();
NewWindow myWindow = new NewWindow();
}
}
}
// ********************************************

import java.awt.*;
import javax.swing.*;

public class NewWindow


{
JFrame frame = new JFrame();
JLabel label = new JLabel("Hello!");

NewWindow()
{
label.setBounds(0,0,100,50);
label.setFont(new Font(null,Font.PLAIN,25));

frame.add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setVisible(true);

}
}

You might also like