相关疑难解决方法(0)

在JOptionPane.showOptionDialog()中设置组件焦点

为了在输入对话框中有自定义按钮标题,我创建了以下代码:

String key = null;
JTextField txtKey = new JTextField();        
int answerKey = JOptionPane.showOptionDialog(this, new Object[] {pleaseEnterTheKey, txtKey}, decryptionKey, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] {okCaption, cancelCaption}, okCaption);        
if (answerKey == JOptionPane.OK_OPTION && txtKey.getText() != null) {
  key = txtKey.getText();
}
Run Code Online (Sandbox Code Playgroud)

如何在显示对话框时将焦点(光标)移动到文本字段?

UPDATE

这对我不起作用,我的意思是文本字段没有焦点:操作系统:Fedora - Gnome

public class Test {
  public static void main(String[] args) {
    String key = null;
    JTextField txtKey = new JTextField();
    txtKey.addAncestorListener(new RequestFocusListener());
    int answerKey = JOptionPane.showOptionDialog(null, new Object[]{"Please enter the key:", txtKey}, "Title", JOptionPane.OK_CANCEL_OPTION, …
Run Code Online (Sandbox Code Playgroud)

java swing focus joptionpane

12
推荐指数
4
解决办法
1万
查看次数

标签 统计

focus ×1

java ×1

joptionpane ×1

swing ×1