我昨天发布了这个代码的另一个问题,但现在我再一次请求大家帮忙..我感谢所有的帮助到目前为止我没有这个社区我到目前为止还没有。
现在,我的问题非常简单。我有一个带有用户输入框的GUI和三个按钮。按钮不是JButton,它们是标准的JOptionsPane yes,no和cancel按钮。我确实将文本更改为“下一个条目”,“下一批”和“已完成”。
在我更改GUI之前,我有标准按钮。最常使用的按钮是“是”按钮(现在是“下一个条目”)..在我的程序中点击这个按钮很多。在默认按钮设置工作之前..用户只需键入一个数字并快速按Enter键。现在,回车键不会激活默认按钮,而是用户必须在物理上点击它。这就是我想要改变的。
我有什么方法可以设置下面的代码,所以当用户点击回车时,无论他们输入什么文字,都是“是”(现在“默认点击下一批”)?对于它的价值,我确实对此进行了研究,但找不到适合我特定情况的解决方案。
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);
}
}
答案 0 :(得分:1)
您需要将默认选项作为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的消息体中。