Код: Выделить всё
public class Consola
{
private final JDialog dialog;
private final JTextArea textArea;
private String userInput;
public Consola(JFrame mainFrame)
{
dialog=new JDialog(mainFrame);
textArea=new JTextArea();
textArea.addKeyListener(new EscuchaKey());
dialog.add(new JScrollPane(textArea),BorderLayout.CENTER);
dialog.setSize(300,300);
dialog.setVisible(true);
}
public String readString()
{
// Resetear el estado
userInput=null;
textArea.setText("");
while(userInput==null)
{
}
return userInput;
}
class EscuchaKey extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
userInput=textArea.getText().trim();
}
}
}
}
Вот основной класс:
Код: Выделить всё
public class ConsolaTest {
public static void main(String[] args) {
// Crear un JFrame principal
JFrame mainFrame = new JFrame("Test Consola");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(300,300);
JButton button = new JButton(":O)");
mainFrame.add(button,BorderLayout.WEST);
Consola consola = new Consola(mainFrame);
// Acción del botón
button.addActionListener(e -> {
String input = consola.readString();
System.out.println("Texto ingresado: " + input);
});
mainFrame.setVisible(true);
}
}
Пожалуйста, помогите мне. Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/793 ... or-jdialog
Мобильная версия