Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从另一个java表单更改值_Java_Variables_Static - Fatal编程技术网

从另一个java表单更改值

从另一个java表单更改值,java,variables,static,Java,Variables,Static,我有一个主窗体(RandomSend)和另一个名为(_user)的窗体 在randomsend表单中,我声明了一个公共静态变量: public class RandomSend extends javax.swing.JFrame { ...... public static String userGender; // this variable I want to change from another form (_user) .... } 在RandomSend类中,我声明了_use

我有一个主窗体(RandomSend)和另一个名为(_user)的窗体 在randomsend表单中,我声明了一个公共静态变量:

 public class RandomSend extends javax.swing.JFrame {
 ......
public static String userGender; // this variable I want to change from another form (_user)
....
}
在RandomSend类中,我声明了_user实例,试图更改userGender

 _user setGender = new _user();
  setGender.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
  setGender.setAlwaysOnTop(true);
  setGender.setVisible(true);
在_用户表单(类)中,我试图更改userGendervale:

public class _user extends javax.swing.JFrame {......
     ....
     RandomSend.userGender="male";
     ....}
当我从\u user中检查值时,RandomSend.userGender的值为“男性”

但是从我的主要形式来看,这个值是空的


新建

根据答案1我的尝试 公共类RandomSend扩展了javax.swing.JFrame{

/**
 *
 */

private static String userGender;
 .....
.....
// show dialogbox to select gender...
 _user setGender = new _user();
  setGender.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
  setGender.setAlwaysOnTop(true);
  setGender.setVisible(true);

....
....

// setter
public static void setUserGender(String gender)
{
    if(gender.toLowerCase().equals("female") ||gender.toLowerCase().equals("male"))
    userGender = gender;
    else userGender= "Unknown!!";
}

//getter
public static String getUserGender()
{
        return userGender;
 }
在另一类(框架)中:


但是Randomsend.userGender不会更改!

您可以通过使用在对象上定义的getter和setter函数来更改对象成员的值。要使用您的示例,您将得到如下结果:

public class RandomSend extend javax.swing.JFrame {
    // This should be preferred for values that can mutate (non-final) to prevent
    // modification without the owning class being alerted the value is changing
    private static String userGender;

    public static void setUserGender(String value) {
        userGender = value;
    }

    public static String getUserGender() {
        return userGender;
    }
}
public class SingletonExample {
    private static SingletonExample instance = null;

    // Declared private to prevent new SingletonExample 
    // outside of this class
    private SingletonExample {} 

    public static SingletonExample getInstance() {
        if (instance == null) {
            instance = new SingletonExample();
        }

        return instance;
    }
}
在本例中,您可以通过调用
RandomSend.setUserGender(“male”)
来更改该值,并通过调用
RandomSend.getUserGender()
来读取该值

一些附加注释

我只是想指出一些关于您的示例我注意到的其他事情。以您自己的方式使用
静态
值不一定是最好的主意。您以错误的方式锁定了类的使用。您应该维护
用户
类的实例或管理信息的其他类型的类特定于用户的选项,如性别。通过管理实例而不是类上的静态值,您可以更轻松地处理应用程序中的其他用户(如果需要)。如果您确定您永远不需要支持比当前用户更多的用户,那么您仍然可以使用实例,但可以使用

这看起来像:

public class RandomSend extend javax.swing.JFrame {
    // This should be preferred for values that can mutate (non-final) to prevent
    // modification without the owning class being alerted the value is changing
    private static String userGender;

    public static void setUserGender(String value) {
        userGender = value;
    }

    public static String getUserGender() {
        return userGender;
    }
}
public class SingletonExample {
    private static SingletonExample instance = null;

    // Declared private to prevent new SingletonExample 
    // outside of this class
    private SingletonExample {} 

    public static SingletonExample getInstance() {
        if (instance == null) {
            instance = new SingletonExample();
        }

        return instance;
    }
}
您可以通过获取类似于
SingletonExample.getInstance()
的实例来使用此类,然后对该实例进行操作。使用此方法可以确保在项目中的所有点上,您都在访问同一对象的同一实例,从而在某种意义上实现“全局”

我想做的另一个注意事项是尝试使用
final
值,或者更好的方法是,使用
enum
而不是字符串来表示性别等最有可能用作值的内容。我这样说是因为为了正确比较性别,您必须:

if (RandomSend.userGender.equals("male")) {
    // ...
}
如果您创建了一个
Gender
类,其常量如下:

public Gender {
    public static final int MALE = 1;
    public static final int FEMALE = 2;
}
和比较(提供适当类别中的值变化)

这是一个非常好的主意,Java有一个独特的结构来提供此解决方案,称为
enum
s。您可以这样定义
Gender
enum:

public enum Gender {
    MALE,
    FEMALE;
}

然后您将
userGender
声明为
Gender
值,并且您的比较与您自己从具有常量值的类中构建枚举的情况相同。从长远来看,这些更改可以使您的项目更易于管理和维护。

欢迎使用stackoverflow。请添加您的问题和您需要解决的问题到目前为止,我已经尝试解决您的问题,因此我们知道我们可以帮助您。它对我来说很好,您可以看到示例。您的代码中存在一些问题,您应该尝试更正。我不会太多地涉及格式,因为这可能会将粘贴到此处。但是像
\u user
这样的名称对于类来说是个坏主意Java命名约定建议使用名词作为名称,这些名词代表该类是什么。对于
JFrame
的子类更是如此。更合适的名称(随意猜测)太长了,读不下去了,我会尝试你的样本,它对我很有用。