How to call a method that has a JPanel over multiple instances in Java [on hold]

54
January 11, 2019, at 09:50 AM

I'm trying to make multiple objects with of a class (the Tic class), each with its own JPanel with buttons and stuff. Right now when I make instances of the class they all add their buttons to the same JFrame. Here's my code: The tic class:

    public class Tic{    
private static JPanel p = new JPanel();  
private static String OX;
private static String LASTOX;
private static int inArow;
private static int turn = 0;
private static String winner;
private static ArrayList<JButton> btns = new ArrayList<>();
public Tic() {  
    p.setLayout(new GridLayout(3, 3, 25, 25));  
    p.setBounds(300, 300, 400, 400); 
    p.setBackground(Color.BLACK);

   for(int i = 1; i <=9; i++) {
       JButton e = new JButton();
       btns.add(e);
       e.setFont(new Font("SansSerifBold", Font.PLAIN, 70));
       e.setFocusPainted(false);
       e.setText("");
       e.setVisible(true);
       p.add(e);
       e.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent a) {
                clicked(e);
            }
       });  
   }

   p.setVisible(true);

}  
public static void clicked(JButton e) {
    if(turn%2 == 0)
        e.setText("X");
    else
        e.setText("O");
    //Rows
    for(int i = 0; i <= 6; i+=3) {
        OX = "STOP";
        LASTOX = "NOT";
        inArow = 1;
        for(int j = 0; j < 3; j++) {
            OX = btns.get(i+j).getText();
            if(OX.equals("")) {
                continue;
            }
            if(OX.equals(LASTOX))
                inArow++;
            LASTOX = OX;
            if(inArow == 3) {
                //setWinner(OX);
                win();
            }
        }
    }
    //Columns
    for(int i = 0; i <= 2; i++) {
        OX = "STOP";
        LASTOX = "NOT";
        inArow = 1;
        for(int j = 0; j <= 6; j+=3) {
            if(OX.equals("")) {
                continue;
            }
            OX = btns.get(i+j).getText();
            if(OX.equals(LASTOX))
                inArow++;
            LASTOX = OX;
            if(inArow == 3) {
                //setWinner(OX);
                win();
            }
        }
    }
    //Diagonals
        OX = "STOP";
        LASTOX = "NOT";
        inArow = 1;
        for(int j = 0; j <= 8; j+=4) {
            if(OX.equals("")) {
                continue;
            }
            OX = btns.get(j).getText();
            if(OX.equals(LASTOX))
                inArow++;
            LASTOX = OX;
            if(inArow == 3) {
                //setWinner(OX);
                win();
            }
        }

        OX = "STOP";
        LASTOX = "NOT";
        inArow = 1;
        for(int j = 2; j <= 6; j+=2) {
            if(OX.equals("")) {
                continue;
            }
            OX = btns.get(j).getText();
            if(OX.equals(LASTOX))
                inArow++;
            LASTOX = OX;
            if(inArow == 3) {
                //setWinner(OX);
                win();
            }
        }
    turn++;
}
public static void win() {
    System.out.println("done");
}
public JPanel getJP() {
    return Tic.p;
}
}

The class making multiple objects of Tic:

public class Game {
public static void main(String[] args) { 
    JFrame f = new JFrame();
    ArrayList<Tic> tics = new ArrayList<>();
    f.setBounds(10, 10, 300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for(int i = 0; i < 9; i++) {
        Tic t = new Tic();
        tics.add(t);
    }
    f.setContentPane(tics.get(0).getJP());
    f.setVisible(true);
}
}

TL:DR When making multiple instances of a class as an object they share variables(the JPanel).

All help is appreciated!

READ ALSO
How to turn arg or string into User?

How to turn arg or string into User?

I am making a command and I need to get a list of roles for a user, the problem is that an argument is a string and getMember() expects a UserHow do I turn a string into a User or is there just a better way?

37
PDFBox: do PDDocument and PDPage have references to one another?

PDFBox: do PDDocument and PDPage have references to one another?

Does a PDPage object contains a reference to the PDDocument to which it belongs? In other words, does a PDPage has knowledge of its PDDocument? Somewhere in the application I have a list of PDDocumentsThese documents get merged into one new PDDocument:

40
How to send youtube-dl stdout to browser to save as a file?

How to send youtube-dl stdout to browser to save as a file?

I am making a php frontend for youtube-dlI am getting stdout from youtube-dl

27
Get the required fields for Google Tag Manager

Get the required fields for Google Tag Manager

I am currently integrating a google tag manager in one of my Silverstripe websites and I require to programatially retrieve details of Product name, order total price and currencyI have retrieved the other details like order id, coupon code but I am unable...

57