jbuttons和具有动作侦听器的jtextfield [英] jbuttons and a jtextfield that has an action listener

查看:84
本文介绍了jbuttons和具有动作侦听器的jtextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由jbuttons和一个jtextfield组成的小键盘,它带有一个动作侦听器.当我按下按钮时,数字将显示在文本字段中,但下一个数字将覆盖它.谁能告诉我如何将文本附加到长度为13的数字上,以及何时将其附加到回车符中.

如果我使用键盘输入数字,则可以输入数字字符串,但不能从按钮输入数字.

我正在使用:

    JButton buttonNo2 = new JButton("2");
    buttonNo2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textfield.setText("2");
        }

buttonNo1.setBounds(11, 18, 50, 50);
    keyboardPanel.add(buttonNo2);
    buttonNo1.setForeground(Color.BLUE);
    buttonNo1.setFont(new Font("Perpetua", Font.BOLD, 20));

解决方案

尝试使用类似的东西

textfield.setText(textfield.getText() + "2");

而不是textfield.setText("2");

setText就是这样,将文本字段的文本设置为您指定的值.

buttonNo1.setBounds(11, 18, 50, 50);看起来像您在没有布局管理器的情况下尝试执行的操作.避免使用null布局,像素完美布局是现代ui设计中的一种幻觉.有太多因素会影响组件的单个大小,您无法控制. Swing旨在与布局管理者为核心一起工作,舍弃这些问题不会导致任何问题,而您将花费越来越多的时间进行纠正

您也可以自己简化它,而使用Action API,这样可以节省很多重复输入的时间...

public class NumberAction extends AbstractAction {

    private JTextField field;
    private int number;

    public NumberAction(JTextField field, int number) {
        this.field = field;
        this.number = number;
        putValue(NAME, Integer.toString(number));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Document doc = field.getDocument();
        try {
            doc.insertString(doc.getLength(), Integer.toString(number), null);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

}

然后,您只需要根据需要添加每个按钮...

add(new JButton(new NumberAction(textfield, 1)));
add(new JButton(new NumberAction(textfield, 2)));
add(new JButton(new NumberAction(textfield, 3)));

有关更多详细信息,请参见如何使用操作 >

I have a keypad made up of jbuttons and a jtextfield that has an action listener. When I press the button the number shows in the textfield but the next number overwrites it. Could anyone tell me how to append the text to the length 13 numbers and when it get's there to carriage return.

If I use the keyboard to enter numbers I can enter a String of numbers but not from the buttons.

I am using:

    JButton buttonNo2 = new JButton("2");
    buttonNo2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textfield.setText("2");
        }

buttonNo1.setBounds(11, 18, 50, 50);
    keyboardPanel.add(buttonNo2);
    buttonNo1.setForeground(Color.BLUE);
    buttonNo1.setFont(new Font("Perpetua", Font.BOLD, 20));

解决方案

Try using something like

textfield.setText(textfield.getText() + "2");

instead of textfield.setText("2");

setText does just that, sets the text of the text field to the value you specified.

Also buttonNo1.setBounds(11, 18, 50, 50); looks like you're trying to do without a layout manager. Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

You could also make it much simpler for yourself, and use the Action API instead, which would save you a not lot of repeated typing...

public class NumberAction extends AbstractAction {

    private JTextField field;
    private int number;

    public NumberAction(JTextField field, int number) {
        this.field = field;
        this.number = number;
        putValue(NAME, Integer.toString(number));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Document doc = field.getDocument();
        try {
            doc.insertString(doc.getLength(), Integer.toString(number), null);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

}

Then you would just need to add each button as required...

add(new JButton(new NumberAction(textfield, 1)));
add(new JButton(new NumberAction(textfield, 2)));
add(new JButton(new NumberAction(textfield, 3)));

See How to Use Actions for more details

这篇关于jbuttons和具有动作侦听器的jtextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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