Hi!
Bitte um Kommentare für diesen Code.
Danke im Voraus
Philipp
Bitte um Kommentare für diesen Code.
Danke im Voraus
Philipp
Code:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainWindow extends JFrame
implements ActionListener
{
static Menu mb = null; //the menu
static JTextField heightInput = new JTextField(); //TextField for the body height
static JTextField weightInput = new JTextField(); //TextField for the weight
static JLabel bmiLabel; //The BMI
static JLabel minWeight; //The minimum weight you should have at you size (BMI = 18,9)
static JLabel maxWeight; //The maximum weight you should have at your size (BMI = 24,9)
static JComboBox systemBox; //The ComboBox to select the measuring system
static byte BMIvalue; //the BMI
static char sex; //the users sex
/** Contains: dataPanel (the upper one) and inforPanel (the lower one) */
public MainWindow()
{
/* creates an array in wich the symbols of the measuring systems are stored
* 0: [cm][kg]
* 1: [foot] [lbs]
*/
final String[][] mesSystem = new String[2][2];
mesSystem[0][0] = "cm"; mesSystem[0][1] = "kg";
mesSystem[1][0] = "foot"; mesSystem[1][1] = "lbs";
//The GUI elements
JPanel main = new JPanel( new GridLayout(0,1) );
getContentPane().add(main);
//dataPanel
JPanel dataPanel = new JPanel();
main.add(dataPanel);
dataPanel.setBorder( BorderFactory.createTitledBorder("Enter your data here") );
//firstLine
JPanel firstLine = new JPanel( new GridLayout(0,2) );
dataPanel.add(firstLine);
firstLine.setBackground(Color.RED);
firstLine.setOpaque(true);
//sizePanel
JPanel sizePanel = new JPanel();
firstLine.add(sizePanel);
JLabel sizeLabel = new JLabel("Size (in cm):");
sizePanel.add(sizeLabel);
heightInput = new JFormattedTextField();
sizePanel.add(heightInput);
//weightPanel
JPanel weightPanel = new JPanel();
firstLine.add(weightPanel);
JLabel weightLabel = new JLabel("Weight (in kg):");
weightPanel.add(weightLabel);
weightInput = new JFormattedTextField();
weightPanel.add(weightInput);
//secondLine
JPanel secondLine = new JPanel();//( new GridLayout(2,1) );
dataPanel.add(secondLine);
//systemPanel
JPanel systemPanel = new JPanel( new FlowLayout(FlowLayout.LEFT) );
secondLine.add(systemPanel);
String[] systems = { "Metric system", "Imperial system" };
systemBox = new JComboBox(systems);
systemPanel.add(systemBox);
systemBox.addActionListener(this);
//buttonPanel
JPanel buttonPanel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
secondLine.add(buttonPanel);
JButton calcButton = new JButton("Calculate BMI");
buttonPanel.add(calcButton);
getRootPane().setDefaultButton(calcButton);
calcButton.setActionCommand("b");
calcButton.addActionListener(this);
//infoPanel
JPanel infoPanel = new JPanel( new GridLayout(0,1) );
main.add(infoPanel);
infoPanel.setBorder( BorderFactory.createTitledBorder("Information") );
//bmiPanel (contains bmiLabel)
JPanel bmiPanel = new JPanel( new FlowLayout(FlowLayout.CENTER) );
infoPanel.add(bmiPanel);
//bmiLabel
bmiLabel = new JLabel("Your BMI is: ");
bmiPanel.add(bmiLabel);
//weightInfoPanel (contains all information regarding to weight)
JPanel weightInfoPanel = new JPanel( new FlowLayout(FlowLayout.CENTER) );
infoPanel.add(weightInfoPanel);
//minWeight
minWeight = new JLabel("Minimum weight: ");
weightInfoPanel.add(minWeight);
//maxWeight
maxWeight = new JLabel("Maximum weight: ");
weightInfoPanel.add(maxWeight);
//Window Preferences
setJMenuBar( mb = new Menu() ); //set the menubar
setSize(300,400);
setResizable(false);
setTitle(AboutWindow.appName+" "+AboutWindow.version);
setLocationRelativeTo(null);
setVisible(true);
}
/** A message that occurs, when any type of input failure happens */
public void failureMessage(String title, String mainMessage, String littleMessage)
{
JOptionPane.showMessageDialog(
this,
"<html>"+
"<font size='+0'>"+mainMessage+"</font></html>"+"\n"+
littleMessage,
title,
JOptionPane.ERROR_MESSAGE,
null
);
}
public void actionPerformed(ActionEvent e)
{
String c = e.getActionCommand();
//"calculate" is pressed
if(c.equals("b"))
{
//check if values are entered
if( weightInput.getText().length() <1 || heightInput.getText().length() <1 )
{
failureMessage("No data prompted","You didn't enter enough values!", "Please complete your input.");
} else
{
try{
byte weight = Byte.parseByte( weightInput.getText() ); //gets the weight
byte height = Byte.parseByte( heightInput.getText() ); //gets the height
//calculate the BMI
BMIvalue = BMI.getBMI( weight, height, sex );
//format the lenght of the returned value
DecimalFormat df = new DecimalFormat("0.0"); //format: xxx.x
bmiLabel.setText("Your BMI is: "+df.format(BMIvalue) );
/** Calculate min and maxWeight */
//get the MinWeight and display it
minWeight.setText("Minimum weight: "+df.format( BMI.getMinWeight(height, sex) )+" kg" );
//get the MaxWeight and display it
maxWeight.setText("Maximal weight: "+df.format( BMI.getMaxWeight(height, sex) )+" kg" );
} catch (NumberFormatException e1)
{
failureMessage("Wrong format!", "The data you have entered has the wrong format!", "Only use numbers and no commas, please.");
}
}
}
}
}