Example of Scanner Example 1 import java util

  • Slides: 9
Download presentation
Example of Scanner

Example of Scanner

// Example #1 import java. util. Scanner; class Test. Revised { public void menu()

// Example #1 import java. util. Scanner; class Test. Revised { public void menu() { Scanner scanner = new Scanner(System. in); System. out. print("Enter a sentence: t"); String sentence = scanner. next. Line(); System. out. print("Enter an index: t"); int index = scanner. next. Int(); System. out. println("n. Your sentence: t" + sentence); System. out. println("Your index: t" + index); } }

// Example #2 import java. util. Scanner; class Test { public void menu() {

// Example #2 import java. util. Scanner; class Test { public void menu() { Scanner scanner = new Scanner(System. in); while (true) { System. out. println("n. Menu Optionsn"); System. out. println("(1) - do this"); System. out. println("(2) - quit"); System. out. print("Please enter your selection: t"); int selection = scanner. next. Int(); if (selection == 1) { System. out. print("Enter a sentence: t"); String sentence = scanner. next. Line(); System. out. print("Enter an index: t"); int index = scanner. next. Int(); System. out. println("n. Your sentence: t" + sentence); System. out. println("Your index: t" + index); } else if (selection == 2) { break; } }

Example of Swing Class

Example of Swing Class

JOoption Pane Joption Icon JFrame

JOoption Pane Joption Icon JFrame

EXAMPLE # 1 import javax. swing. Image. Icon; import javax. swing. JFrame; import javax.

EXAMPLE # 1 import javax. swing. Image. Icon; import javax. swing. JFrame; import javax. swing. JOption. Pane; public class Show. Message. Dialog. Example 1 { public static void main(String[] args) { String backup. Dir = "/Users/al/backups"; // create a jframe JFrame frame = new JFrame("JOption. Pane show. Message. Dialog example"); } } // show a joptionpane dialog using show. Message. Dialog JOption. Pane. show. Message. Dialog(frame, "Problem writing to backup directory: '" + backup. Dir + "'. "); System. exit(0);

EXAMPLE # 2 import javax. swing. Image. Icon; import javax. swing. JFrame; import javax.

EXAMPLE # 2 import javax. swing. Image. Icon; import javax. swing. JFrame; import javax. swing. JOption. Pane; public class Show. Message. Dialog. Example 1 { public static void main(String[] args) { String backup. Dir = "/Users/al/backups"; // create a jframe JFrame frame = new JFrame("JOption. Pane show. Message. Dialog example"); } } // show a joptionpane dialog using show. Message. Dialog JOption. Pane. show. Message. Dialog(frame, "Problem writing to backup directory: '" + backup. Dir + "'. ", "Backup problem", JOption. Pane. INFORMATION_MESSAGE); System. exit(0);

Example of AWT Class

Example of AWT Class

EXAMPLE import java. awt. Font; import java. awt. Graphics 2 D; import java. awt.

EXAMPLE import java. awt. Font; import java. awt. Graphics 2 D; import java. awt. Rendering. Hints; import javax. swing. JFrame; import javax. swing. JPanel; public class Main extends JPanel{ public void paint(Graphics g) { Graphics 2 D g 2 = (Graphics 2 D)g; g 2. set. Rendering. Hint(Rendering. Hints. KEY_ANTIALIASING, Rendering. Hints. VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font. PLAIN, 96); g 2. set. Font(font); g 2. draw. String("Text", 40, 120); } public static void main(String[] args) { JFrame f = new JFrame(); f. get. Content. Pane(). add(new Main()); f. set. Size(300, 200); f. set. Visible(true); } }