1. 不带输入的加法器(在对话框中显示结果)
(1)代码实现
package TEST;
import javax.swing.JOptionPane; //导入类
public class TEST
{
public static void main(String args[])
{
int n1,n2,sum;
n1 = 12;
n2 = 24;
sum = n1+n2;
JOptionPane.showMessageDialog(null, "The sum is:"+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);
//第1个参数:null 显示在中央
//第2个参数:要显示的字符
//第3个参数:标题栏信息
//第4个参数:对话框类型
System.exit(0); //终结图形用户界面程序必须的
}
}
(2)运行结果
2. 包含输入对话框的加法器
(1)代码实现
package TEST;
import javax.swing.JOptionPane; //导入类
public class TEST
{
public static void main(String args[])
{
String input_pane1,input_pane2;
int n1,n2,sum;
input_pane1 = JOptionPane.showInputDialog("Please input the first number"); //输入框1
input_pane2 = JOptionPane.showInputDialog("Please input the second number"); //输入框2
n1 = Integer.parseInt(input_pane1); //获取输入框中输入数据的整数类型
n2 = Integer.parseInt(input_pane2);//获取输入框中输入数据的整数类型
sum = n1+n2;
JOptionPane.showMessageDialog(null, "The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);
//第1个参数:null 显示在中央
//第2个参数:要显示的字符
//第3个参数:标题栏信息
//第4个参数:对话框类型
System.exit(0); //终结图形用户界面程序必须的
}
}
(2)运行结果