Java 当textField位于不同的面板中时,textField.getText()不工作(与button.addActionListener比较)

Java 当textField位于不同的面板中时,textField.getText()不工作(与button.addActionListener比较),java,swing,Java,Swing,我是JavaSwing新手,在使用JTextField.getText()时遇到了一个问题。基本上,getText()不会拾取我输入到文本字段中的任何字符串并返回空字符串 我认为我得到一个空字符串的原因是JTextField与按钮位于不同的面板中,但不知道如何使其工作。。。任何帮助都将不胜感激 这是我的逻辑 (1) 创建一个JFrame,称之为frame (2) 创建几个JPanel和frame.add(JPanel) (3) 用JButton和JTextField填充面板。请注意,文本字段与按

我是JavaSwing新手,在使用
JTextField.getText()
时遇到了一个问题。基本上,getText()不会拾取我输入到文本字段中的任何字符串并返回空字符串

我认为我得到一个空字符串的原因是JTextField与按钮位于不同的面板中,但不知道如何使其工作。。。任何帮助都将不胜感激

这是我的逻辑

(1) 创建一个JFrame,称之为frame

(2) 创建几个JPanel和frame.add(JPanel)

(3) 用JButton和JTextField填充面板。请注意,文本字段与按钮位于不同的面板中

(4) 调用button.addActionListener(…)并使用JTextField.getText()

这是我的密码:

package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class aaaaa {

// Class attributes
// Overall class attributes
private JFrame frame = new JFrame("Simulation App");
    
// Class attributes for method setTextFieldPar
private JPanel panelThetaCh = new JPanel();
private JPanel panelSetButton = new JPanel();

private JTextField textFieldThetaCh = new JTextField();

private String StringThetaCh;

// Class attributes for method setButton
private JButton buttonSetPar;

// ========================================================================================================================
// Class methods

// Text field of all simulation parameters
public void setTextFieldPar(JPanel panel, JTextField textField, String latexString){
    // Panel layout - FlowLayout
    panel.setLayout(new FlowLayout());
    panel.setMinimumSize(new Dimension(300, 100));
    frame.add(panel);
    panel.setAlignmentX(Component.CENTER_ALIGNMENT);
    
    JLabel labelText = new JLabel("text");
    panel.add(labelText);
    
    // Create text field        
    textField = new JTextField(13);
    panel.add(textField);
}


// Button "Set Parameters"
public void setButton (JPanel panel){
    panel.setLayout(new GridLayout(4, 0));
    panel.setMaximumSize(new Dimension(200, 100));
    frame.add(panel);
    panel.setAlignmentX(Component.CENTER_ALIGNMENT);
    
    buttonSetPar = new JButton("Set Parameters");
    panel.add(buttonSetPar);        
}   


// Monitor input in text field
public void monitorTextField() {
    buttonSetPar.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent e) {
            // Extract numbers entered in text field for the parameters
            StringThetaCh = textFieldThetaCh.getText();
                
            if (StringThetaCh.equals("")) {
                JFrame errorWindow = new JFrame("Error");
                errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                errorWindow.setLocationRelativeTo(null);
                
                JOptionPane.showMessageDialog(errorWindow, "At least one text field is empty, please enter numerical values");
            }
        }
    });
}


// Constructor
public aaaaa(){
    frame.setSize(350, 800);
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    
    // Text field for parameters
    setTextFieldPar(panelThetaCh, textFieldThetaCh, "\\theta_{CH}");

    // Button for set parameter
    setButton(panelSetButton);
    
    // Monitoring input in text field
    monitorTextField();
    
}


public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {    
        public void run() {    
            aaaaa window2 = new aaaaa();
            window2.frame.setVisible(true);
        } 
    });
}

}

您执行的按钮操作位于构造函数中的monitorTextField()中。它在开始时被激发,但之后从未被调用(参考monitorTextField()。
将执行的操作放在函数之外的某个位置,以便它可以在按下按钮时运行,并且应该可以工作。

您的按钮操作执行在构造函数中的monitorTextField()中。它在开始时被激发,但之后从未被调用(参考monitorTextField()。
将执行的操作放在功能之外的某个位置,以便它可以在按下按钮的任何时候运行,并且应该可以工作。

我只使用文本字段,而不将其作为pram发送 当您在textField上键入文本时,您正在从textField中获取文本

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class aaaaa {

// Class attributes
// Overall class attributes
private JFrame frame = new JFrame("Simulation App");

// Class attributes for method setTextFieldPar
private JPanel panelThetaCh = new JPanel();
private JPanel panelSetButton = new JPanel();

private JTextField textFieldThetaCh = new JTextField(13);

private String StringThetaCh;

// Class attributes for method setButton
private JButton buttonSetPar;



// Text field of all simulation parameters
public void setTextFieldPar(JPanel panel, JTextField textField, String         
latexString){
// Panel layout - FlowLayout
panel.setLayout(new FlowLayout());
panel.setMinimumSize(new Dimension(300, 100));
frame.add(panel);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);

JLabel labelText = new JLabel("text");
panel.add(labelText);

// Create text field
// textField = new JTextField(13);

panel.add(textFieldThetaCh);
}


// Button "Set Parameters"
public void setButton (JPanel panel){
panel.setLayout(new GridLayout(4, 0));
panel.setMaximumSize(new Dimension(200, 100));
frame.add(panel);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);

buttonSetPar = new JButton("Set Parameters");
panel.add(buttonSetPar);
}


// Monitor input in text field
public void monitorTextField() {
buttonSetPar.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e) {
        // Extract numbers entered in text field for the parameters
        StringThetaCh = textFieldThetaCh.getText();

        if (StringThetaCh.equals("")) {
            JFrame errorWindow = new JFrame("Error");
            errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            errorWindow.setLocationRelativeTo(null);

            JOptionPane.showMessageDialog(errorWindow, "At least one text 
field is empty, please enter numerical values");
        }
        else
                          JOptionPane.showMessageDialog(null, 
StringThetaCh);

    }
});
}


// Constructor
public aaaaa(){
frame.setSize(350, 800);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

// Text field for parameters
setTextFieldPar(panelThetaCh, textFieldThetaCh, "\\theta_{CH}");

// Button for set parameter
setButton(panelSetButton);

// Monitoring input in text field
monitorTextField();

}


public static void main(String args[]) {
 EventQueue.invokeLater(new Runnable() {
    public void run() {
        aaaaa window2 = new aaaaa();
        window2.frame.setVisible(true);
    }
});
}

}

我只是直接使用文本字段,而没有将其作为婴儿车发送 当您在textField上键入文本时,您正在从textField中获取文本

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class aaaaa {

// Class attributes
// Overall class attributes
private JFrame frame = new JFrame("Simulation App");

// Class attributes for method setTextFieldPar
private JPanel panelThetaCh = new JPanel();
private JPanel panelSetButton = new JPanel();

private JTextField textFieldThetaCh = new JTextField(13);

private String StringThetaCh;

// Class attributes for method setButton
private JButton buttonSetPar;



// Text field of all simulation parameters
public void setTextFieldPar(JPanel panel, JTextField textField, String         
latexString){
// Panel layout - FlowLayout
panel.setLayout(new FlowLayout());
panel.setMinimumSize(new Dimension(300, 100));
frame.add(panel);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);

JLabel labelText = new JLabel("text");
panel.add(labelText);

// Create text field
// textField = new JTextField(13);

panel.add(textFieldThetaCh);
}


// Button "Set Parameters"
public void setButton (JPanel panel){
panel.setLayout(new GridLayout(4, 0));
panel.setMaximumSize(new Dimension(200, 100));
frame.add(panel);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);

buttonSetPar = new JButton("Set Parameters");
panel.add(buttonSetPar);
}


// Monitor input in text field
public void monitorTextField() {
buttonSetPar.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e) {
        // Extract numbers entered in text field for the parameters
        StringThetaCh = textFieldThetaCh.getText();

        if (StringThetaCh.equals("")) {
            JFrame errorWindow = new JFrame("Error");
            errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            errorWindow.setLocationRelativeTo(null);

            JOptionPane.showMessageDialog(errorWindow, "At least one text 
field is empty, please enter numerical values");
        }
        else
                          JOptionPane.showMessageDialog(null, 
StringThetaCh);

    }
});
}


// Constructor
public aaaaa(){
frame.setSize(350, 800);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

// Text field for parameters
setTextFieldPar(panelThetaCh, textFieldThetaCh, "\\theta_{CH}");

// Button for set parameter
setButton(panelSetButton);

// Monitoring input in text field
monitorTextField();

}


public static void main(String args[]) {
 EventQueue.invokeLater(new Runnable() {
    public void run() {
        aaaaa window2 = new aaaaa();
        window2.frame.setVisible(true);
    }
});
}

}您已经在类声明中创建了文本字段:

private JTextField textFieldThetaCh = new JTextField();
然后将其传递给方法
setTextFieldPar
,并在其中创建另一个添加到面板的文本字段:

textField = new JTextField(13);
panel.add(textField);
因此,类变量
textfieldtheach
不是添加到面板的变量,因此用户无法访问

只需在
setTextFieldPar
中删除新文本字段的创建即可

下面是正在发生的事情的视觉表示:

  • 在类别声明中:
  • setTextFieldPar
    方法内部(请记住,参数是按值传递的,因此会生成对象引用的副本):
  • textField=newjtextfield(13)之后,引用的副本现在指向一个新对象:
  • 面板后添加(textField),新对象将添加到面板中,而不是
    textfield标签所指向的:

  • 您已经在类声明中创建了文本字段:

    private JTextField textFieldThetaCh = new JTextField();
    
    然后将其传递给方法
    setTextFieldPar
    ,并在其中创建另一个添加到面板的文本字段:

    textField = new JTextField(13);
    panel.add(textField);
    
    因此,类变量
    textfieldtheach
    不是添加到面板的变量,因此用户无法访问

    只需在
    setTextFieldPar
    中删除新文本字段的创建即可

    下面是正在发生的事情的视觉表示:

  • 在类别声明中:
  • setTextFieldPar
    方法内部(请记住,参数是按值传递的,因此会生成对象引用的副本):
  • textField=newjtextfield(13)之后,引用的副本现在指向一个新对象:
  • 面板后添加(textField),新对象将添加到面板中,而不是
    textfield标签所指向的:

  • 回答得很好,洛里斯!非常感谢!回答得很好,洛里斯!非常感谢!