【问题标题】:Enter key JTextField输入键 JTextField
【发布时间】:2012-12-21 06:00:08
【问题描述】:

我有一个带有 Actionlistener 的 JTextField。现在我希望它在我按 Enter 时执行某些操作。我正在使用一个共享的 ActionListener 所以试图在 JTextField 上做一个 getSource 但它不起作用!希望任何人都可以提供帮助。

JTextField txtProductAantal = new JTextField(String.valueOf(WinkelApplication.getBasket().getProductAmount(productdelete)));
            txtProductAantal.setBounds(340, verticalPosition + i * productOffset, 40, 20);
            txtProductAantal.addActionListener(this);
            add(txtProductAantal); 

public void actionPerformed(ActionEvent event) {

        if (event.getSource() == btnEmptyBasket) {
            WinkelApplication.getBasket().empty();
            WinkelApplication.getInstance().showPanel(new view.CategoryList());
        }

        if(event.getSource() == txtProductAantal){
            String productgetal = txtProductAantal.getText();
            txtProductAantal.setText(productgetal);
            WinkelApplication.getInstance().showPanel(new view.Payment());
        }
    }

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2)txtProductAantal.setBounds(..)使用布局来避免接下来的17个问题。

标签: java swing awt actionlistener jtextfield


【解决方案1】:
  • 必须创建一个临时的Object 进行比较

例如

   public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == btnEmptyBasket) {
           //...........
        } else if (source == txtProductAantal) {
           //........... 
        } else {

        }
    }
  • 或者只有JTextFields(避免instanceofif - else语句中)你可以直接将Object转换为JTextField

JTextField source = (JTextField) event.getSource();

编辑,

  • one from the next adviced 17 problems.

  • please to read suggestion by @Andrew Thompson, again

    • 1) For better help sooner, post an SSCCE.

    • 2) txtProductAantal.setBounds(..)

    • Use layouts to avoid the next 17 problems.

我的代码按预期工作

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class JTextFieldAndActionListener implements ActionListener {

    private JFrame frm = new JFrame("JTextFieldAndActionListener");
    private JTextField one = new JTextField(10);
    private JTextField two = new JTextField();
    private JTextField three = new JTextField();

    public JTextFieldAndActionListener() {
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        frm.setLayout(new GridLayout());
        frm.add(one);
        frm.add(two);
        frm.add(three);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLocation(400, 300);
        frm.pack();
        frm.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == one) {
            System.out.println("firing from JTextField one");
        } else if (source == two) {
            System.out.println("firing from JTextField two");
        } else if (source == three) {
            System.out.println("firing from JTextField three");
        } else {
            System.out.println("something went wrong");
        }
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JTextFieldAndActionListener ie = new JTextFieldAndActionListener();
            }
        });
    }
}

在 ENTER 键上打印输出我

run:
firing from JTextField one
firing from JTextField two
firing from JTextField three
BUILD SUCCESSFUL (total time: 15 seconds)

【讨论】:

  • 还是不行。我已经在文本字段的 if 上放置了一个系统,但它对输入按钮没有反应。
  • @DaViDa :它确实工作得太好了。您可以通过查看此link 自己测试SSCCE
  • 我认为其他地方有问题,这就是它不起作用的原因,但感谢大家向我展示了这样做的好方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 2014-09-08
  • 2013-10-01
相关资源
最近更新 更多