如何更改或隐藏JTextField,使其不向用户显示,同时仍然让用户键入密钥?我试着使用"KeyCodeT.setVisible(false);",但它不起作用。
这就是我要说的:http://oi46.tinypic.com/35hobbn.jpg
完整代码在这里:http://pastebin.com/8t3cTLKX
JTextField KeyCodeT = new JTextField("Key Code:");
public KeyProgram() {
KeyCodeT.addKeyListener(this);
KeyCodeT.setEditable(false);
add(KeyCodeT);
setSize(300, 300);
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed!!!");
if (e.getKeyCode() == 27) {
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Released!!!");
KeyCodeT.setText("Key Code:" + e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
KeyProgram key = new KeyProgram();
key.setVisible(true);
}发布于 2013-04-19 22:39:47
if (e.getKeyCode() == 27)不要使用魔术数字。阅读你的代码的人不知道那是什么。请改用:
KeyEvent.VK_ESCAPE但是,您甚至不应该为此使用KeyListener。相反,您应该使用Key Bindings。
发布于 2013-04-19 20:25:56
您可以尝试如下所示:
JTextField textField= new JTextField();
textField.setBackground(UIManager.getColor("Panel.background")); // set the text box background color to color of component behind
textField.setBorder(null); // remove the text box border如下所示:

注意:对于这个需求,这可能不是最好的方法,但它确实有效。
https://stackoverflow.com/questions/16104459
复制相似问题