Title: import javax'swing'JOptionPane
1Introduction to GUI
import javax.swing.JOptionPane //import
javax. public class OptionPane1
public static void main(String args)
String numstr1 int num1
numstr1 JOptionPane.showInputDialog("Enter a
number") System.out.println("Here")
num1 Integer.parseInt(numstr1)
num1 Math.abs(num1)
JOptionPane.showMessageDialog(null,
num1"",
"Results Window",
//JOptionPane.WARNING_M
ESSAGE)
//JOptionPane.PLAIN_MESSAGE)
JOptionPane.INFORMATION_MESSAGE)
System.exit(0)
JOptionPane with Swing
2Screen Shot of OptionPane1.java
- A string is grabbed from the dialog box.
- parseInt throws an exception with an invalid
string is entered. (String with letters,
punctuation, non-digits characters.)
3OptionPane2
import javax.swing.JOptionPane public class
OptionPane2 public static void
main(String args) String
numstr1 double num1 numstr1
JOptionPane.showInputDialog("Enter a number")
num1 Double.parseDouble(numstr1)
num1 Math.abs(num1)
JOptionPane.showMessageDialog(null,
""num1,
"Results Window",
//JOptionPane.PLAIN_MES
SAGE)
JOptionPane.INFORMATION_MESSAGE)
System.exit(0)
4Sreenshot of OptionPane2
5Introduction to Applet
import javax.swing.JApplet import
java.awt.Graphics public class Applet1 extends
JApplet public void paint(Graphics g)
g.drawLine(20, 5, 150, 5)
g.drawString("This is my first", 20, 20)
g.drawString("Applet program.", 20,
40) g.drawLine(20, 50, 150, 50)
6HTML File for Applet1
lt!-- FirstApplets/Applet1.html --gt ltHTMLgt ltHEADgt
ltTITLEgtFirst Appletlt/TITLEgt lt/HEADgt ltBODYgt
lth1gtMy First Applet Pagelt/h1gt lthrgt ltapplet
code"Applet1.class" width500 height100gt lt/apple
tgt lt/BODYgt lt/HTMLgt
7Applet2
import javax.swing.JApplet import
java.awt.Graphics import javax.swing.JOptionPane
public class Applet2 extends JApplet
private double num1 public void init()
String numstr1
JOptionPane.showInputDialog("Enter a number")
num1 Double.parseDouble(numstr1)
public void paint(Graphics
g) g.drawRect(5,
5, 400, 90) g.drawString("This
is the absolute value "Math.abs(num1),
25, 25)
- Combining JOptionPane and Applet window.
8Screen Shot of Applet2
9Screen Shot of Applet2 (cont.)
10Introduction to Event Handling
import javax.swing. import java.awt. import
java.awt.event. public class Exercise1 extends
JApplet implements ActionListener private
Container c private JLabel prompt
private JTextField input private JTextArea
message new JTextArea(2, 30) private int
num1, num2 private boolean correct
public void init() correcttrue c
getContentPane() c.setLayout(new
FlowLayout()) prompt new JLabel()
c.add(prompt) input new
JTextField(10) //input.setEditable(true)
//default is true
input.addActionListener(this)
c.add(input) message.setEditable(false)
c.add(message)
displayQuestion()
public void displayQuestion() if
(correct) num1 (int)(Math.random() 10
1) num2 (int)(Math.random() 10 1)
prompt.setText("How much is "num1"
times "num2"?") input.setText("")
public void actionPerformed(ActionEvent
e) int result
Integer.parseInt(input.getText()) if
(result (num1num2)) correct true
message.setText("Yes, "num1 " times"
num2" equals to "result ".\nTry the next
question...") else
correct false message.setText("Sorry, try
again...") displayQuestion()
//end Exercise1
11Applet Window Structure
JTextField
JLabel
JTextArea
12Screenshots of Exercise1
13Anonymous Inner Class
public class Exercise1b extends
JApplet // input new JTextField(10) input
.addActionListener( new
ActionListener() public void
actionPerformed(ActionEvent e) int result
Integer.parseInt(input.getText()) if (result
(num1num2)) correct true message.se
tText("Yes, "num1 " times "num2" equals to
" result".\nTry the next
question...") else correct
false message.setText("Sorry, try
again...") displayQuestion()
) c.add(input) //
14Exercise1c More than one event handler
input.addMouseListener( new MouseAdapter()
public void mouseEntered(MouseEvent
e) input.setBackground(Color.yellow)
public void mouseExited(MouseEvent
e) input.setBackground(Color.white)
)
15Exercise1d Abandoning Anonymous Inner Class
public class Exercise1d //... input.addActionLi
stener(new inputHandler()) input.addMouseListene
r(new inputEditingHandler()) //... private
class inputHandler implements ActionListener pu
blic void actionPerformed(ActionEvent e) int
result Integer.parseInt(input.getText()) if
(result (num1num2)) correct
true message.setText("Yes, "num1 " times
"num2" equals to " result".\nTry
the next question...") else correct
false message.setText("Sorry, try
again...") displayQuestion() pr
ivate class inputEditingHandler extends
MouseAdapter public void mouseEntered(MouseEven
t e) input.setBackground(Color.yellow)
public void mouseExited(MouseEvent
e) input.setBackground(Color.white) /
/end Exercise1d