import javax'swing'JOptionPane - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

import javax'swing'JOptionPane

Description:

g.drawString('This is my first', 20, 20); g.drawString('Applet ... g.drawString('This is the absolute value: ' Math.abs(num1), 25, 25); Introduction to GUI ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 16
Provided by: crc7
Category:

less

Transcript and Presenter's Notes

Title: import javax'swing'JOptionPane


1
Introduction 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
2
Screen 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.)

3
OptionPane2
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)
4
Sreenshot of OptionPane2
5
Introduction 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)
6
HTML 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
7
Applet2
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.

8
Screen Shot of Applet2
9
Screen Shot of Applet2 (cont.)
10
Introduction 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
11
Applet Window Structure
JTextField
JLabel
JTextArea
12
Screenshots of Exercise1
13
Anonymous 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) //
14
Exercise1c 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)
)
15
Exercise1d 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
Write a Comment
User Comments (0)
About PowerShow.com