0% found this document useful (0 votes)
9 views7 pages

Oop Final Project

OOP final project

Uploaded by

merajgohar403
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)
9 views7 pages

Oop Final Project

OOP final project

Uploaded by

merajgohar403
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
You are on page 1/ 7

OOP FINAL PROJECT

Rao Meraj Gohar


BSSE-2nd
MTN-SP-000153
OOP in Java
A Football Match
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

// FootballTeam class to represent a team


class FootballTeam {
private final String name;
private int goals;

public FootballTeam(String name) {


this.name = name;
this.goals = 0;
}

public String getName() {


return name;
}

public int getGoals() {


return goals;
}
public void scoreGoal() {
goals++;
}
}

// FootballMatchGUI class for the GUI


public class FootballMatchGUI extends JFrame {
private final FootballTeam teamA;
private final FootballTeam teamB;

private final JLabel scoreLabel;


private final JLabel resultLabel;

public FootballMatchGUI(String teamAName, String


teamBName) {
// Initialize the teams
teamA = new FootballTeam(teamAName);
teamB = new FootballTeam(teamBName);

// Set up the GUI


setTitle("Football Match Tracker");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 1));

// Display the match title


JLabel matchLabel = new JLabel("Match: " +
teamA.getName() + " vs " + teamB.getName(),
SwingConstants.CENTER);
matchLabel.setFont(new Font("Arial", Font.BOLD, 16));
add(matchLabel);

// Display the score


scoreLabel = new JLabel(getScoreText(),
SwingConstants.CENTER);
scoreLabel.setFont(new Font("Arial", Font.PLAIN, 14));
add(scoreLabel);

// Add buttons for scoring


JPanel buttonPanel = new JPanel();
JButton teamAButton = new JButton(teamA.getName() +
" Scores");
JButton teamBButton = new JButton(teamB.getName() +
" Scores");
buttonPanel.add(teamAButton);
buttonPanel.add(teamBButton);
add(buttonPanel);

// Add the result label


resultLabel = new JLabel("Match is ongoing...",
SwingConstants.CENTER);
resultLabel.setFont(new Font("Arial", Font.ITALIC, 14));
add(resultLabel);

// Add button actions


teamAButton.addActionListener(e -> {
teamA.scoreGoal();
updateScore();
});

teamBButton.addActionListener(e -> {
teamB.scoreGoal();
updateScore();
});
}

private String getScoreText() {


return "Score: " + teamA.getName() + " " +
teamA.getGoals() + " - " + teamB.getGoals() + " " +
teamB.getName();
}

private void updateScore() {


scoreLabel.setText(getScoreText());
// Update the match result dynamically
if (teamA.getGoals() > teamB.getGoals()) {
resultLabel.setText("Current Leader: " +
teamA.getName());
} else if (teamB.getGoals() > teamA.getGoals()) {
resultLabel.setText("Current Leader: " +
teamB.getName());
} else {
resultLabel.setText("It's a draw!");
}
}

public static void main(String[] args) {


// Start the GUI
SwingUtilities.invokeLater(() -> {
FootballMatchGUI gui = new FootballMatchGUI("Team
A", "Team B");
gui.setVisible(true);
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy