Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. Here are the instructions for my assignment. In BlueJ, start up the Simple GUI Extension. Layout the application as shown in the image above.

Hello. Here are the instructions for my assignment.

In BlueJ, start up the Simple GUI Extension.

Layout the application as shown in the image above.

You will need add a mouseClicked event for the Calculate button.

Save this GUI application and edit the code.

To convert the textfield for the age into an integer, you will need something similar to:

int age = Integer.parseInt( txtAge.getText() );

Calculate the lower target heart rate and store this in a variable. According to the CDC this is:

For moderate activity: lower heart rate = (220 - age) * 0.5

For vigorous activity: lower heart rate = (220 - age) * 0.7

Calculate the upper target heart rate and store this in a variable. According to the CDC this is:

For moderate activity: lower heart rate = (220 - age) * 0.7

For vigorous activity: lower heart rate = (220 - age) * 0.85

Display both of these heart rates using appropriate text fields. To format this display, you will need code similar to:

txtMax.setText( String.format("%.2f", maxRate) );

This is the code I have so far. And these are the errors that come up. What am I doing wrong?

image text in transcribed

/** *Text genereted by Simple GUI Extension for BlueJ */ import javax.swing.UIManager.LookAndFeelInfo; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.border.Border; import javax.swing.*;

public class GUI_project extends JFrame { private JMenuBar menuBar; private JButton button1; private JLabel label1; private JLabel label2; private JLabel label3; private JRadioButton rbModerate; private JRadioButton rbVigorous; private JTextField txtAge; private JTextField txtLow; private JTextField txtMax;

//Constructor public GUI_project(){

this.setTitle("GUI_project"); this.setSize(500,400); //menu generate method generateMenu(); this.setJMenuBar(menuBar);

//pane with null layout JPanel contentPane = new JPanel(null); contentPane.setPreferredSize(new Dimension(500,400)); contentPane.setBackground(new Color(192,192,192));

button1 = new JButton(); button1.setBounds(37,163,181,64); button1.setBackground(new Color(214,217,223)); button1.setForeground(new Color(0,0,0)); button1.setEnabled(true); button1.setFont(new Font("sansserif",0,12)); button1.setText("Calculate"); button1.setVisible(true); //Set methods for mouse events //Call defined methods button1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { mouseButtonClicked(evt); } });

label1 = new JLabel(); label1.setBounds(61,18,200,35); label1.setBackground(new Color(214,217,223)); label1.setForeground(new Color(0,0,0)); label1.setEnabled(true); label1.setFont(new Font("sansserif",0,12)); label1.setText("Target Heart Rate Calculator"); label1.setVisible(true);

label2 = new JLabel(); label2.setBounds(15,65,90,35); label2.setBackground(new Color(214,217,223)); label2.setForeground(new Color(0,0,0)); label2.setEnabled(true); label2.setFont(new Font("sansserif",0,12)); label2.setText("Age"); label2.setVisible(true);

label3 = new JLabel(); label3.setBounds(125,294,90,35); label3.setBackground(new Color(214,217,223)); label3.setForeground(new Color(0,0,0)); label3.setEnabled(true); label3.setFont(new Font("sansserif",0,12)); label3.setText("to"); label3.setVisible(true);

rbModerate = new JRadioButton(); rbModerate.setBounds(167,53,150,35); rbModerate.setBackground(new Color(214,217,223)); rbModerate.setForeground(new Color(0,0,0)); rbModerate.setEnabled(true); rbModerate.setFont(new Font("sansserif",0,12)); rbModerate.setText("Moderate Activity"); rbModerate.setVisible(true);

rbVigorous = new JRadioButton(); rbVigorous.setBounds(166,84,90,35); rbVigorous.setBackground(new Color(214,217,223)); rbVigorous.setForeground(new Color(0,0,0)); rbVigorous.setEnabled(true); rbVigorous.setFont(new Font("sansserif",0,12)); rbVigorous.setText("Vigorous Activity"); rbVigorous.setVisible(true);

txtAge = new JTextField(); txtAge.setBounds(45,66,90,35); txtAge.setBackground(new Color(255,255,255)); txtAge.setForeground(new Color(0,0,0)); txtAge.setEnabled(true); txtAge.setFont(new Font("sansserif",0,12)); txtAge.setText("21"); txtAge.setVisible(true);

txtLow = new JTextField(); txtLow.setBounds(24,295,90,35); txtLow.setBackground(new Color(255,255,255)); txtLow.setForeground(new Color(0,0,0)); txtLow.setEnabled(true); txtLow.setFont(new Font("sansserif",0,12)); txtLow.setText("0"); txtLow.setVisible(true);

txtMax = new JTextField(); txtMax.setBounds(149,295,90,35); txtMax.setBackground(new Color(255,255,255)); txtMax.setForeground(new Color(0,0,0)); txtMax.setEnabled(true); txtMax.setFont(new Font("sansserif",0,12)); txtMax.setText("0"); txtMax.setVisible(true);

//adding components to contentPane panel contentPane.add(button1); contentPane.add(label1); contentPane.add(label2); contentPane.add(label3); contentPane.add(rbModerate); contentPane.add(rbVigorous); contentPane.add(txtAge); contentPane.add(txtLow); contentPane.add(txtMax);

//adding panel to JFrame and seting of window position and close operation this.add(contentPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); }

//Method mouseClicked for button1 private void mouseButtonClicked (MouseEvent evt) { //TODO try{ int age = Integer.parseInt( txtAge.getText()); } catch (NumberFormatException nfe) { System.out.println("ERROR: Bad age input. Setting age to 18."); txtAge.setText("18"); age = 18; if (rbModerate.isSelected()) { txtLow = (220 - age) * 0.5; } if (rbVigorous.isSelected()) { txtLow = (220 - age) * 0.7; } if (rbModerate.isSelected()) { txtMax = (220 - age) * 0.7; } if (rbVigorous.isSelected()) { txtMax = (220 - age) * 0.85; } txtLow.setText( String.format( "%.2f", txtLow) ); txtMax.setText( String.format("%.2f", txtMax) ); } }

//methof for generate menu public void generateMenu(){ menuBar = new JMenuBar();

JMenu file = new JMenu("File"); JMenu tools = new JMenu("Tools"); JMenu help = new JMenu("Help");

JMenuItem open = new JMenuItem("Open "); JMenuItem save = new JMenuItem("Save "); JMenuItem exit = new JMenuItem("Exit "); JMenuItem preferences = new JMenuItem("Preferences "); JMenuItem about = new JMenuItem("About ");

file.add(open); file.add(save); file.addSeparator(); file.add(exit); tools.add(preferences); help.add(about);

menuBar.add(file); menuBar.add(tools); menuBar.add(help); }

public static void main(String[] args){ System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new GUI_project(); } }); } }

GUI-project Compile Undo Cut Copy Paste Find Close Source Code /TODO try int age = Integer.parselnt( txtage.getText()); catch (NumberFormatException nfe) System.out.println("ERROR: Bad age input. Setting age to txtAge.setText("18" e18: if (rbModerate.isSelected)) ( txtLow = (220 -age) * 0.5; if (rbVigorous.isSelected(O) txtLow = (220 -age) * 0.7; if (rbModerate.isSelected)) txtMax (220 -age) * 0.7; if (rbVigorous.isSelected)) txtMax (220 -age) * 0.85; txtLow.setText( String.format ".2f", txtLow) txtMax.setText( String. format("$.2f", txtHax) //methof for generate menu

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions

Question

What is ment by polymorphism

Answered: 1 week ago