Skip to main content

Swing help in java

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2012-07-22

I am a beginner in java. i am working on swing to make a GUI. i have created a frame with a enter button. now i want that on clicking enter a window opens up and closes the existing window. Plz help me regarding this .

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-08-02

I attached basic GUI application using in swing....

import java.awt.event.*;

import javax.swing.*;
public class Frame extends JFrame implements ActionListener{
JFrame jf;
JTextField jtf;
JButton jb;
JLabel jl;
String a=" ";

Frame() //constructor for this class
jf=new JFrame();  // create an instance for Jframe

* To Design the Layout If we set the layout is null means we able to use setBounds(jtf.setBounds)
* setBounds is used to arrange the components inside the frame
* setBounds have 4 parameters (i.e)x,y,width and height
jl=new JLabel("Please Enter the text");// create an instance for JLabel class
jtf=new JTextField("",60);// create an instance for JTextField with empty textbox
jb=new JButton("Click Me"); // instance for button class

jtf.setBounds(50, 50, 100, 50);
jb.setBounds(50, 150, 100, 50);
jl.setBounds(200, 50, 200, 50);
* add the components inside the frame by using jFrame object

* ActionListener is a class used to perform whatever actions we performed.
* we need to implement ActionListener class into our class

jf.setTitle("My Form Name Is Mohan");
jf.setSize(1000, 1000);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);//if we need to close the frame means use this
jf.setVisible(true);// if we set this visible is false it does not how for us


* default method for ActionListener class is actionperformed
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e!=null) // if action is performed means goto another if condition
if(jtf.equals(a)) //if textfield is null means set the error text to label box
jl.setText("Please Enter The Text");
jl.setText(jtf.getText()); //if text field have any text means that text shows in label button
public static void main(String args[]){
Frame f=new Frame();