简体   繁体   中英

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

I'm new to java and I am trying to output to a textfield that is in a class outside of the main and I can't get it to work.

In my main, I have:

public static void 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(){
         FlowLayout flo = new FlowLayout();
         Container pane = getContentPane();

         JTextArea display = new JTextArea (30, 90);
         JButton button = new JButton("CLICK ME");
         JLabel label = new JLabel("Dont Click Him!");

         setTitle("Merchables!?");
         setVisible(true);
         setSize(1000,600);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pane.setLayout(flo);
         pane.add(display);
         pane.add(button);
         pane.add(label);
   }
}

You need to make display a public field of class GUI.

public class GUI extends JFrame{
    public final JTextArea display;
    public GUI(){
        FlowLayout flo = new FlowLayout();
        Container pane = getContentPane();

        display = new JTextArea (30, 90);

        JButton button = new JButton("CLICK ME");
        JLabel label = new JLabel("Dont Click Him!");

        setTitle("Merchables!?");
        setVisible(true);
        setSize(1000,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setLayout(flo);
        pane.add(display);
        pane.add(button);
        pane.add(label);
    }
}

I think the error you are getting is because the variable 'display' is local to the scope of its nearest curly braces {}.

This means that it is only in scope for: public GUI() { ..//in scope here }

What you want is to move the display variable outside of the method, and make it accessible to other methods. As follows:

 import java.awt.*;
 import javax.swing.*;

 public class GUI extends JFrame{
      // Now display is publicly accessible outside the GUI class
      public JTextArea display;
      public GUI(){
         FlowLayout flo = new FlowLayout();
         Container pane = getContentPane();

         display =  = new JTextArea (30, 90);
         JButton button = new JButton("CLICK ME");
         JLabel label = new JLabel("Dont Click Him!");

         setTitle("Merchables!?");
         setVisible(true);
         setSize(1000,600);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pane.setLayout(flo);
         pane.add(display);
         pane.add(button);
         pane.add(label);
   }
}

I advise you also look up encapsulation, with get and set methods. It is bad practise to use public variables. Private variables should be used and get/set methods should be used to access them.

For example:

 import java.awt.*;
 import javax.swing.*;

 public class GUI extends JFrame{
      // Now display is private to the GUI class
      private JTextArea display;
      public GUI(){
         FlowLayout flo = new FlowLayout();
         Container pane = getContentPane();

         display =  = new JTextArea (30, 90);
         JButton button = new JButton("CLICK ME");
         JLabel label = new JLabel("Dont Click Him!");

         setTitle("Merchables!?");
         setVisible(true);
         setSize(1000,600);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pane.setLayout(flo);
         pane.add(display);
         pane.add(button);
         pane.add(label);
   }

   // Get accessor method for display variable
   public JTextArea getDisplay() {
         return display;
   }
}

Now, in your main to edit the display, you want something like to get the display variable and append the text:

public static void main(String[] args) {
    GUI gu = new GUI ();
    gu.getDisplay().append("hi");
}

Finally, if you're bothered about other classes accessing the object directly (as per MadProgrammer's comment), a better method may be to have this in your GUI class:

   // Method to access and append text to display variable without getting it
   public void appendToDisplay(String text) {
         display.append(text);
   }

and call that instead. This allows the caller to only append text to the display variable, rather than whatever they like. Your main method now looks like:

   public static void main(String[] args) {
         GUI gu = new GUI ();
         gu.appendToDisplay("hi");
   }

There are a number of ways this might be achieved,mother simplest is probably providing some means by which other classes can access your JTextArea .

The first problem you need to over come is changing the reference context of JTextArea , as it is currently only accessible from a local context within your GUI constructor

public class GUI extends JFrame{
    public GUI(){
        //.,,
        JTextArea display = new JTextArea (30, 90);

This means that display will only be accessible from within the context of constructor.

You need to move the display so that it is accessible from within the class context, that is, make it a class or instance field...

public class GUI extends JFrame{
    JTextArea display;
    public GUI(){
        //.,,
        display = new JTextArea (30, 90);

This next problem is deciding how to allow access to the display . You could make the class field public ...

public class GUI extends JFrame{
    public JTextArea display;

But this exposes the display to anybody. GUI components are complex objects with multiple properties and states. Exposing a GUI component in this way removes you ability to manage the states and properties of this object, it also exposes the component to abuse, as other classes can suddenly do what ever they like to/with it...which isn't very desirable.

A better solution is to restrict access to the field and provide management functionality via the use of methods within the class it self...

public class GUI extends JFrame{
    private JTextArea display; // Only accessible from this instance of GUI

    //...

    public void appendDisplay(String text) {
        display.append(text);
    }

    public String getDisplayText() {
        return display.getText();
    }

This means you GUI class now controls access to the display and determines what other classes can do to it...

Have a look at Controlling Access to Members of a Class for more details...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM