Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.

Similar presentations


Presentation on theme: "1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010."— Presentation transcript:

1 1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010

2 2 Displaying Text in a Dialog Box ( 使用 JOptionPane 來顯示 ) Display ( 顯示 ) – 顯示資料的方式有 : Command Windows, Graphic Window, Dialog Box … –Class JOptionPane 提供了 Dialog Box 的顯示方式 Packages – 一組已經寫好了的 classes – 相類似的 classes 組成 packages 所有的 packages 被稱為 Java class library 或 Java applications programming interface (Java API) –JOptionPane 是 javax.swing 這個 package 裡 的一個 class, 同時也包含其他 Graphical User Interfaces (GUIs) 的 classes

3 3 Displaying Text in a Dialog Box ( 使用 JOptionPane 來顯示 ) // 檔名 : Welcome.java // 利用 java package javax.swing 顯示數行文字 import javax.swing.JOptionPane; // 程式使用了 JOptionPane public class Welcome { public static void main( String args[] ) { JOptionPane.showMessageDialog( null, "Welcome\nto\nJava\nProgramming!" ); System.exit( 0 ); // 可略過這一行 } // end method main } // end class Welcome

4 4 Inputing using JOptionPane ( 使用 JOptionPane 來輸入資料 ) // 檔名 : Addition.java // 整數相加 Addition program that displays the sum of two numbers. // Java packages import javax.swing.JOptionPane; // program uses JOptionPane public class Addition { // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // 用來存放使用者輸入的第一個字串 String secondNumber; // 用來存放使用者輸入的第二個字串

5 5 int number1; // 宣告第一個整數變數來存放輸入的數字 int number2; // 宣告第二個整數變數來存放輸入的數字 int sum; // 數字的和 // 讀進使用者輸入的第一個數字, JOptionPane 的輸入是字串 firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); // 讀進使用者輸入的第二個數字 secondNumber= JOptionPane.showInputDialog("Enter second integer" ); number1 = Integer.parseInt( firstNumber ); // 把 數字從字串 String number2 = Integer.parseInt( secondNumber ); // 轉換為 int sum = number1 + number2; // 相加 JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // 這行可省略 } // end method main } // end class Addition

6 6 Output

7 7 Message Icons


Download ppt "1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010."

Similar presentations


Ads by Google