nikitakrut58

prac4

Sep 23rd, 2020
768
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. class Main extends JFrame {
  8.     int milanScore  = 0;
  9.     int madridScore = 0;
  10.  
  11.     JButton btn1 = new JButton("AC Milan");
  12.     JButton btn2 = new JButton("Real Madrid");
  13.  
  14.     JLabel text1 = new JLabel("Result: 0 X 0");
  15.     JLabel text2 = new JLabel("Last Scorer: N/A");
  16.     Label  text3 = new Label("Winner: N/A");
  17.  
  18.     void printResults()
  19.     {
  20.         text1.setText(String.format("Result: %d X %d", milanScore, madridScore));
  21.  
  22.         if (milanScore > madridScore) text3.setText("Winner: AC Milan");
  23.         else text3.setText("Winner: Real Madrid");
  24.     }
  25.  
  26.     public Main() {
  27.         super("practic 4");
  28.         super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  29.  
  30.         setSize(300, 300);
  31.         setLayout(new FlowLayout());
  32.  
  33.         add(btn1);
  34.         add(btn2);
  35.  
  36.         add(text1);
  37.         add(text2);
  38.         add(text3);
  39.  
  40.         btn1.addActionListener(ae -> {
  41.             milanScore++;
  42.             text2.setText("Last Scorer: AC Milan");
  43.  
  44.             printResults();
  45.         });
  46.  
  47.         btn2.addActionListener(ae-> {
  48.             madridScore++;
  49.             text2.setText("Last Scorer: Real Madrid");
  50.  
  51.             printResults();
  52.         });
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         new Main().setVisible(true);
  57.     }
  58. }
RAW Paste Data