1 / 11

Problem Solving 6 GUIs and Event Handling

Problem Solving 6 GUIs and Event Handling. ICS-201 Introduction to Computing II Semester 071. 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

maili
Download Presentation

Problem Solving 6 GUIs and Event Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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()); Solution – Code for the GUI

  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. public void actionPerformed(ActionEvent e) { double weight = Double.parseDouble(wtt.getText()); double height = Double.parseDouble(htt.getText()); double bmi = weight/(height*height); double bmirounded = ((int) (bmi*1000))/1000.0; rsll.setText("Your BMI is: " + bmirounded); wtt.setText(""); htt.setText(""); } public static void main(String[] args) { new BMICalc(); } } Solution – Action Listener

  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(false); 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.drawString("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(); } }

More Related