简体   繁体   中英

creating instances of classes

I am currently working on some code with which I am having some trouble.

I have two buttons on a GUI. If one button is pressed it assigns a value to a string a value to reflect this.

The other button is the same except that a different value is assigned to the string.

This String is created at the beginning before the constructor in the following manner:

public string s = "String"; // public so I can call it in another class

The problem comes when I want to find out which button was pressed in another class. I want to see what s is so I have to create an instance of that class GUI:

gui = new GUI(); 

This then resets the value of s to "String" again and this ruins my comparison.

Is there any way I can get around this?

I've tried lots of ideas but nothing seems to work.

When you initially create the GUI (ie GUI gui = new GUI(); ), save that reference for when you want to access the member s .

When you create a new GUI object, that new object was never manipulated by the user, so its s value is just "String" . You need to hold the reference to the original object.

It is probably better practice to pass the state of your first form to the second form or have the first form inform the second form that a particular event has occurred in real time. Otherwise you would need to pass a reference of the first form to the second form to do the comparison you are trying IE:

public class SecondForm {
    private FirstForm _firstForm = null;
    public SecondForm(FirstForm firstForm){
        _firstForm = firstForm;
    }

    ...

    if(_firstForm.s == "comparison"){
        ...
    }
}

SecondForm secondForm = new SecondForm(this);

in the comments below this post, myself and 2 other individuals have determined that you should learn the java langauge, research the notion of object oriented programming (including object scope), and then learn how to implement the proper event handling code - there are several ways in which you can accomplish what you want, i would suggest a combination of the code i provided along with the reference suggestion supplied by Welbog up above (this of course, will require modification of my code, but not much actually if your main class is somewhere else)

basically, google a java tutorial (or buy a book/"acquire" a book), and if you know some basic concepts skip ahead to the object-oriented part.

if i follow what you want, here's some skeleton code:

// imports

public class GUI extends JFrame implements ActionListener {
    public String s = "String";

    public GUI() {
        // initializer code for buttons
        btn1.addActionListener(this);
        btn2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if(s == "???")
            s = "this";
        else
            s = "that";
    }

    // other methods

}

I would use a listener for observing the state of the 'string' variable.

public class Gui
{
    public static final String STRING_PROPERTY = "string";
    private String string = "String";
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    private void setString(String string)
    {
        String oldValue = this.string;
        this.string = string;
        propertyChangeSupport.firePropertyChange(STRING_PROPERTY, oldValue, this.string);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
    {
        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }
}

This snippet just demonstrates how to add propertyChangeListener support to your class. The class that is interested in knowing the value of 'string' would implement PropertyChangeListener and be added to through the 'addPropertyChangeListener' method.

You could use an ActionListener and an action command on each button as shown in this code

public class GUI  extends JFrame implements ActionListener
{
  public static final String BUTTON_1_PRESSED = "BUTTON_1_PRESSED";
  public static final String BUTTON_2_PRESSED = "BUTTON_2_PRESSED";

  public String s = "String";

  public GUI()
  {
    // create buttons
    button1.addActionListener(this);
    button1.setActionCommand(BUTTON_1_PRESSED);
    button2.addActionListener(this);
    button2.setActionCommand(BUTTON_2_PRESSED);
  }

  public void actionPerformed(ActionEvent e) 
  {
    if(e.getActionCommand().equals(BUTTON_1_PRESSED))
    {
      s = "this";
    }
    else if(e.getActionCommand().equals(BUTTON_2_PRESSED))
    {
      s = "that";
    }
  }
}

It's very difficult to decipher your question. But in general, if you want to know which button it was that caused an event, add a different ActionListener to each one (or its model). Keep the code as clean and simple as possible.

create an extra value that holds the button(name). Change the value when you click on the appropiate button and read it out from a different class. Basicly the same idea as s

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