0% found this document useful (0 votes)
2 views6 pages

Java App Homepage

The document contains Java code for a GUI application called 'Snack Track' that includes a homepage with buttons for various functionalities such as login, BMI calculator, diet plan, calorie tracker, and food calorie info. The application uses Swing components to create a user-friendly interface and handles button actions to open different windows for each feature. Additionally, it includes a food calorie information section that displays nutritional data for selected food items.

Uploaded by

nileshboy36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Java App Homepage

The document contains Java code for a GUI application called 'Snack Track' that includes a homepage with buttons for various functionalities such as login, BMI calculator, diet plan, calorie tracker, and food calorie info. The application uses Swing components to create a user-friendly interface and handles button actions to open different windows for each feature. Additionally, it includes a food calorie information section that displays nutritional data for selected food items.

Uploaded by

nileshboy36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package appGUI;

import [Link].*;
import [Link].*;
import [Link];
import [Link];

public class Homepage extends JFrame {


public Homepage() {
setTitle("Snack Track - Home");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

// Header
JLabel titleLabel = new JLabel("Welcome to Snack Track",
[Link]);
[Link](new Font("Arial", [Link], 20));
[Link]([Link]);
[Link](true);
[Link](new Color(70, 130, 180));
[Link](new Dimension(500, 50));
add(titleLabel, [Link]);

// Main Panel
JPanel panel = new JPanel(new GridLayout(4, 1, 10, 10));
[Link]([Link](20, 50, 20, 50));

JButton loginButton = new JButton("Login");


JButton bmiButton = new JButton("BMI Calculator");
JButton dietPlanButton = new JButton("Diet Plan");
JButton calorieTrackerButton = new JButton("Calorie Tracker");

styleButton(loginButton);
styleButton(bmiButton);
styleButton(dietPlanButton);
styleButton(calorieTrackerButton);

[Link](loginButton);
[Link](bmiButton);
[Link](dietPlanButton);
[Link](calorieTrackerButton);

add(panel, [Link]);

// Button Actions
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
new LoginSignupPage().setVisible(true);
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
new BMICalculator().setVisible(true);
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
new DietPlan().setVisible(true);
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
new CalorieTracker().setVisible(true);
}
});
}

private void styleButton(JButton button) {


[Link](new Font("Arial", [Link], 14));
[Link](new Color(100, 149, 237));
[Link]([Link]);
[Link](false);
[Link]([Link]());
}

public static void main(String[] args) {


[Link](new Runnable() {
public void run() {
new Homepage().setVisible(true);
}
});
}
}

class BMICalculator extends JFrame {


private JTextField weightField, heightField;
private JLabel resultLabel, categoryLabel;

public BMICalculator() {
setTitle("BMI Calculator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

// Header
JLabel titleLabel = new JLabel("BMI Calculator", [Link]);
[Link](new Font("Arial", [Link], 20));
[Link]([Link]);
[Link](true);
[Link](new Color(70, 130, 180));
[Link](new Dimension(400, 50));
add(titleLabel, [Link]);
// Main Panel
JPanel panel = new JPanel(new GridLayout(4, 2, 10, 10));
[Link]([Link](20, 50, 20, 50));

[Link](new JLabel("Weight (kg):"));


weightField = new JTextField();
[Link](weightField);

[Link](new JLabel("Height (cm):"));


heightField = new JTextField();
[Link](heightField);

JButton calculateButton = new JButton("Calculate");


styleButton(calculateButton);
[Link](calculateButton);

resultLabel = new JLabel("BMI: ", [Link]);


categoryLabel = new JLabel("", [Link]);
[Link](resultLabel);
[Link](categoryLabel);

add(panel, [Link]);

// Action Listener
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculateBMI();
}
});
}

private void styleButton(JButton button) {


[Link](new Font("Arial", [Link], 14));
[Link](new Color(100, 149, 237));
[Link]([Link]);
[Link](false);
[Link]([Link]());
}

private void calculateBMI() {


try {
double weight = [Link]([Link]());
double heightCm = [Link]([Link]());
double heightM = heightCm / 100; // Convert cm to meters

if (weight <= 0 || heightM <= 0) {


throw new NumberFormatException();
}

double bmi = weight / (heightM * heightM);


[Link]("BMI: " + [Link]("%.2f", bmi));
[Link]("Category: " + getBMICategory(bmi));
} catch (NumberFormatException e) {
[Link](this, "Please enter valid positive
numbers.");
}
}

private String getBMICategory(double bmi) {


if (bmi < 18.5) return "Underweight";
else if (bmi < 24.9) return "Normal weight";
else if (bmi < 29.9) return "Overweight";
else return "Obese";
}
}

Food calories info


package appGUI;

import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];

public class Homepage extends JFrame {


public Homepage() {
setTitle("Snack Track - Home");
setSize(500, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

// Header
JLabel titleLabel = new JLabel("Welcome to Snack Track",
[Link]);
[Link](new Font("Arial", [Link], 20));
[Link]([Link]);
[Link](true);
[Link](new Color(255, 140, 0)); // Changed to
Orange
[Link](new Dimension(500, 50));
add(titleLabel, [Link]);

// Main Panel
JPanel panel = new JPanel(new GridLayout(5, 1, 10, 10));
[Link]([Link](20, 50, 20, 50));

JButton loginButton = new JButton("Login");


JButton bmiButton = new JButton("BMI Calculator");
JButton dietPlanButton = new JButton("Diet Plan");
JButton calorieTrackerButton = new JButton("Calorie Tracker");
JButton foodCalorieButton = new JButton("Food Calorie Info");

styleButton(loginButton);
styleButton(bmiButton);
styleButton(dietPlanButton);
styleButton(calorieTrackerButton);
styleButton(foodCalorieButton);

[Link](loginButton);
[Link](bmiButton);
[Link](dietPlanButton);
[Link](calorieTrackerButton);
[Link](foodCalorieButton);

add(panel, [Link]);

// Button Actions
[Link](e -> new
LoginSignupPage().setVisible(true));
[Link](e -> new
BMICalculator().setVisible(true));
[Link](e -> new
DietPlan().setVisible(true));
[Link](e -> new
CalorieTracker().setVisible(true));
[Link](e -> new
FoodCalorie().setVisible(true));
}

private void styleButton(JButton button) {


[Link](new Font("Arial", [Link], 14));
[Link](new Color(255, 140, 0)); // Changed to Orange
[Link]([Link]);
[Link](false);
[Link]([Link]());
}

public static void main(String[] args) {


[Link](() -> new Homepage().setVisible(true));
}
}

class FoodCalorie extends JFrame {


private JComboBox<String> foodSelector;
private JLabel proteinLabel, calorieLabel;
private static final Map<String, String[]> FOOD_DATA = new HashMap<>();

static {
FOOD_DATA.put("Apple", new String[]{"0.3g", "52"});
FOOD_DATA.put("Banana", new String[]{"1.3g", "96"});
FOOD_DATA.put("Chicken Breast", new String[]{"31g", "165"});
FOOD_DATA.put("Salmon", new String[]{"25g", "206"});
FOOD_DATA.put("Lentils", new String[]{"9g", "116"});
FOOD_DATA.put("Tofu", new String[]{"8g", "76"});
}

public FoodCalorie() {
setTitle("Food Calorie Info");
setSize(400, 250);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 1, 10, 10));

JLabel titleLabel = new JLabel("Select a food item:", [Link]);


[Link](new Font("Arial", [Link], 16));

foodSelector = new JComboBox<>(FOOD_DATA.keySet().toArray(new


String[0]));
JButton showButton = new JButton("Show Info");
styleButton(showButton);

proteinLabel = new JLabel("Protein: ", [Link]);


calorieLabel = new JLabel("Calories: ", [Link]);

add(titleLabel);
add(foodSelector);
add(showButton);
add(proteinLabel);
add(calorieLabel);

[Link](e -> displayFoodInfo());


}

private void styleButton(JButton button) {


[Link](new Font("Arial", [Link], 14));
[Link](new Color(255, 140, 0));
[Link]([Link]);
[Link](false);
[Link]([Link]());
}

private void displayFoodInfo() {


String selectedFood = (String) [Link]();
if (selectedFood != null) {
String[] info = FOOD_DATA.get(selectedFood);
[Link]("Protein: " + info[0]);
[Link]("Calories: " + info[1] + " kcal");
}
}
}

You might also like