2014-06-15 34 views
0

在我的應用程序中,我需要從用戶獲得1和9之間的響應。我正在使用JTextField來獲得輸入。然而,當按下按鈕時,用戶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 
+0

Java的圖形用戶界面可能需要在多個平臺上工作,在不同的屏幕分辨率和使用不同的PLAFs。因此,它們不利於組件的準確放置。爲了組織強大的GUI,請改爲[使用佈局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)或[它們的組合](http:// stackoverflow.com/a/5630271/418556),以及[white space]的佈局填充和邊框(http://stackoverflow.com/q/17874717/418556)。 –

回答

2

當用戶試圖複製並在他們輸入粘貼您應該幾乎從來不使用帶鞦韆的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); 
     } 
    } 

}