[英]show input dialog box after error message dialog box
我需要顯示一個輸入對話框並輸入播放器的名稱。
如果玩家單擊了ok按鈕並且輸入對話框的值是數字或null,則應顯示錯誤。 如果播放器單擊錯誤消息對話框的確定按鈕,則輸入框將再次出現。
我不知道我的代碼是不是錯了。 因為我的網絡編程主題,我忘記了如何在java中編碼。
int[] player = new int[1];
for(int a =0; a<player.length; a++){
String input = JOptionPane.showInputDialog("Enter your Name:",JOptionPane.OK_CANCEL_OPTION);
try {
if(!input.matches("[a-zA-Z]+")){
JOptionPane.showMessageDialog(null,"Use Letters only", "Warning", JOptionPane.OK_OPTION);
} else {
input = String.valueOf(player[a]);
category c = new category();
this.dispose();
c.show();
}
}
catch(Exception e){
};
如果你做正確的循環,你只需要一個對話框 :
String message = "Enter Your Name:";
String playerName = null;
do {
playerName =
JOptionPane.showInputDialog(message);
message = "<html><b style='color:red'>Enter Your Name:</b><br>"
+ "Use letters only.";
} while(playerName != null && !playerName.matches("[a-zA-Z]+"));
System.out.println("PlayerName: " + playerName);
這將包括在請求有效名稱的下一次迭代中的錯誤消息 。 使用它也更好一些。
在第一次通話時顯示如下:
如果出現錯誤,請像這樣:
注意
如果名稱中的數字可以正常(例如"bunny99"
),您可能希望將正則表達式更改為"[a-zA-Z]\\\\w+"
顯示錯誤對話框,顯示消息“alert”:
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
顯示包含“信息”消息的內部信息對話框:
JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);
顯示一個信息面板,其中包含選項yes / no和消息'choose one':
JOptionPane.showConfirmDialog(null,"choose one", "choose one", JOptionPane.YES_NO_OPTION);
顯示內部信息對話框,其中包含選項yes / no / cancel和消息'please choose one'和標題信息:
JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
顯示警告對話框,其中包含選項OK,CANCEL,標題'Warning'和消息'單擊OK繼續':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
顯示一個對話框,要求用戶輸入字符串:
String inputValue = JOptionPane.showInputDialog("Please input a value");
顯示一個對話框,要求用戶選擇一個字符串:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.