0

That's My Code Down Here. I want the answer for java.awt.Button and java.awt.Frame.

Can any one help me with it?

import java.awt.*;
import java.awt.event.*;

public class TestGUI extends Frame implements ActionListener, WindowListener{

    private Label lbl;
    private Label lbl1
    private Label lbl2;
    private Label lbl3;
    private TextField tf;
    private TextField tf1;
    private TextField tf2;
    private Button btn;
    private Button btn1;
    private Frame frame;

    public TestGUI() {
        setLayout(new FlowLayout());

        lbl = new Label("Hi Guys! That's My First GUI Program and is made by me too");
        add(lbl);

        lbl1 = new Label("Enter Your Name Please ~");
        add(lbl1);

        tf1 = new TextField(30);
        tf1.setEditable(true);
        add(tf1);

        lbl2 = new Label("Enter Your Age Please ~");
        add(lbl2);

        tf2 = new TextField(30);
        tf2.setEditable(true);
        add(tf2);

        lbl3 = new Label("Enter Your School/College Name Please ~");
        add(lbl3);

        tf = new TextField(28);
        tf.setEditable(true);
        add(tf);

        btn = new Button("Cancel");
        add(btn);

        btn.addActionListener(this);

        addWindowListener(this);

        setTitle("My own GUI");
        setSize(500, 300);

        setVisible(true);
    }

    public static void main(String[] args){
        TestGUI app = new TestGUI();
    }

    @Override
    public void actionPerformed(ActionEvent evt){

    }

    @Override
    public void windowClosing(WindowEvent evt){
        System.exit(0);
    }

    @Override public void windowDeactivated(WindowEvent evt){}

    @Override public void windowActivated(WindowEvent evt){}

    @Override public void windowOpened(WindowEvent evt){}

    @Override public void windowClosed(WindowEvent evt){}

    @Override public void windowIconified(WindowEvent evt){}

    @Override public void windowDeiconified(WindowEvent evt){}
}

Thanks in Advance.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Keval
  • 141
  • 1
  • 10

3 Answers3

0

You're just complicating the things. Instead of extending the frame & implementing those interfaces, just extend JFrame.

public class TestGUI extends JFrame{...}

In your TestGUI frame create another JFrame say otherFrame and create two bottons say Open & Close and then bind ActionListener to them.

openBtn.addActionListener(new ActionListener(){
    otherFrame.setVisible(true);
});

closeBtn.addActionListener(new ActionListener(){
    otherFrame.setVisible(false);
});

The setVisible() method accepts boolean & this is what you actually need. Much simpler & cleaner code.

Adarsh Singhal
  • 362
  • 1
  • 3
  • 12
0

It might make more sense for you to use a JFrame instead of a Frame (I recomend you read Kumar Vivek Mitra's answer here to get a better idea of why). If you use a JFrame, you'll need to call yourJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to stop your program when you close the window.

To respond to your button clicks, simply pass Anonymous Classes to your buttons addOnClickListener() method, like this:

btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        //Do stuff here

                    }

                });

Then you should be able to remove your existing actionPerformed() method.

For opening a new frame and closing your existing one, you should be creating two JFrame objects instead of extending Frame (or JFrame). Then, when you want to open your second frame, just call secondFrame.setVisable(true), and close your first one with firstFrame.dispose. However, I'd have a look at JDialogs and JOptionPanes first to see if they might work better for you.

After all this you should be able to remove all your WindowListener stuff, as that's for something slightly different. (Have a look here if you're interested)

Finally, don't forget to add a semicolon after your lbl1 label. ;)

Good luck!

Community
  • 1
  • 1
Gulllie
  • 523
  • 6
  • 21
0

You may use ActionListener interface. However for a little addition to above guys commented. You may add animation to your frame by adding for loop and setSize method within the loop and the height width of the corresponding loop's variable.