java 将值附加到 JTextField?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28940433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:26:01  来源:igfitidea点击:

Append value to JTextField?

javaswingjtextfield

提问by Shafiq Fata

I want to append a value into a JTextFieldin Java. Something like:

我想JTextField在 Java 中将一个值附加到 a中。就像是:

String btn1="1"; 
textField.appendText(btn1);

回答by Duncan Jones

I think you'll find setTextis the answer. Just combine the current value with the new value:

我想你会找到setText答案。只需将当前值与新值结合起来:

textField.setText(textField.getText() + newStringHere); 

回答by camickr

If you text field is not editable, you could use:

如果您的文本字段不可编辑,您可以使用:

textField.replaceSelection("...");

If it is editable you might use:

如果它是可编辑的,您可以使用:

textField.setCaretPosition( textField.getDocument().getLength() );
textField.replaceSelection("...");

This would be slightly more efficient (than using setText()) because you are just appending text directly to the Document and would more resemble a JTextArea.append(...).

这会稍微更有效(比使用 setText()),因为您只是将文本直接附加到 Document 并且更类似于 JTextArea.append(...)。

It would only result in a single DocumentEvent - insertUpdate().

它只会导致单个 DocumentEvent - insertUpdate()。

You can also access the Document directly and do the insert:

您还可以直接访问 Document 并进行插入:

Document doc = textField.getDocument();
doc.insertString(...);

but I find this more work because you also have to catch the BadLocationException.

但我发现这更有效,因为您还必须捕获 BadLocationException。

Simple example, that also demonstrate the use of Key Bindings:

简单的例子,也演示了键绑定的使用:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
//      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}