目录
GUI图形开发API --- 弹窗功能
//1、消息框 -- 用来替代System.out.println()
/*
showMessageDialog() 接受两个参数,中间用","号分隔;
参数1:目前固定写为 null,代表弹在屏幕的正中央;
参数2:是我们要显示在弹窗上的消息。
*/
JOptionPane.showMessageDialog(null,"今天天气真好!");
//2、输入框 -- 用来替代Scanner
/*
参数1:null表示显示在屏幕的正中央;
参数2:显示在弹窗上的提示信息。
注意:
1、弹窗输入框中无论输入什么内容,在程序中都得到的是字符串。
2、如果点击了输入框的"取消"按钮,那么input会被赋值为null,
即没有引用指向任何字符串。
*/
String input = JOptionPane.showInputDialog(null,"请输入今天的气温:");
//3、确认框 - 提出一个问题,给用户三个选择"是","否","取消"
/*
点击不同的按钮,会返回不同的int值。
是 -- 0
否 -- 1
取消 -- 2
*/
int choice = JOptionPane.showConfirmDialog(null,"我帅吗?");
System.out.println(choice);
字符串和基本数据类型的转换
/*
演示:字符串和基本数据类型之间的转换
1、基本数据类型 转 字符串 -- 基本数据类型 + ""
比如: 1 + "";
"" + 3.14;
2、字符串 转 基本数据类型
首先保证字符串里面的内容是一个正确的可以被转换的数据
*/
public static void main(String[] args) {
//1、字符串 ---> 整型
String ageStr = JOptionPane.showInputDialog(null,"请输入你的年龄");
int age = Integer.parseInt(ageStr);
System.out.println(age + 1);
System.out.println(ageStr + 1);
//2、字符串 ---> double
String weightStr = JOptionPane.showInputDialog(null,"你多少斤?");
double weight = Double.parseDouble(weightStr);
float weight2 = Float.parseFloat(weightStr);
//3.字符串 ---> char[]
String heightStr = JOptionPane.showInputDialog(null,"你多少高?");
char[] height = heightStr.toCharArray();