簡體   English   中英

如何在 GUI 中的文本字段中獲取值? 爪哇

[英]How do i get the value in the textfield in GUI? java

我還想讓輸入動態化,以便在我按任意鍵時更改值。 謝謝我不知道如何使用密鑰偵聽器或可以處理我的代碼的東西。

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的更改,您可以將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獲取Document對象並添加它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM