java - JOptionsPane 的默认按钮无法正常工作

标签 java

我昨天为另一个问题发布了这段代码,但现在我必须再次向大家寻求帮助。我感谢迄今为止我所得到的所有帮助,如果没有这个社区,我就不可能做到这一点。

现在,我的问题很简单。我有一个 GUI,其中有一个用户输入框和三个按钮。这些按钮不是 JButton,它们是标准 JOptionsPane 是、否和取消按钮。我确实将文本更改为“下一个条目”、“下一批”和“完成”。

在更改 GUI 之前,我有标准按钮。最常使用的按钮是"is"按钮(现在是“下一个条目”)。在我的程序中将多次单击该按钮。在默认按钮设置起作用之前..用户只需键入一个数字并快速按 Enter 键即可。现在,回车键不会激活默认按钮,而是用户必须实际单击它。这就是我想要更改的内容。

有什么方法可以设置下面的代码,以便当用户单击输入时,无论他们输入什么文本,都会显示"is"(现在默认单击“下一批”)?对于它的值(value),我确实对此进行了研究,但找不到适合我的具体情况的解决方案。

package nacha;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Testing4 

{

    public static void main(String args[]){

        String css = "<span style='font-size:10; color: white; background-color:black'>";
        String batchCss = "<span style='font-size: 20'>";
        String endSpanCss = "</span>"; 
        String table = "<table border=4>";
        String endTable = "</table>"; 
        String mainCss = "<span style='font-size:12; color: red'>";
        String header1Css = "<span style = 'font-size:15; font-weight:bold;text-decoration:underline;border:1px dotted red'>";
        String text1Css = "<span style = 'font-size:12; font-style:italic'>";



         String text = "<html>" +
                css + batchCss + "1 of 2"+endSpanCss+endSpanCss+ endSpanCss +
                "<br><br><br>"+header1Css+"Entry Detail:"+endSpanCss +
                "<br>"+mainCss+"111111111111111111111"+endSpanCss+
                "<br><br><br>"+text1Css+"Please type 1-21 to apply a reason code and addenda record to the entry detail." +
                "<br>Please type 'h' and press the next entry button to open the help screen."+endSpanCss +
                "<br><br><br>"+header1Css+"Reason Codes"+ endSpanCss +
                "<br>"+table+"R01 - Insufficient Funds" +
                "<br>R02 - Account Closed" +
                "<br>R03 - No Account" +
                "<br>R04 - Invalid Account Number" +
                "<br>R05 - Unauthorized Debit to Consumer Account" +
                "<br>R06 - Returned per ODFI Request" +
                "<br>R07 - Auth Revoked by Customer" +
                "<br>R08 - Payment Stopped" +
                "<br>R09 - Uncollected Funds" +
                "<br>R10 - Customer Advises Not Authorized" +
                "<br>R11 - Check Truncation Entry Return" +
                "<br>R12 - Branch Sold to Another DFI" +
                "<br>R13 - Invalid ACH Routing Number" +
                "<br>R14 - Represenative Payee Deceased or Unable to Continue" +
                "<br>R15 - Beneficiary or Account Holder Deceased" +
                "<br>R16 - Account Frozen" +
                "<br>R17 - File Record Edit Criteria" +
                "<br>R18 - Improper Effective Entry Date" +
                "<br>R19 - Account Field Error" +
                "<br>R20 - Non-Transaction Amount" +
                "<br>R21 - Invalid Company Information" +
                "<br>R22 - Invalid Individual ID Number"+endTable;

         //Below code creates the GUI for the return builder portion of the program. 
          Object[] options1 = {"Next Entry","Next Batch","Finished"};//Changes the default buttons. 
          BorderLayout border = new BorderLayout();
          JPanel panel = new JPanel();     
          panel.setLayout(border);
          panel.add(new JLabel(text),BorderLayout.NORTH);//Adds the label to the top of the panel.               
          JTextField textField = new JTextField(10); 
          panel.add(textField,BorderLayout.SOUTH);//Adds a user-input text area to the bottom of the panel.

          int result = JOptionPane.showOptionDialog(null, panel, "Return Builder", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, JOptionPane.YES_OPTION);

    }

}

最佳答案

您需要将默认选项作为最后一个参数传递给 JOptionPane.showOptionDialog:

int result = JOptionPane.showOptionDialog(null, panel, "Return Builder",
    JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
    options1, options1[0]);

这会产生副作用,使该按钮在显示对话框时具有初始键盘焦点。如果您不希望这样,您可以强制 JTextField 在窗口中显示时接收焦点:

textField.addHierarchyListener(new HierarchyListener() {
    @Override
    public void hierarchyChanged(HierarchyEvent e) {
        final Component c = e.getComponent();
        long flags = e.getChangeFlags();
        if ((flags & HierarchyEvent.SHOWING_CHANGED) != 0 &&
                c.isShowing()) {

            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    c.requestFocusInWindow();
                }
            });
        }
    }
});

附注:为了要求用户从已知选项列表中进行选择,JComboBox是比 JTextField 更好的选择。您的“帮助”选项可以只是一个不同的 JButton,出现在 JOptionPane 的消息正文中。

关于java - JOptionsPane 的默认按钮无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044898/

相关文章:

java - BlockingQueue - 仅获取特定对象。

用于服务器端应用程序的 JavaFX 脚本

Java Swing : Implementing a validity check of input values

java - 两个图像之间快速切换?

java - 在 Android < 2.0 上更新联系信息

java - JPA:@ManyToOne,1 表中不必要的重复条目

java - spring boot websocket配置错误

java - onItemClick 单击时不起作用

java - java中使用BigDecimal进行乘积计算

java - Android Studio 找不到主类报错