一、简单指令
//Java的选项面板
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class DialogueDemo {
public static void main(String[] args) {
// JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
// 参数1.对话框的父窗体对象
// 参数2.对话框的提示信息
// 参数3.对话框的标题信息
// 参数4.对话框的消息类型(主信息前的图标会改变) JOptionPane.---
// 参数5.图像,针对参数4的自定义图像 new ImageIcon("images/-文件名.jpg-")
JOptionPane.showMessageDialog(null, "提示信息", "标题信息", JOptionPane.WARNING_MESSAGE, new ImageIcon("images/网易云.jpg"));
// JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon);
JOptionPane.showConfirmDialog(null, "Confirm", "判断", 0, 0, new ImageIcon("images/头1.Jpg"));
// JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)
// 参数6.下拉选项,可以用字符串输组
// 参数7.在没有选项6的情况下可以出现默认提示
JOptionPane.showInputDialog(null, "网易云", "网易云", 0,new ImageIcon("images/网易云.jpg"), new String [] {"1.网易云时刻","2.12点到了","3.年轻人绝对不网抑云"},null);
}
}
二、后宫选妃(实例)
知识点:
1. JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
2. JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue);
3. JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon)
4. 大循环结构用while
5. 每次的选择结果用switch
6. 比较字符串是否相等,用于查找嫔妃是否已拥有
×××.equals()
×××.compareTo() == 0
7. 游戏开始前要判断选择取消就continue
8. 图片插入 new ImageIcon("images/×××.jpg")
9. 图形界面不支持/t
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ConcubineDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//新加妃子数组
String[] NewNameArray = {"王昭君","西施","貂蝉","杨玉环"};
//妃子数组个数
int NewCount = 4 ;
//妃子数组
String[] ConcubineNameArray = {"甄嬛","安陵容","沈眉庄","年世兰","","","",""};
//对应妃子的好感度
int[] loves = new int[10];
//妃子的显示数量为4
int ConcubineCount = 4;
//默认每个妃嫔好感度值为100
for(int i = 0 ;i < ConcubineCount ; i++) {
loves [i] = 100;
}
//游戏运行天数
int GameDays = 1;
//游戏主界面
JOptionPane.showMessageDialog(null, null, "后宫选妃", 0, new ImageIcon("images/甄嬛后宫封面图.jpg"));
while(GameDays <= 10) {
// System.out.println("游戏进行到第"+GameDays+"天");
// System.out.println("1.都不喜欢,朕要纳妃!\t\t增加角色数量");
// System.out.println("2.深得朕意,马上宠幸!\t\t增加角色好感度");
// System.out.println("3.朕意已决,打入冷宫!\t\t减少角色数量");
// System.out.print("陛下请选择:");
String [] strMenu = {"1.都不喜欢,朕要纳妃!","2.深得朕意,马上宠幸!","3.朕意已决,打入冷宫!"};
Object objresult = JOptionPane.showInputDialog(null, strMenu, "游戏进行到第"+GameDays+"天", 0, new ImageIcon("images/皇帝思考.jpg"), new String []{"1","2","3"}, 0);
int temp = -1 ;
String name = new String();
int choice = Integer.parseInt(objresult.toString());
switch (choice) {
case 1:
// System.out.print("想要纳妃的名字:");
objresult = JOptionPane.showInputDialog(null, "请选择秀女", "后宫选妃", 0, new ImageIcon("images/皇上瞧瞧选啥啊.jpg"), NewNameArray,null);
if(objresult == null) {
continue;
}
String newName = objresult.toString();
JOptionPane.showMessageDialog(null, objresult.toString(), "选中的嫔妃", 0, new ImageIcon("images/"+objresult.toString()+".jpg"));
for(int i = 0; i < ConcubineCount ; i++) {
if(newName.equals(ConcubineNameArray[i])) {
temp = i;
JOptionPane.showMessageDialog(null, "我已经是你的人了", "皇上果然不记得我了", 0 ,new ImageIcon("images/"+objresult.toString()+".jpg"));
temp = 1;
break;
}
}
if(ConcubineCount == ConcubineNameArray.length) {
JOptionPane.showMessageDialog(null, "纳妃失败!后宫嫔妃太多养不起了!", objresult.toString()+"纳入失败", 0, new ImageIcon("images/人多.jpg"));
break;
}else if(temp == 1) {
continue;
}
else {
ConcubineNameArray[ConcubineCount] = newName;
loves[ConcubineCount] = 100 ;
for(int i = 0 ; i < ConcubineCount ; i++) {
loves[i] -= 10 ;
}
ConcubineCount++;
}
break;
case 2:
objresult = JOptionPane.showInputDialog(null, "皇上今天你想宠幸谁啊?", "宠幸我宠幸我", 0, new ImageIcon("images/等待宠幸.jpg"), ConcubineNameArray, null);
if(objresult == null) {
continue;
}
name = objresult.toString();
for(int i = 0 ; i < ConcubineCount ; i++) {
if(name.compareTo(ConcubineNameArray[i]) == 0) {
temp = i;
break;
}
}
if(temp == -1) {
System.out.println(name+"不在皇上的后宫里,宠幸失败!");
break;
}
loves[temp] += 20 ;
int B = 0;
for(int j = 0 ;j < ConcubineCount ; j++) {
if(j != temp) {
loves[j] -=10 ;
}
if(loves[j] < 60) {
B++;
}
}
if(B >= 3) {
JOptionPane.showMessageDialog(null, B+"妃夺权,皇帝驾崩,游戏结束!", "隐藏结局----"+B+"妃暴乱", 0, new ImageIcon("images/皇帝死.jpg"));
System.exit(0);
}
JOptionPane.showMessageDialog(null, "宠幸"+ConcubineNameArray[temp]+",好感度+20", "宠幸成功", 0, new ImageIcon("images/受赏.jpg"));
break;
case 3:
objresult = JOptionPane.showInputDialog(null, "哪位妃子惹皇上生气了,贬入冷宫:", "贬入冷宫", 0, new ImageIcon("images/皇帝生气.png"), ConcubineNameArray, null);
if(objresult == null) {
continue;
}
for(int i = 0; i < ConcubineCount; i++) {
if(objresult.toString().equals(ConcubineNameArray[i])) {
temp = i;
}
}
for(int i = temp ; i < ConcubineCount ; i++) {
ConcubineNameArray[i] = ConcubineNameArray[i + 1];
loves[i] = loves[i + 1];
}
ConcubineCount--;
JOptionPane.showMessageDialog(null, "", objresult+"已被贬入冷宫", 0, new ImageIcon("images/冷宫.jpg"));
break;
default:
System.out.println("必须输入1-3之间做出选择");
continue;
}
//每日结算
String value = "没羞没臊的生活又过了一天,后宫的情况如下:\n嫔妃 好感度\n";
for(int i = 0 ; i < ConcubineCount ; i++) {
value += String.format("%s %d\n", ConcubineNameArray[i],loves[i]);
}
JOptionPane.showMessageDialog(null, value, "看看今天的爱妃都咋样了", 0, new ImageIcon("images/最后.jpg"));
// System.out.println();
// System.out.println("姓名\t\t好感度");
// for(int i = 0 ;i < ConcubineCount ; i++) {
// System.out.println(ConcubineNameArray[i]+"\t\t"+loves[i]);
// }
// System.out.println();
// System.out.println("--------------------------------------------------");
// System.out.println();
GameDays++;
}
}
}