没有响应的JTextField

时间:2014-06-15 22:13:06

标签: java swing jtextfield keylistener

在我的应用程序中,我需要从用户那里获得1到9之间的响应。我使用JTextField来获取输入。但是,当按下按钮时,JTextField用户无法响应。以下是问题的简要示例:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;

public class InputTest {
    private JTextField inputBox;

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

    public InputTest() { // constructor
    JFrame f = new JFrame("Input Test");
    f.setBounds(100, 100, 450, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);

    JLabel label = new JLabel("Enter an integer:");
    label.setFont(new Font("Tahoma", Font.PLAIN, 14));
    label.setBounds(109, 120, 109, 30);
    f.add(label);

    inputBox = new JTextField();
    inputBox.setBounds(259, 120, 30, 30);
    f.add(inputBox);

    JButton button = new JButton("Button");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button Pressed");
        }
    });
    button.setMnemonic('B');
    button.setBounds(166, 198, 78, 23);
    f.add(button);
    f.setVisible(true);

    inputBox.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (c < '!' || e.getModifiers() > 0)
                return;
            if (c < '1' || c > '9') {
                JOptionPane.showMessageDialog(null, c
                        + " is not an integer");
                // inputBox.setText("error");
                System.out.println("You typed a non-integer");
            } else
                System.out.println("You typed " + c);
            inputBox.setText(null);
        }
    });
} // end constructor
} // end class

1 个答案:

答案 0 :(得分:2)

你几乎不应该使用带有Swing JTextComponent的KeyListener,例如JTextField,例如当用户尝试复制并粘贴其输入时会发生什么?而是考虑使用JFormattedTextField,或者使用带有DocumentFilter的JTextField,或者我的选择 - 一个JSpinner。


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

public class InputTest2 extends JPanel {
   private JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 1, 9, 1));
   private JButton getSelectionButton = new JButton(new GetSelectionAction("Get Selection"));

   public InputTest2() {
      add(spinner);
      add(getSelectionButton);
   }

   private static void createAndShowGui() {
      InputTest2 mainPanel = new InputTest2();

      JFrame frame = new JFrame("InputTest2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

   private class GetSelectionAction extends AbstractAction {
      public GetSelectionAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         int value = ((Integer)spinner.getValue()).intValue();
         System.out.println("You've selected " + value);
      }
   }

}