swing - How to append text to a textfield from a class outside of main in Java? -


I'm new to Java and I'm trying to output a text file that is found in main and outer sections I can not get it to work.

In my main, I have:

  public static zero main (string [] args) {GUI gu = new GUI (); Gu.display.append ("hi"); }  

In my GUI class, I have:

  import java.awt. *; Import javax.swing *; Public class GUI extends JFrame (public GUI) {Flow layout float = new flow layout (); Container pane = getContentPane (); JTextArea Display = New JTextArea (30, 90); Jebton Button = New Pocket ("Click Me"); Jlabel label = new jlabel ("do not click it!"); SetTitle ("Merchables!!"); SetVisible (true); SetSize (1000600); SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Pane.setLayout (flo); Pane.add (display); Pane.add (button); Pane.add (label); }}   

I think the error you're getting is because the variable ' The display is 'Local' within the scope of its closest horoscope braces {}.

This means that it is only in this area: Public GUI (). Karin Scope Here}

Do you want to move the display variable outside the method , And make it accessible for other methods. Here is how:

  import java.awt. *; Import javax.swing *; Public class GUI grows GFIARAI {// Now the display is publicly available outside the GUI class public JetExteria display; Public GUI (Flow Layout Flow = New Flow Layout); Container pane = getContentPane (); Display = = new JTextArea (30, 90); Jebton Button = New Pocket ("Click Me"); Jlabel label = new jlabel ("do not click it!"); SetTitle ("Merchables!!"); SetVisible (true); SetSize (1000600); SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Pane.setLayout (flo); Pane.add (display); Pane.add (button); Pane.add (label); }}  

I recommend that you also see encapsulation with similar methods. This is a poor practice for using public variables. Private variables should be used and used to access them.

For example:

  import java.awt. *; Import javax.swing *; Public class GUI extends JFrame {// Display Now GUI class is private for personal JTextArea display; Public GUI (Flow Layout Flow = New Flow Layout); Container pane = getContentPane (); Display = = new JTextArea (30, 90); Jebton Button = New Pocket ("Click Me"); Jlabel label = new jlabel ("do not click it!"); SetTitle ("Merchables!!"); SetVisible (true); SetSize (1000600); SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Pane.setLayout (flo); Pane.add (display); Pane.add (button); Pane.add (label); } // Get accessory method for display variable JTextArea getDisplay () {return display; Now, to edit the display in your head, you want something that you get the display variable and add text:  
  Public static zero main (string [] args) {GUI gu = new GUI (); . Gu.getDisplay () Attached ("Hi"); }  

Finally, if you are disturbing other sections directly to the object (according to the Madprogrammer's comment), then it may be in your GUI class: < / P>

 Adding text to display the variable to use the  // method and it is publicly endowed with plain text {display.append (text); }  

and call instead. This allows the caller to attach the text to the display variable instead of just what they prefer. Your main method now looks:

  public static zero main (string [] args) {GUI gu = new GUI (); Gu.appendToDisplay ("hi"); }  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -