Problem Solving 6 GUIs and Event Handling - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Problem Solving 6 GUIs and Event Handling

Description:

The purpose of the GUI is to calculate the body mass index (BMI) of a person. ... super('Simple Pie Chart Drawing'); setSize(300, 300); setResizable(false); show ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 12
Provided by: ocwKfu
Category:

less

Transcript and Presenter's Notes

Title: Problem Solving 6 GUIs and Event Handling


1
Problem Solving 6 GUIs and Event Handling
  • ICS-201 Introduction to Computing II
  • Semester 071

2
Constructing a GUI
  • Consider the following problem
  • Construct a GUI as follows
  • The purpose of the GUI is to calculate the body
    mass index (BMI) of a person. The formula to
    calculate the BMI is
  • BMI weight(in kg)/height2 (in metres)

3
Solution Components in the GUI
  • Here we find that the GUI has the following
    components
  • The layout of the components is Flow Layout

Button
Labels
Text fields
4
Solution Code for the GUI
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class BMICalc extends JFrame implements
    ActionListener
  • private JTextField wtt, htt
  • private JButton convert
  • private JLabel wtl, htl, rsll
  • public BMICalc()
  • super("Body Mass Index Calculator")
  • setSize(500, 70)
  • Container cp getContentPane()
  • cp.setLayout(new FlowLayout())

5
Solution Code for the GUI (contd)
  • wtt new
    JTextField(5)
  • htt new JTextField(5)
  • wtl new JLabel("Weight (kg)")
  • htl new JLabel("Height (m)")
  • rsll new JLabel("Your BMI is")
  • convert new JButton("Calculate")
  • cp.add(wtl)
  • cp.add(wtt)
  • cp.add(htl)
  • cp.add(htt)
  • cp.add(convert)
  • cp.add(rsll)
  • convert.addActionListener(this)
  • show()

6
Solution Action Listener
  • public void actionPerformed(ActionEvent e)
  • double weight Double.parseDouble(wtt.getText()
    )
  • double height Double.parseDouble(htt.getText()
    )
  • double bmi weight/(heightheight)
  • double bmirounded ((int) (bmi1000))/1000.0
  • rsll.setText("Your BMI is " bmirounded)
  • wtt.setText("")
  • htt.setText("")
  • public static void main(String args)
  • new BMICalc()

7
Problem Using Layout Managers
  • Construct a GUI that divides a frame into
    appropriate areas as shown in the following GUI.
    Use Border and Grid Layouts.

8
Solution
  • Solution in attached zip file. Note in the
    solution that

Border Layout
Grid Layout
Flow Layout
9
Problem Drawing Shapes
  • Write a program that constructs the following
    figure along with associated labels.

10
Solution
public class PieChart extends Frame public
PieChart() super("Simple Pie Chart
Drawing") setSize(300, 300) setResizable(fal
se) show() public void paint(Graphics
g) Graphics2D g2 (Graphics2D)
g g2.setFont(new Font("Arial", Font.ITALIC,
12)) Arc2D.Double slice1 new
Arc2D.Double(50, 50, 150, 150, 0, 90,
2) g2.setColor(Color.red) g2.draw(slice1)
g2.fill(slice1) g2.drawString("25 Revenue",
170, 60)
11
Solution contd.
Arc2D.Double slice2 new Arc2D.Double(50,
50, 150, 150, 90, 45, 2) g2.setColor(Color.blue
) g2.draw(slice2) g2.fill(slice2) g2.drawStri
ng("12.5 Sales", 40, 50) Arc2D.Double
slice3 new Arc2D.Double(50, 50, 150, 150,
135, 135, 2) g2.setColor(Color.magenta)
g2.draw(slice3) g2.fill(slice3) g2.drawString(
"37.5 Profits", 30, 220) Arc2D.Double
slice4 new Arc2D.Double(50, 50, 150, 150,
270, 90, 2) g2.setColor(Color.green)
g2.draw(slice4) g2.fill(slice4) g2.drawString(
"25 Expenditures", 180, 200) public
static void main(String args) PieChart p
new PieChart()
Write a Comment
User Comments (0)
About PowerShow.com