如何在GUI中获取文本字段中的值?JAVA

如何在GUI中获取文本字段中的值?JAVA,java,swing,Java,Swing,我还想使输入动态,以便在我按下任何键时更改值。谢谢,我不知道如何使用keylistener或处理我的代码的东西 public static void DecimalToAll(String varInput){ //DeciToHexa int varDeciToHexa = Integer.parseInt(varInput); String DeciToHexaAnswer = Integer.toHexString(varDeciToHexa); Syste

我还想使输入动态,以便在我按下任何键时更改值。谢谢,我不知道如何使用keylistener或处理我的代码的东西

public static void DecimalToAll(String varInput){
    //DeciToHexa
    int varDeciToHexa = Integer.parseInt(varInput);
    String DeciToHexaAnswer = Integer.toHexString(varDeciToHexa);
    System.out.println(DeciToHexaAnswer.toUpperCase());
    //DeciToOctal
    int varDeciToOctal = Integer.parseInt(varInput);
    String DeciToOctalAnswer = Integer.toOctalString(varDeciToOctal);
    System.out.println(DeciToOctalAnswer);
    //DeciToBinary
    int varDeciToBinary = Integer.parseInt(varInput);
    String DeciToBinaryAnswer = Integer.toBinaryString(varDeciToBinary);
    System.out.println(DeciToBinaryAnswer);

使用DocumentListener的示例:


JTextField不能像其他组件一样添加“ChangeListener”。要在
JTextField
中“监视”更改,您可以在textfield中添加DocumentListener

private class MyDocumentListener implements DocumentListener
{
    public void changedUpdate(DocumentEvent e){
        //Do nothing
    }
    public void insertUpdate(DocumentEvent e){
        //Do things when text are inserted
    }
    public void removeUpdate(DocumentEvent e){
        //Do things when text are deleted
    }
}
要添加DocumentListener,请从
JTextField
获取
文档
对象并添加它:

JTextField txt = new JTextFeld();
txt.getDocument().addDocumentListener(new MyDocumentListener());

myInputField.getText()?我如何监控它,以便每当它更改值时,我的程序都会自动处理它。我正在GUI中使用按钮。方法中发生的事情将保留在方法中。在调用方法之前,这些变量甚至不存在。鉴于您没有显示任何其他代码,也没有说明您正试图执行的操作,我所能做的就是现在的用法是:当您运行该方法时(我想是在单击按钮时)将getText结果的解析版本作为参数传递给method我如何触发一个事件,在更改textfield中的值时,它将再次调用该方法。感谢您的回答向textfield添加一个侦听器来执行此操作。@PatrickGomez您可以通过单击我的答案旁边空心的勾号来接受我的解决方案。您将得到2个答案计算点数作为回报。