그래서 내 텍스트 필드에 입력되는 내용이 숫자이며 영숫자가 아닌지 확인하려고합니다. 형식화 된 텍스트 상자를 살펴 보았지만이를 구현할 수 없었습니다. 자습서 오라클에 대한 이해가 부족하기 때문에 사이트에 있습니다. 좀 더 검색을하고 핵심 수신기 클래스를 찾았으며 최상의 옵션처럼 보였습니다.키 수신기를 사용하여 텍스트 필드에 입력을 확인하는 방법
그래서 프로그램을 실행하고 문자 이벤트를 사용하는 대신 텍스트 필드에 입력하면 경고음이 울립니다. 여기에 코드입니다 : 여기
private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a time in seconds the object has fallen");
// Create a text field 10 characters wide
fallingTextField = new JTextField(10);
// Add a keylistener to the text field
fallingTextField.addKeyListener(new TextFieldListener());
// Create a button with the caption "Calculate"
calcButton = new JButton("Calcualte");
// Add an action listener to the button
calcButton.addActionListener(new CalcButtonListener());
//Create a JPanel object and let the panel field reference it
panel = new JPanel();
// Add the label, text field, and button components to the panel
panel.add(messageLabel);
panel.add(fallingTextField);
panel.add(calcButton);
}
/**
The TextFieldListener class checks to see if a valid key input is typed into
the text field.
*/
private class TextFieldListener implements KeyListener
{
/**
The keyPressed method
@param evt is a key event that verifies a number is inputed otherwise
the event is consumed.
*/
public void keyPressed(KeyEvent evt)
{
char c = evt.getKeyChar();
if(Character.isAlphabetic(c))
{
getToolkit().beep();
evt.consume();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent ev)
{
}
}
돈 당신처럼, 마지막 문자 않고 필드의 값을 설정해야 'KeyListener'를 사용하지 말고'KeyListener'를 사용하지 말고 대신'DocumentFilter'를 사용하십시오. – MadProgrammer
을위한 것입니다. 포맷터가 작동하지 않으면 ... 그걸 포기하지 마라. 그 것을 사용하는 법을 계속 읽고 배우십시오. 아시다시피, 문제가 발생하여 포기하기로 결정했습니다. 그러지 마. ** 퍼즐을 풀어 ** 프로그래밍을 배웁니다. 다른 하나를 교환하지 않음 ... – GhostCat
사용자가 텍스트를 필드에 붙여 넣으면 어떻게됩니까? (힌트 없음) – MadProgrammer