0

I am currently taking a programming class in high school focused on Java. We have started to get into Swing and GUI's and I am really liking creating GUI applications. So my question was:

A program I wrote displays a JFrame with two buttons: Continue and Quit. I figured out how to set the quit action to the quit button but now I want to know how I cant create an ActionListener and add it to the continue button. When it's clicked, it will display a simple message like "You Did It!".

What I was thinking was to create a method that will create a new JFrame with that message. When the continue button is clicked it will call that method and display the message.

Is there an easy way to do this?

Any help would be awesome! Thank You!

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
shrillhook
  • 37
  • 3
  • 1
    There is simply `JOptionPane` which you may use to simply display a String message. Somewhat like this... `JOptionPane.showMessageDialog(null, "You did it!");` – 3kings Apr 19 '16 at 01:33
  • 2
    [How to use dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Apr 19 '16 at 01:35
  • Also to simply add an actionListener to a button just simply do... `buttonName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "You did it!"); } });` – 3kings Apr 19 '16 at 01:37
  • 1
    [Many, many, many dups](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=opening+new+jframe+from+button+site:http:%2F%2Fstackoverflow.com%2F). – Hovercraft Full Of Eels Apr 19 '16 at 01:38
  • 1
    Also the obligatory: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Apr 19 '16 at 01:39
  • Thanks for the comments I'm actually going to try this right now. :) – shrillhook Apr 19 '16 at 05:20
  • Hey thanks it worked! But now I noticed that joptionpane displays with an OK button is there any way to add an action listener to the OK button or to the joptionpane itself? – shrillhook Apr 19 '16 at 07:10

0 Answers0