This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Panel vs Fram  RSS feed

 
Tony Jenkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,

I just joined the forum because I have some Questions about Java. At first it was kind of easy to follow but it keeps getting harder. I'm working on an application with a group from school and I'm trying to make a Body Mass Index Calculator.
I was supposed to make a Panel but I made a Frame. My question is how can I make adjustments so the code will run? Below is my code. Previously it extended a JFrame and I changed that into JPanel so now the line: JPanel panel = (JPanel)getContentPane(); gives an error.
I hope I made myself clear, all the help is welcome, and if you see other weird things in my code I appreciate your feedback. Thank you!

package view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
*
* @author Yashech
*/
public class CalculateBMI extends JPanel
{
private JLabel lblHeader;
private JLabel lblHeight;
private JTextField txtHeight;
private JLabel lblM;
private JLabel lblWeight;
private JTextField txtWeight;
private JLabel lblKg;
private JLabel lblBmi;
private JTextField txtBmi;
private JLabel lblBmiOutput;
private JLabel lblAdvice;
private JTextField txtAdvice;
private JLabel lblAdviceOutput;
private JTextField txtAdviceOutput;
private JButton btnProceed;
private JButton btnExit;

public CalculateBMI()
{
lblHeader = new JLabel("BMI Applicatie");
lblHeight = new JLabel("Lengte");
txtHeight = new JTextField(5);
lblM = new JLabel("meter");
lblWeight = new JLabel("Gewicht");
txtWeight = new JTextField(5);
lblKg = new JLabel("kilo's");
lblBmi = new JLabel("BMI");
txtBmi = new JTextField(5);
lblBmiOutput = new JLabel();
lblAdvice = new JLabel("Resultaat");
txtAdvice = new JTextField(5);
lblAdviceOutput = new JLabel();
txtAdviceOutput = new JTextField(5);
btnProceed = new JButton("Bereken");
btnExit = new JButton("Sluiten");

JPanel panel = (JPanel)getContentPane();

//null layout
panel.setLayout(null);
lblHeader.setBounds(120,20,400,30);
lblHeader.setFont(new Font ("Arial", Font.BOLD, 30));
lblHeight.setBounds(50,70,100,20);
txtHeight.setBounds(150,70,80,20);
lblM.setBounds(250,70,40,20);
lblWeight.setBounds(50,100,100,20);
txtWeight.setBounds(150,100,80,20);
lblKg.setBounds(250,100,40,20);
lblBmi.setBounds(50,130,40,20);
// txtBmi.setBounds(150,130,80,20);
lblBmiOutput.setBounds(100,130,100,20);
lblAdvice.setBounds(50,160,150,20);
// txtAdvice.setBounds(150,160,80,20);
lblAdviceOutput.setBounds(50,190,200,20);
// txtAdviceOutput.setBounds(150,190,80,20);
btnProceed.setBounds(270,130,90,20);
btnExit.setBounds(270,160,90,20);

panel.add(lblHeader);
panel.add(lblHeight);
panel.add(txtHeight);
panel.add(lblM);
panel.add(lblWeight);
panel.add(txtWeight);
panel.add(lblKg);
panel.add(lblBmi);
panel.add(txtBmi);
panel.add(lblBmiOutput);
panel.add(lblAdvice);
panel.add(txtAdvice);
panel.add(lblAdviceOutput);
panel.add(txtAdviceOutput);
panel.add(btnProceed);
panel.add(btnExit);

btnProceed.addActionListener(new CalculateBMI.MyListener());
btnExit.addActionListener(new CalculateBMI.MyListener());

// setTitle("Body Mass Index");
setSize(400,250);
setVisible(true);
}

public double calBmi (double bmi)
{
return bmi;
}

public class MyListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnProceed)
{
double weight = 0.0f;
double height = 0.0f;

try
{
height = Double.parseDouble(txtHeight.getText());
weight = Double.parseDouble(txtWeight.getText());
} catch (NumberFormatException ex)
{
lblAdviceOutput.setText("Ongeldige invoer!");
}

double bmi = weight/(height*height);

if (bmi < 13)
lblAdviceOutput.setText("U bent vel over been!!! ");
else if (bmi < 16)
lblAdviceOutput.setText("U bent veel te dun! ");
else if (bmi < 18.5)
lblAdviceOutput.setText("U bent te dun.");
else if (bmi < 25)
lblAdviceOutput.setText("Uw gewicht is goed!");
else if (bmi < 29)
lblAdviceOutput.setText("U bent te dik.");
else if (bmi < 38)
lblAdviceOutput.setText("U bent veel te zwaar!");
else if (bmi >=38)
lblAdviceOutput.setText("U bent gigantisch dik!!!");
else
lblAdviceOutput.setText("Ongeldige invoer!");
}
else if (e.getSource() == btnExit)
System.exit(0);
}
}

public static void main (String [] args)
{
CalculateBMI bmi = new CalculateBMI();
}
}
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the "Code Formatting" tags when posting code.

Don't use setBounds(). Swing was designed to be used with Layout Managers.

Read the Swing tutorial for examples of using layout managers. All the example show how you might better structure your code.

The basic structure of your class could be like:


 
Tony Jenkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply, I will try to change it.
But why is: JPanel panel = (JPanel)getContentPane(); not working when I extend JPanel and only works when i extend JFrame?
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the API for JPanel? It doesn't have a content pane.

You add components to a panel, then you add the panel to the frame.

Read the tutorial. There is a section on "Using Top Level Containers" that may help.

Also, you don't even need to get the content pane of a frame. You just add the panel to the frame and the frame will add it to the content pane for you.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!