Java Swing; how to show text after button is clicked -


I want 'hello world'! To show when my button is clicked, go to one next frame, but in the same window! Import javax I

What am I doing in this code?

  import javax swing.JButton; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.JOptionPane; Public category myTest {public static zero main (string [] args) {jpn panel = new zpn (); Pocket button 1 = new pocket (); Frame.add (panel); Panel.add (Button1); Button1.addActionListener (public action action action) (ActionAgentRGET) {Joppenpenny. Shomosge Delog (blank, "Hello World")}}}); }}    

Try with code:

  Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.JOptionPane; Public category myTest {public static zero main (string [] args) {last JFrame frame = new JFrame (); Zpean panel = new zpn (); Pocket button 1 = new pocket (); Frame.add (panel); Panel.add (Button1); Frame.setVisible (true); Button1.addActionListener (new executable () {public zero verb action (ActionEvent arg0) {JOptionPane.showMessageDialog (frame.getComponent (0), "Hello World")}}}); }}   

This is working as expected.

Or try this code if you want a message on the same frame.

  Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.JLabel; Import javax.swing.JOptionPane; Import javax.swing.JPanel; Public category myTest {public static zero main (string [] args) {last JFrame frame = new JFrame (); Zpean panel = new zpn (); Pocket button 1 = new pocket (); Last JL Label Label = New JLABEL ("Hello World"); Label.setVisible (wrong); Frame.add (panel); Panel.add (Button1); Panel.add (label); Frame.setVisible (true); Button1.addActionListener {Public Zero ActionPerfed (Action Event RGET) {//JOptionPane.showMessageDialog(frame.getComponent (0), "Hello World"); Labels .Setizable (true);}}); }}    

Comments