javajframekeylistener

I want to trigger button not only with click but with enter


I made a "game" with an interface where you could guess a random number. You have to click a button to submit your guess and to get an answer from the console (I use eclipse). But now I want to trigger the button by clicking enter. Here is the code of my Program (I know I don't need this many imports but I tried a lot of stuff):

import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.util.Scanner;
   public class UIZahlRaten{

    static int meineZahl = ThreadLocalRandom.current().nextInt(100);
    static int AnzVersuche = 0;
    static JLabel text = new JLabel ("Bitte geben sie eine Zahl ein: ");
    
    public static void main(String[] args) {
         
        openUI();
        
    }
    
    
    
    public static void openUI() {
        
        JFrame frame = new JFrame ("Rate die Zahl");
        frame.setSize(370, 140);
        frame.setLocation(500, 300);
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JTextField textField = new JTextField();
        textField.setBounds(8,50,237,30);
        
        JButton button = new JButton("Abschicken");
        button.setBounds(247, 50, 101, 29);
        
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                 int zahl =  Integer.parseInt(text);
                 AnzVersuche++;
                 raten(zahl);
                 
            }
        });
        text.setBounds(7,20,200,20);
        
        
        frame.add(button);
        frame.add(text);
        frame.setLayout(null);
        frame.add(textField);
        frame.setVisible(true);
        
        
    }

    
    public static void naechsteRunde() {
        AnzVersuche++;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Bitte geben sie eine Zahl ein: ");
        int zahl = scanner.nextInt();
        raten(zahl);
    }
    
    public static void raten(int zahl) {
        
        if (zahl == meineZahl) {
            
            System.out.println("Richtig geraten! Sie haben " + AnzVersuche + " Versuche gebraucht");
        }
        else if(zahl > meineZahl) {
            System.out.println("Die Zahl ist kleiner");
            }   
        else {
            System.out.println("Die Zahl ist groesser");
        }
        if(zahl != meineZahl) {
        text.setText("Bitte erneut raten");
        }
    }
}

Solution

  • You can add in openUI() an action listener like:

    Action action = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("some action");
                System.out.println("value: " + textField.getText());
            }
        };
    
        textField.addActionListener( action );