“附加"JTextField 的文本 [英] "append" text for JTextField

查看:28
本文介绍了“附加"JTextField 的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小型应用程序,其中 U 输入 ID(在 JButton 的帮助下 - 从 0 到 9 - ),然后将按下的数字传递给 putText-Method,然后将其显示在 JTextField 中 - 问题是每次我按下新号码时,我在消失之前按下的号码:s .有人可以帮我解决这个问题吗?

I'm trying to make a small application where U input ID (with help of JButton -from 0 to 9 - ) and then pass the number that have been pressed to putText-Method and then display it in JTextField-the problem is that every time I press new number the one I pressed before disapears:s . Could anyone help me with that pls?

public class IdPanel extends JPanel {

    private JTextField idField;
    private JLabel idLabel;

    public IdPanel() {

        setLayout(new GridBagLayout());

        setPreferredSize(new Dimension(500, 70));

        idField = new JTextField(20);
        idField.setFont(new Font("Serif", Font.PLAIN, 20));

        idLabel = new JLabel("Enter ID:");
        idLabel.setFont(new Font("Serif", Font.PLAIN, 20));

        GridBagConstraints gc = new GridBagConstraints();

        gc.gridx = 0;
        gc.gridy = 0;
        gc.insets = new Insets(9, 9, 9, 9);
        gc.anchor = GridBagConstraints.CENTER;
        add(idLabel, gc);

        gc.gridx = 1;
        gc.anchor = GridBagConstraints.CENTER;
        add(idField, gc);
    }

    public void putText(String number) {
        idField.setText(number);
    }
}

推荐答案

尝试:

idField.setText(idField.getText()+number);

这篇关于“附加"JTextField 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆