Final Oop Project
Final Oop Project
// Main class
public class CricketMatchGUI {
public static void main(String[] args) {
// Create an instance of CricketMatch and display the
GUI
CricketMatch match = new CricketMatch();
match.display();
}
}
// CricketMatch class
class CricketMatch {
private JFrame frame;
private JTextField team1Field, team2Field,
team1ScoreField, team2ScoreField;
private JLabel resultLabel;
// Constructor to initialize GUI components
public CricketMatch() {
// Create the main frame
frame = new JFrame("Cricket Match Score");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new GridLayout(5, 2, 10, 10));
// Labels
JLabel team1Label = new JLabel("Team 1 Name:");
JLabel team2Label = new JLabel("Team 2 Name:");
JLabel team1ScoreLabel = new JLabel("Team 1 Score:");
JLabel team2ScoreLabel = new JLabel("Team 2 Score:");
resultLabel = new JLabel("Result: ",
SwingConstants.CENTER);
// Text Fields
team1Field = new JTextField();
team2Field = new JTextField();
team1ScoreField = new JTextField();
team2ScoreField = new JTextField();
// Button to calculate the result
JButton calculateButton = new JButton("Calculate
Result");
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateResult();
}
});
if (team1.isEmpty() || team2.isEmpty()) {
resultLabel.setText("Error: Enter team names!");
return;
}
try {
int team1Score =
Integer.parseInt(team1ScoreField.getText().trim());
int team2Score =
Integer.parseInt(team2ScoreField.getText().trim());
if (team1Score > team2Score) {
resultLabel.setText("Result: " + team1 + " Wins!");
} else if (team2Score > team1Score) {
resultLabel.setText("Result: " + team2 + " Wins!");
} else {
resultLabel.setText("Result: It's a Draw!");
}
} catch (NumberFormatException e) {
resultLabel.setText("Error: Enter valid numeric
scores!");
}
}
}