GUI Basics

All GUI’s are composed of GUI components. i.e. menu, scroll bars.

Java has all these components built in, you just have to say where to put it and what to do with it.

import javax.swing.JOptionPane;

// jamesprogram.java
class jamesprogram {

	public static void main (String[] args){
		
		// each JOptionPane doesn't move on until
		// it has received an input
		String fn = JOptionPane.showInputDialog("Enter first number");
		String sn = JOptionPane.showInputDialog("Enter second number");
	
		// we need to convert these variables to integers
		int num1 = Integer.parseInt(fn);
		int num2 = Integer.parseInt(sn);	
		int sum = num1 + num2;
		
		JOptionPane.showMessageDialog(null,"The answer is " +sum, "the title", JOptionPane.PLAIN_MESSAGE);
		
	}
}

The showMessageDialog method takes 4 parameters,

null = positions in middle of screen
“The answer is ” +sum = the message
“the title” = title of the prompt box
JOptionPane.PLAIN_MESSAGE = plain message

joptionpane video

Leave a Reply