Java Simple Simulator

1.4k views Asked by At

I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i click simulate a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;



public class sim extends JPanel {

  public sim() {
    // JFrame constructor
      super(true);

    JRadioButton chelsea, arsenal, chelsea2, arsenal2;

    this.setLayout(new GridLayout(3,0));

    ButtonGroup group = new ButtonGroup();
    ButtonGroup group2 = new ButtonGroup();

    // takes image and saves it the the variable
    Icon a = new ImageIcon(getClass().getResource("a.PNG"));
    Icon c = new ImageIcon(getClass().getResource("c.JPG"));


    chelsea = new JRadioButton("Chelsea",c);
    chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal = new JRadioButton("Arsenal",a);
    arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);

    group.add(chelsea);
    group.add(arsenal);

    JLabel label = new JLabel("");
    TitledBorder titled = new TitledBorder("Team 1");
    label.setBorder(titled);

    chelsea.setBorder(titled);
    arsenal.setBorder(titled);

    JButton button = new JButton("Simulate");
    button.setHorizontalAlignment(JButton.CENTER);
    add(button, BorderLayout.CENTER);

    chelsea2 = new JRadioButton("Chelsea",c);
    chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal2 = new JRadioButton("Arsenal",a);
    arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);

    group2.add(chelsea2);
    group2.add(arsenal2);

    JLabel label2 = new JLabel("");
    TitledBorder titled2 = new TitledBorder("Team 2");
    label2.setBorder(titled2);

    chelsea2.setBorder(titled);
    arsenal2.setBorder(titled);

    add(label);
    add(chelsea);
    add(arsenal);

    add(button);

    add(chelsea2);
    add(arsenal2);

    HandlerClass handler = new HandlerClass();
    chelsea.addActionListener(handler);
  }

  private class HandlerClass implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {

          //JOptionPane.showMessageDialog(null, String.format("%s",     e.getActionCommand()));

      }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Final");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1920, 1080);
    frame.setContentPane(new sim());
    frame.setVisible(true);
  }
}
1

There are 1 answers

2
Mehdi On

Firstly capitalize the name of your class always in Java, then check this code :

public class Sim extends JPanel {

    public Sim() {
        // JFrame constructor
        super(true);

        JRadioButton chelsea, arsenal, chelsea2, arsenal2;

        this.setLayout(new GridLayout(3,0));

        ButtonGroup group = new ButtonGroup();
        ButtonGroup group2 = new ButtonGroup();

        // takes image and saves it the the variable
        Icon a = new ImageIcon("/home/mehdi/Pictures/ICONS/Test/1.png");
        Icon c = new ImageIcon("/home/mehdi/Pictures/ICONS/Test/2.png");


        chelsea = new JRadioButton("Chelsea",c);
        chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
        chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);

        arsenal = new JRadioButton("Arsenal",a);
        arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
        arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);

        group.add(chelsea);
        group.add(arsenal);

        final JLabel label = new JLabel("");
        TitledBorder titled = new TitledBorder("Team 1");
        label.setBorder(titled);

        chelsea.setBorder(titled);
        arsenal.setBorder(titled);

        JButton button = new JButton("Simulate");
        button.setHorizontalAlignment(JButton.CENTER);
        add(button, BorderLayout.CENTER);

        chelsea2 = new JRadioButton("Chelsea",c);
        chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
        chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);

        arsenal2 = new JRadioButton("Arsenal",a);
        arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
        arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);

        group2.add(chelsea2);
        group2.add(arsenal2);

        JLabel label2 = new JLabel("");
        TitledBorder titled2 = new TitledBorder("Team 2");
        label2.setBorder(titled2);

        chelsea2.setBorder(titled);
        arsenal2.setBorder(titled);

        add(label);
        add(chelsea);
        add(arsenal);

        add(button);

        add(chelsea2);
        add(arsenal2);

        button.addActionListener(new HandlerClass(label));
    }

    private class HandlerClass implements ActionListener{

        final JLabel label;
        public HandlerClass(JLabel label){
             this.label = label;
        }
        public void actionPerformed(ActionEvent e)
        {
            label.setText("SSSSSSSSSSSSSsssss");
            JOptionPane.showMessageDialog(null, "Something");
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Final");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1920, 1080);
        frame.setContentPane(new Sim());
        frame.setVisible(true);
    }
}