Was halter iht von diesem Code?

Status
Nicht offen für weitere Antworten.

materthron

Mitglied
Hi!
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.");
            }
          }
        }
    }

}
 
Dass ich nicht weiß, was ich sagen soll.
Willst du zum Codestil Anmerkungen? Testen kann mans nicht, weil Klassen fehlen.
 
Illuvatar hat gesagt.:
Dass ich nicht weiß, was ich sagen soll.
Willst du zum Codestil Anmerkungen? Testen kann mans nicht, weil Klassen fehlen.

Ich schließe mich dieser Meinung an, sag was du wissen möchtest, dann wird dir auch geholfen.
 
Code:
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

Ich finds äusserst schlecht, wenn man "static" an einem Ort einsetzt, wo es absolut nix zu suchen hat.
Denn so kann nur ein einziges MainFrame erstellt werden. (Und so wirklich OOP ist das auch nicht...)

[ Edit: und diese Variablen sollten auch private sein, schliesslich gehen sie nur das MainFrame was an. Andere Klassen sollten, wenn überhaubt, mit einem Getter / Setter zugreiffen. ]
 
Code:
/* 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";

Wozu definiert man sowas in nem 2-dimensionalen Array. Zudem benutzt du das net mals^^

Und ich find auch das du mal alles private setzen solltest was nicht von anderen Klassen benötigt wird bzw ich schliess mich im Rest mal Beni an. (getter/setter fehlen + Geheimnisprinzip (oder wie das heisst) wär scho was 🙂)
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben