Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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_User Interface_Variables_Scope - Fatal编程技术网

Java 变量";“用户问题”;没有值。我试图从另一个类访问它,但它没有返回任何内容

Java 变量";“用户问题”;没有值。我试图从另一个类访问它,但它没有返回任何内容,java,user-interface,variables,scope,Java,User Interface,Variables,Scope,我在其中声明和设置变量的类。 每次用户激活按钮时,都会设置变量userQuestion。这样做的目的是为了在下一节课上用它来重新研究这个问题。但是,似乎没有设置userQuestion变量。抱歉,代码太乱了,它是在Eclipse中使用WindowBuilder创建的 我试图访问变量的类。 在某些听众中,你并没有真正把问题放在框架中: btnWhatIsYour_1.addActionListener(new ActionListener() { public void actionP

我在其中声明和设置变量的类。 每次用户激活按钮时,都会设置变量userQuestion。这样做的目的是为了在下一节课上用它来重新研究这个问题。但是,似乎没有设置userQuestion变量。抱歉,代码太乱了,它是在Eclipse中使用WindowBuilder创建的

我试图访问变量的类。


在某些听众中,你并没有真正把问题放在框架中:

btnWhatIsYour_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        input frame = new input();
        frame.setVisible(true);
        String userQuestion = "What is your favorite book?";
        // Missing setter
    }
});

如果您将代码完全粘贴到问题中,效果会更好。没有人想阅读你的pastebin链接。更好的是,不要粘贴所有的代码-粘贴a。(现在也是学习并开始遵循Java命名约定的好时机。)我会使用什么方法?我会在JFrame或ActionListener中使用方法吗?frame.SetUserQuestion(userQuestion);正如您在第一个listenerI中所做的那样,我尝试了这一点,但在打开的新窗口中仍然返回null。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JPasswordField;
import java.awt.Insets;

public class input extends JFrame {
private JTextField txtWhatIsThe;

/**
 * Launch the application.
 */ 
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                input frame = new input();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
String userQuestion;

public void SetUserQuestion(String question)
{
    this.userQuestion = question;
}

public input() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(null);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
    getContentPane().setLayout(gridBagLayout);

    txtWhatIsThe = new JTextField();
    txtWhatIsThe.setText(userQuestion );
    GridBagConstraints gbc_txtWhatIsThe = new GridBagConstraints();
    gbc_txtWhatIsThe.insets = new Insets(0, 0, 5, 0);
    gbc_txtWhatIsThe.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtWhatIsThe.gridx = 0;
    gbc_txtWhatIsThe.gridy = 0;
    getContentPane().add(txtWhatIsThe, gbc_txtWhatIsThe);
    txtWhatIsThe.setColumns(10);
}

}
btnWhatIsYour_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        input frame = new input();
        frame.setVisible(true);
        String userQuestion = "What is your favorite book?";
        // Missing setter
    }
});