Hello,
Thank you for reading my message!
I have an applet, where I make two instances of the custom JPanel objects and I add them into two JFrames and display them. Both of these panels have a 10 x 10 grid of buttons and an event handler is added on each of those buttons. When I run the program, an applet window and two JFrames with 10 x 10 grids appear. When I click on a button in the first JPanel, an 'X' or an 'O' appears on the button. So far, so good.
What I want is the following:
After I click on a button in the first JPanel (say 10th button and 'X 'appears), I want the 10th button in the second JPanel to be immediately set to 'X'.
I would be grateful for any help. Please show me a sample program if I need a new concept to solve this problem. My files are below.
Thank you!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TictacToe extends JApplet
{
private BattleShipH h;
private BattleShipC c;
private JFrame frame1, frame2;
public void init()
{
h = new BattleShipH();
frame1 = new JFrame("Human");
frame1.add(h);
frame1.setSize(800, 600);
frame1.setVisible(true);
c = new BattleShipC();
frame2 = new JFrame("Computer");
frame2.add(c);
frame2.setSize(800, 600);
frame2.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BattleShipH extends JPanel
{
private JButton[] buttons = new JButton [100];
private GridLayout grid;
private int cell = 0, click = 0;
public BattleShipH ()
{
grid = new GridLayout (10, 10, 5, 5);
setLayout(grid);
for (int i = 0; i <100; i++)
{
buttons[i] = new JButton (String.valueOf(i));
buttons[i].setBackground(Color.GRAY);
//buttons[i].setFont(new Font ("Times New Roman", Font.BOLD, 0));
//buttons[i].addActionListener (new Listener());
buttons[i].addActionListener (new ClickHandler());
add(buttons[i]);
}
}
public void setClicked(int value)
{
cell = value;
buttons[cell].setFont(new Font ("Times New Roman", Font.BOLD, 50));
click++;
if (click % 2 == 0)
{
buttons[cell].setText("X");
}
else
{
buttons[cell].setText("O");
}
}
public int getClicked()
{
return cell;
}
private class ClickHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JButton button = (JButton) e.getSource();
JOptionPane.showMessageDialog(null, "Here");
if (button.getText().equals("O") || button.getText().equals("X"))
{
JOptionPane.showMessageDialog (null, "Entry has already been made");
}
else
{
cell = Integer.parseInt(button.getText());
click++;
buttons[cell].setFont(new Font ("Times New Roman", Font.BOLD, 50));
if (click % 2 == 0)
{
buttons[cell].setText("X");
}
else
{
buttons[cell].setText("O");
}
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BattleShipC extends JPanel
{
private JButton[] buttons = new JButton [100];
private GridLayout grid;
private int cell = 0, click = 0;
public BattleShipC ()
{
grid = new GridLayout (10, 10, 5, 5);
setLayout(grid);
for (int i = 0; i <100; i++)
{
buttons[i] = new JButton (String.valueOf(i));
buttons[i].setBackground(Color.GRAY);
//buttons[i].setFont(new Font ("Times New Roman", Font.BOLD, 0));
buttons[i].addActionListener (new Listener());
add(buttons[i]);
}
}
public void setClicked(int value)
{
cell = value;
buttons[cell].setFont(new Font ("Times New Roman", Font.BOLD, 50));
click++;
if (click % 2 == 0)
{
buttons[cell].setText("X");
}
else
{
buttons[cell].setText("O");
}
}
public int getClicked()
{
return cell;
}
private class Listener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JButton button = (JButton) e.getSource();
if (button.getText().equals("O") || button.getText().equals("X"))
{
JOptionPane.showMessageDialog (null, "Entry has already been made");
}
else
{
cell = Integer.parseInt(button.getText());
click++;
buttons[cell].setFont(new Font ("Times New Roman", Font.BOLD, 50));
if (click % 2 == 0)
{
buttons[cell].setText("X");
}
else
{
buttons[cell].setText("O");
}
}
}
}
}