gpt4 book ai didi

java - 当 textField 位于不同面板中时,textField.getText() 不起作用(与 button.addActionListener 相比)

转载 作者:行者123 更新时间:2023-12-01 19:54:57 24 4
gpt4 key购买 nike

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

我认为我得到空字符串的原因是 JTextField 与按钮位于不同的面板中,但不知道如何让它工作......任何帮助将不胜感激!

这是我的逻辑

(1) Create a JFrame, call it frame

(2) Create several JPanels and frame.add(JPanel)

(3) Fill in the panels with JButton and JTextField. Note that the text field is in a different panel than the button.

(4) Call button.addActionListener(...) and use 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);
}
});
}

}

最佳答案

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

private JTextField textFieldThetaCh = new JTextField();

然后将其传递给方法setTextFieldPar,并在其中创建另一个添加到面板中的文本字段:

textField = new JTextField(13);
panel.add(textField);

因此,类变量 textFieldThetaCh 不是添加到面板中的变量,因此用户无法访问。

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

这是正在发生的事情的直观表示:

  1. 在类声明中:

class declaration

  • setTextFieldPar 方法内(请记住,参数是按值传递的,因此会创建对象引用的副本):
  • setTextFieldPar method

  • textField = new JTextField(13);之后,引用的副本现在指向一个新对象:
  • after textField = new JTextField(13)

  • panel.add(textField);之后,新对象被添加到面板中,这不是textFieldThetaCh所指向的:
  • after `panel.add(textField)

    关于java - 当 textField 位于不同面板中时,textField.getText() 不起作用(与 button.addActionListener 相比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928852/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com