本文介绍了在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();



$ b

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



更新

不为我工作,我的意思是textfield没有关注:
操作系统:Fedora的 - 侏儒

  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,JOptionPane.QUESTION_MESSAGE,null,new Object [] {OKKK ,CANCELLLL},OKKK);
if(answerKey == JOptionPane.OK_OPTION&& txtKey.getText()!= null){
key = txtKey.getText();




$ div class =h2_lin>解决方案

显示了如何轻松设置焦点在模态对话框中的任何组件。


In order to have custom button captions in an input dialog, I created the following code:

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();
}

How can I move the focus (cursor) to the text field as the dialog is displayed?

UPDATE

This does not work for me, I mean the textfield has no focus:OS: 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, JOptionPane.QUESTION_MESSAGE, null, new Object[]{"OKKK", "CANCELLLL"}, "OKKK");
    if (answerKey == JOptionPane.OK_OPTION && txtKey.getText() != null) {
      key = txtKey.getText();
    }
  }
}
解决方案

Dialog Focus shows how you can easily set the focus on any component in a modal dialog.

这篇关于在JOptionPane.showOptionDialog()中设置组件焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:23