问题
I'm trying to make a program that can converts fahrenheit to celcius in java. In program i have 2 Labels and 1 TextField for input. I want to make convert temperature when user types the temperature and presses Enter. To do that, i added a key listener to my textfield but it doesn't work. When i press Enter listener don't fire at all.
And here's my code.
public class TempConv extends JFrame{
private JLabel info;
private JLabel result;
private JTextField input;
private String outcome;
public TempConv(){
super("Temperature Converter");
setLayout(new BorderLayout());
info = new JLabel("Enter Fahrenheit Temperature");
add(info, BorderLayout.NORTH);
input = new JTextField(12);
add(input, BorderLayout.CENTER);
result = new JLabel("Temperature in Celcius is: " + outcome);
add(result, BorderLayout.SOUTH);
input.addKeyListener(
new KeyListener(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == KeyEvent.VK_ENTER){
outcome = input.getText();
}
}
}
);
}
public static void main(String[] args) {
TempConv ftc = new TempConv();
ftc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ftc.setLocationRelativeTo(null);
ftc.setSize(370, 100);
ftc.setVisible(true);
}
}
Edit: It works with ActionListener but i need to do it with anonymous class. Without anonymous class it fires with Enter.
回答1:
Try e.getKeyCode() instead of e.getKeyChar(). The constant KeyEvent.VK_ENTER is an int, not a char.
In other words:
if(e.getKeyCode() == KeyEvent.VK_ENTER){
outcome = input.getText();
}
instead of
if(e.getKeyChar() == KeyEvent.VK_ENTER){
outcome = input.getText();
}
回答2:
Late answer, but I tried with the code in the question and the KeyPressed did trigger, but the because the JLabel didn't update, you assumed the KeyEvent wasn't fired.
Just after
outcome = input.getText();
add
result.setText("Temperature in Celcius is: " + outcome);
so the label will update itself.
回答3:
Buttons don't need a KeyListener
(and for the most part shouldn't use them), they use an ActionListener
to respond to all activation events, including Enter, mouse clicks and keyboard shortcuts, it's a much more simplified API.
See How to Use Buttons, Check Boxes, and Radio Buttons, How to Write an Action Listeners and How to Use Actions for more details
You can also set a button as the "default" button which can be activated when not focused (so long as the currently focused component doesn't use/consume the Enter key)
See JRootPane#setDefaultButton and How to Use Root Panes for more details
回答4:
First of all, you need to implement all the methods from KeyListener. You haven't implemented keyTyped and keyReleased. Another thing is you should check the key code instead of the key char because the "Enter" char is not visible, so preferably you should check if the key code equals KeyEvent.VK_ENTER. The last thing is when you hit enter you update the outcome String variable but you doesn't show it anywhere, so you need to set the text on the result JLabel. You also forgot to make the conversion. My explanation could be confusing but below is the code:
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TempConv extends JFrame{
private JLabel info;
private JLabel result;
private JTextField input;
private String outcome;
public TempConv(){
super("Temperature Converter");
setLayout(new BorderLayout());
info = new JLabel("Enter Fahrenheit Temperature");
add(info, BorderLayout.NORTH);
input = new JTextField(12);
add(input, BorderLayout.CENTER);
result = new JLabel("Temperature in Celcius is: " + outcome);
add(result, BorderLayout.SOUTH);
input.addKeyListener(
new KeyListener(){
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
outcome = input.getText();
double celsius = (((Double.valueOf(outcome)) - 32) * 5 / 9 );
result.setText("Temperature in Celcius is: " + celsius);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
);
}
public static void main(String[] args) {
TempConv ftc = new TempConv();
ftc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ftc.setLocationRelativeTo(null);
ftc.setSize(370, 100);
ftc.setVisible(true);
}
}
来源:https://stackoverflow.com/questions/14309510/keylistener-in-textfield-not-firing-when-press-enter