I\'m having trouble getting a text area to appear in this program. I need a text
ID: 3805144 • Letter: I
Question
I'm having trouble getting a text area to appear in this program. I need a text area to the right that displays the following information:
Membership Cost: $400.00
Training Sessions Cost: $200.00
Discounts:
Seniors - 30$ off Membership.
Purchase of 12 or more months : 15% off membership.
5 or more training sessions: 20% off each training session.
Here is the code that I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClubMembership extends JFrame {
private JLabel seniorL, monthsL, tSessL, memCostL;
private JTextField seniorTF, monthsTF, tSessTF, memberCostTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
private static final int WIDTH = 800;
private static final int HEIGHT = 500;
public ClubMembership() {
//Create the four labels
seniorL = new JLabel("Are you a senior? (1 for yes / 2 for no): ",
SwingConstants.RIGHT);
monthsL = new JLabel("Enter the months: ",
SwingConstants.RIGHT);
tSessL = new JLabel("Enter the number of training sessions: ",
SwingConstants.RIGHT);
memCostL = new JLabel("Your total membership cost is: $",
SwingConstants.CENTER);
//Create the four text fields
seniorTF = new JTextField(5);
monthsTF = new JTextField(5);
tSessTF = new JTextField(5);
memberCostTF = new JTextField(10);
//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Set the title of the window
setTitle("Gym Membership Calculator");
//Get the container
Container pane = getContentPane();
//Set the layout
pane.setLayout(new GridLayout(5, 3));
//Place the components in the pane
pane.add(seniorL);
pane.add(seniorTF);
pane.add(monthsL);
pane.add(monthsTF);
pane.add(tSessL);
pane.add(tSessTF);
pane.add(memCostL);
pane.add(memberCostTF);
pane.add(calculateB);
pane.add(exitB);
//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int senior, months, tSess;
double totalCost;
senior = Integer.parseInt(seniorTF.getText());
months = Integer.parseInt(monthsTF.getText());
tSess = Integer.parseInt(tSessTF.getText());
//Calculations
GymMembershipCalc a = new GymMembershipCalc(senior, months, tSess);
totalCost = a.getTotalCost();
//Outputs totalCost into existing textfield.
memberCostTF.setText(totalCost+"");
}
}
private class ExitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
ClubMembership rectObject = new ClubMembership();
}
}
Explanation / Answer
Correct code is :-
seniorL = new JLabel("Are you a senior? (1 for yes / 2 for no): ",
JLabel.RIGHT);
monthsL = new JLabel("Enter the months: ",
JLabel.RIGHT);
tSessL = new JLabel("Enter the number of training sessions: ",
JLabel.RIGHT);
memCostL = new JLabel("Your total membership cost is: $",
JLabel.CENTER);
Wherever SwingConstants are used , you need to use JLabel. Now it will work properly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.