Java 如何将变量从Gui表单类保存到另一个类

Java 如何将变量从Gui表单类保存到另一个类,java,swing,Java,Swing,我正在开发一个有gui的程序。用户输入一个数字,动作监听器将该数字保存在一个变量中,然后将该变量发送到一个类main,在该类main中该变量存储在另一个变量中。问题是,我目前的做法是没有正确保存值(有时vaue保存,有时不保存)。我希望当用户在gui中输入值,然后按下按钮时,该值会保存在主类中的变量中。我对java非常陌生,请原谅我糟糕的编码。 这是我的主课 public class Main { public static boolean Click=false; public sta

我正在开发一个有gui的程序。用户输入一个数字,动作监听器将该数字保存在一个变量中,然后将该变量发送到一个类main,在该类main中该变量存储在另一个变量中。问题是,我目前的做法是没有正确保存值(有时vaue保存,有时不保存)。我希望当用户在gui中输入值,然后按下按钮时,该值会保存在主类中的变量中。我对java非常陌生,请原谅我糟糕的编码。 这是我的主课

    public class Main {
public static boolean Click=false;
public static void main(String[] args) {
    int n=0, i;
    JFrame Frame = new JFrame();
    Input1 I1 = new Input1();
    Frame.setSize(600, 600);
    Frame.setLocationRelativeTo(null);
    Frame.setContentPane(I1.Input1Panel);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.pack();
    Frame.setVisible(true);
    while (Click==false) {
        n = I1.Setn();
        System.out.println(""+n);
    }
这是我的gui类

    public class Input1 {
private JButton doneButton;
private JTextField textField1;
private JLabel Title;
private JLabel EnterNum;
public JPanel Input1Panel;

public int n;

Main M = new Main();

public Input1() {
    doneButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            n = Integer.parseInt(textField1.getText());
            while (M.Click==false) {
                if (n==0) {
                    n = Integer.parseInt(textField1.getText());
                }
                else {
                    M.Click=true;
                }
            }
        }
    });
}
public int Setn() {
    return n;
}
}

新代码 主类

    public class Main {
public static void main(String[] args) {
    int n=0, i;
    JFrame Frame = new JFrame();
    Input1 I1 = new Input1();
    Frame.setSize(600, 600);
    Frame.setLocationRelativeTo(null);
    Frame.setContentPane(I1.Input1Panel);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.pack();
    Frame.setVisible(true);
    while (n==0) {
        n = I1.Setn();
        //System.out.println(""+n);
    }
    System.out.println("Main:"+n);
GUI类

    public class Input1 {
private JButton doneButton;
private JTextField textField1;
private JLabel Title;
private JLabel EnterNum;
public JPanel Input1Panel;

public int n;

Main M = new Main();

public Input1() {
    doneButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            n = Integer.parseInt(textField1.getText());
            System.out.println("Action Listener:"+n);
        }
    });
}
public int Setn() {
    return n;
}

}

看起来你把事情复杂化了一点。另外,请阅读@pvg did的评论。你没有准确地解释问题。此外,你的代码的方式,我无法理解你到底想做什么

如果您想从文本字段中获取输入,并使用以下操作:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        // Swing applications must run on EDT thread. Use this method to achieve that.
        EventQueue.invokeLater(() -> {
            try {
                Test window = new Test();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * Create the application.
     */
    public Test() {
        frame = new JFrame();
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        addComponents();
        frame.pack();
    }

    private void addComponents() {
        JTextField txtInput = new JTextField("Input: ");
        txtInput.setEditable(false);
        frame.getContentPane().add(txtInput);
        JTextField inputField = new JTextField();
        inputField.setColumns(10);
        frame.getContentPane().add(inputField);
        JButton getInputBtn = new JButton("Click me to give the input");
        getInputBtn.addActionListener(e -> {
            String input = inputField.getText(); 
            System.out.println("The input is: " + input); //Do something with the input.
        });
        frame.getContentPane().add(getInputBtn);
    }

}
我看到你从输入中解析了一个整数。这个,你处理它的方式,可以给你一个很好的解释。我建议你阅读以避免这种情况


附言:在你的帖子中添加“swing”标签。

经过进一步研究,我找到了一个解决方案,我所需要做的就是在声明时将“volatile”加上n。有关说明,请访问此网站。 我的代码正在更新

    volatile public static int n=0;
public static void main(String[] args) {

    int i=0;

    JFrame Frame = new JFrame();
    Input1 I1 = new Input1();
    Input2 I2 = new Input2();

    Frame.setSize(600, 600);
    Frame.setLocationRelativeTo(null);
    Frame.setContentPane(I1.Input1Panel);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.pack();
    Frame.setVisible(true);
    while (n==0) {
        n = I1.Setn();
    }

你应该看一看和。清楚、重复地阐述问题的过程通常足以让你自己回答,但如果回答不清楚,其他人会更容易帮助你。你需要重新思考你的整个代码。例如a
while(true)
将在线性控制台程序中工作,但在事件驱动GUI中没有位置,因为这样的代码可能会占用GUI的事件线程,冻结程序,即使它没有这样做,它也不会像您认为的那样工作。更好的做法是响应事件。此外,创建Main的实例(一个cl)也不会带来任何好处ass只不过是一个带有静态字段的静态主方法。相反,创建带有非静态字段(state)和方法(behavior)的体面类,并将它们绑定在一起。我的程序做了一些更改。让我重新表述我的问题。我现在从动作侦听器获得了输入。我想将此输入发送到我使用Setn()执行的主类。在主循环中,我创建了一个while循环,以便在用户输入一个数字之前,程序不会进一步执行。问题是,我的方法Setn似乎不会在主类中设置值,除非我在循环中放入print语句。如果我不放入print语句,该值将保留在gui类中,并且不会在主类中热进入。我有添加了新代码。