Access a static variable from another class

I have two classes in one package. I declared static variablein one class and want to access this variable in another class.

Here is my code in which I declared a static variable

public class wampusGUI extends javax.swing.JFrame {

    static String userCommand;

    public wampusGUI() {
        initComponents();
    }

    public void setTextArea(String text) {
        displayTextArea.append(text);
    }

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
        userCommand = commandText.getText();
    }

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                wampusGUI w = new wampusGUI();
                w.setVisible(true);
                Game g = new Game(w);
                g.play();
            }
        });
    }
}

Here is the code in which I want to access the variable

public class Game {

    private wampusGUI gui;

    public Game(wampusGUI w) {
        world = new World();
        world.start();
        gui = w;
    }

    public void play() {
        gui.setTextArea(welcome());
        gui.setTextArea(describe());
        for (;;) {
            String s = userCommand; // here value should come should 
            System.out.println(userCommand);
            Command c = Command.create(s);
            String r = c.perform(world);
            // is game over?
            if (r == null) {
                break;
            }
            System.out.println(r);
        }
        System.out.println("Game over");
    }
}

However, I can pass a variable from the first class as an argument. but the problem is that when I run the program, the value will first be null, which I don't want. I want when I enter a value in textfield, then it must go to another class.

Thank.

+5
source share
3 answers

, GUI-. , , , - Swing PropertyChangeSupport, PropertyChangeListeners. Swing PropertyChangeListener. , , WampusGUI ( ) :

public Game(WampusGUI w) {
  gui = w;

  gui.addPropertyChangeListener(new PropertyChangeListener() {
     // ....
  }

Game gui.

gui userCommand String " ", setter, , . :

public class WampusGUI extends JFrame {
   public static final String USER_COMMAND = "user command";
   // ....

   private void setUserCommand(String userCommand) {
      String oldValue = this.userCommand;
      String newValue = userCommand;
      this.userCommand = userCommand;
      firePropertyChange(USER_COMMAND, oldValue, newValue);
   } 

String setter:

private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
  setUserCommand(commandText.getText());
}

:

  gui.addPropertyChangeListener(new PropertyChangeListener() {

     @Override
     public void propertyChange(PropertyChangeEvent pcEvt) {

        // is the property being changed the one we're interested in?
        if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {

           // get user command:
           String userCommand = pcEvt.getNewValue().toString();

           // then we can do with it what we want
           play(userCommand);

        }

     }
  });

, , GUI, (Game). :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class WampusGUI extends JFrame {
   public static final String USER_COMMAND = "user command";
   private String userCommand;
   private JTextArea displayTextArea = new JTextArea(10, 30);
   private JTextField commandText = new JTextField(10);

   public WampusGUI() {
      initComponents();
   }

   private void setUserCommand(String userCommand) {
      String oldValue = this.userCommand;
      String newValue = userCommand;
      this.userCommand = userCommand;
      firePropertyChange(USER_COMMAND, oldValue, newValue);
   }

   private void initComponents() {
      displayTextArea.setEditable(false);
      displayTextArea.setFocusable(false);
      JButton enterButton = new JButton("Enter Command");
      enterButton.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent evt) {
            enterButtonActionPerformed(evt);
         }
      });
      JPanel commandPanel = new JPanel(); 
      commandPanel.add(commandText);
      commandPanel.add(Box.createHorizontalStrut(15));
      commandPanel.add(enterButton);

      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BorderLayout());
      mainPanel.add(new JScrollPane(displayTextArea));
      mainPanel.add(commandPanel, BorderLayout.SOUTH);
      add(mainPanel);
   }

   public void setTextArea(String text) {
      displayTextArea.append(text);
   }

   private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
      setUserCommand(commandText.getText());
   }

   public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            WampusGUI w = new WampusGUI();
            w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            w.pack();
            w.setLocationRelativeTo(null);
            w.setVisible(true);
            Game g = new Game(w);
            g.play();
         }
      });
   }
}

class Game {
   private WampusGUI gui;

   public Game(WampusGUI w) {
      gui = w;

      gui.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {

            // is the property being changed the one we're interested in?
            if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {

               // get user command:
               String userCommand = pcEvt.getNewValue().toString();

               // then we can do with it what we want
               play(userCommand);

            }
         }
      });
   }

   public void play() {
      gui.setTextArea("Welcome!\n");
      gui.setTextArea("Please enjoy the game!\n");
   }

   public void play(String userCommand) {
      // here we can do what we want with the String. For instance we can display it in the gui:
      gui.setTextArea("User entered: " + userCommand + "\n");
   }

}
+2

, ,

gui.setTextArea(welcome());
gui.setTextArea(describe());

, .

  • setTextArea - , . , .
  • , (EDT), . , Command, Event Dispatch Thread, . Concurrency Swing
  • for - , EDT . , ( EDT, ). JOptionPane (, JOptionPane#showInputDialog). - , .
+4

, ...

ur, :

public class wampusGUI extends javax.swing.JFrame
{
    private static wampusGUI myInstance;
    public wampusGUI( )
    {
        myInstance = this;
        initComponents();
    }

    public static void getUserCommand()
    {
        if(myInstance!=null)
        {
            return myInstance.commandText.getText();
        }
        else
        {
            return null;
        }
    }
    ......
    ......
}

:

public void play()
{
    .....
    //String s = userCommand; // here value should come should
    String s = wampusGUI.getUserCommand();
    .....
}

... .

0

All Articles