如何在这个getch类中从https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java 这里是代码C++,我使用,然后在链接中提供,并且我急于实现它,但是看不出它在哪里返回它在我当前的程序中使用: x = _getch(); //take input without waiting for an enter key pressed // upon input interrupt thread interrupted.store(true); < >我使用用户输入来中断C++中的线程,因此我将X值分配给输入处理。我正在寻找相同的功能
如何使用链接中的java代码实现这一点:如何在这个getch类中从https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java 这里是代码C++,我使用,然后在链接中提供,并且我急于实现它,但是看不出它在哪里返回它在我当前的程序中使用: x = _getch(); //take input without waiting for an enter key pressed // upon input interrupt thread interrupted.store(true); < >我使用用户输入来中断C++中的线程,因此我将X值分配给输入处理。我正在寻找相同的功能,java,c++,Java,C++,如何使用链接中的java代码实现这一点: import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import javax.swing.*; class getch { public static void getCh() { final JFrame frame = new JFrame(); synchronized(frame) {
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class getch
{
public static void getCh()
{
final JFrame frame = new JFrame();
synchronized(frame)
{
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
synchronized(frame)
{
frame.setVisible(false);
frame.dispose();
frame.notify();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
});
frame.setVisible(true);
try
{
frame.wait();
}
catch(InterruptedException e1)
{
}
}
}
}
似乎没有返回,我很难看到代码在哪里“抓取和存储”字符。当我转换这个程序时,在我的主语言java中如何称呼它?我在这里问错问题了吗。。。似乎如果它应该执行一个getCH函数,那么它应该返回一个字符