Good grief! Who taught you that sort of code? Please tell us
where you found that code, so we can be sure there isn't a breach of copyright. I am pretty sure I have seen that code before somewhere.
I have remembered where I last saw it. I told you it was dreadful then and I am telling you it is dreadful now. The worst bit is making the frame a listener. You are now moving out of the realms of object‑oriented programming (or for Java8, functional programming). If you have two buttons you will end up with a horrible mess of nested
if‑elses. Each button should have it own listener and each listener should be an object (or in Java8 maybe a function).
Read
this thread and the links therein for a longer discussion about listeners.
Also find out about Actions (and the AbstractAction class); Actions are similar to action listeners but probably better.
If you use Java8, find out about using a λ instead of an anonymous class.
Moving this discussion to our GUIs forum.