Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否将值附加到JTextField?_Java_Swing_Jtextfield - Fatal编程技术网

Java 是否将值附加到JTextField?

Java 是否将值附加到JTextField?,java,swing,jtextfield,Java,Swing,Jtextfield,我想在Java中的JTextField中添加一个值。比如: String btn1="1"; textField.appendText(btn1); 我想你会发现setText就是答案。只需将当前值与新值组合即可: textField.setText(textField.getText() + newStringHere); 如果文本字段不可编辑,则可以使用: textField.replaceSelection("..."); 如果它是可编辑的,您可以使用: textField.se

我想在Java中的
JTextField
中添加一个值。比如:

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

我想你会发现
setText
就是答案。只需将当前值与新值组合即可:

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

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

textField.replaceSelection("...");
如果它是可编辑的,您可以使用:

textField.setCaretPosition( textField.getDocument().getLength() );
textField.replaceSelection("...");
这将比使用setText()稍微高效一些,因为您只是将文本直接附加到文档中,更像JTextArea.append(…)

它只会产生一个DocumentEvent-insertUpdate()

您还可以直接访问文档并执行插入操作:

Document doc = textField.getDocument();
doc.insertString(...);
但我发现这需要更多的工作,因为您还必须捕获BadLocationException

一个简单的示例,也演示了密钥绑定的使用:

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();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类计算器面板扩展了JPanel
{
专用JTextField显示;
公共计算器面板()
{
Action numberAction=new AbstractAction()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
display.replaceSelection(例如getActionCommand());
}
};
setLayout(新的BorderLayout());
display=新的JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
添加(显示,BorderLayout.NORTH);
JPanel buttonPanel=新的JPanel();
setLayout(新的GridLayout(0,5));
添加(按钮面板、边框布局、中心);
对于(int i=0;i<10;i++)
{
String text=String.valueOf(i);
JButton按钮=新JButton(文本);
addActionListener(numberAction);
button.setOrder(新行边框(颜色为黑色));
按钮。设置首选尺寸(新尺寸(50,50));
按钮面板。添加(按钮);
InputMap InputMap=button.getInputMap(JComponent.WHEN_IN_FOCUSED_窗口);
inputMap.put(击键.getKeyStroke(文本),文本);
inputMap.put(KeyStroke.getKeyStroke(“NUMPAD”+text),text);
button.getActionMap().put(文本、数字操作);
}
}
私有静态void createAndShowUI()
{
//UIManager.put(“按钮边距”,新插图(10,10,10,10));
JFrame=新的JFrame(“计算器面板”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新计算器面板());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowUI();
}
});
}
}

是,
setText(…)
!这将导致多个DocumentEvents-removeUpdate()和insertUpdate(),这可能是问题,也可能不是问题。无论如何,它并不是严格意义上的JTextArea.append()。@camickr感谢您提供的信息。我给了你一个+1,因为你的回答似乎很优雅。