Advertisement
Toxotsist

T4

Sep 9th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.plaf.basic.BasicButtonListener;
  5. import javax.swing.plaf.basic.BasicOptionPaneUI;
  6.  
  7. public class Window extends JFrame {
  8.     private int a = 0;
  9.     private int b = 0;
  10.     protected String win;
  11.     private JButton butt = new JButton("AC Milan");
  12.     private JButton butt2 = new JButton("Real Madrid");
  13.     private JLabel lab1 = new JLabel("Result: " + a + " X " + b);
  14.     private JLabel lab2 = new JLabel("Last Scorer: ");
  15.     private JLabel Winner = new JLabel();
  16.  
  17.  
  18.     public String getWinner(){
  19.         if (a > b) {win = "AC Milan";}
  20.         else if (a < b) {win = "Real Madrid";}
  21.         else {win = "Draw";}
  22.         return win;
  23.     };
  24.  
  25.     public Window(){
  26.         super("Match");
  27.         this.setBounds(200,200,640,640);
  28.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         lab1.setHorizontalAlignment(lab1.CENTER);
  30.         lab2.setHorizontalAlignment(lab2.CENTER);
  31.         Winner.setHorizontalAlignment(Winner.RIGHT);
  32.         butt.addActionListener(new ActionListener() {
  33.             @Override
  34.             public void actionPerformed(ActionEvent e) {
  35.                 a++;
  36.                 lab1.setText("Result: " + a + " X " + b);
  37.                 lab2.setText("Last Scorer: AC Milan");
  38.                 Winner.setText("Winner: " + getWinner());
  39.             }
  40.         });
  41.         butt2.addActionListener(new ActionListener() {
  42.             @Override
  43.             public void actionPerformed(ActionEvent e) {
  44.                 b++;
  45.                 lab1.setText("Result: " + a + " X " + b);
  46.                 lab2.setText("Last Scorer: Real Madrid");
  47.                 Winner.setText("Winner: " + getWinner());
  48.             }
  49.         });
  50.  
  51.  
  52.         JPanel panel = new JPanel();
  53.         GridLayout layout = new GridLayout(3,2,5,5);
  54.         panel.setLayout(layout);
  55.         panel.add(lab1);
  56.         panel.add(lab2);
  57.         panel.add(butt);
  58.         panel.add(butt2);
  59.         panel.add(Winner);
  60.         getContentPane().add(panel);
  61.         setVisible(true);
  62.     }
  63.  
  64.  
  65.  
  66.     public static void main(String[] args){
  67.         Window gui = new Window();
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement