Hello,
I am facing this difficulty here : First I'll give code
public class Game extends JFrame {
private JPanel contentPane;
//private JPanel settingPane;
protected JLabel lblPlayerName2; // I am using this one for my testing
JLabel[] lbls = new JLabel[9];
private final ButtonGroup buttonGroup = new ButtonGroup();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Game() {
setTitle("Tic Tac Toe");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 810, 607);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setName("Tic Tac Toe");
.
.
.
.
.
for (int i=0; i<lbls.length; i++) {
lbls[i] = new JLabel(""+(i+1));
lbls[i].addMouseListener(new AllbtnBehvr());
lbls[i].setFont(new Font("Trebuchet MS", Font.BOLD, 95));
lbls[i].setHorizontalAlignment(SwingConstants.CENTER);
lbls[i].setBorder(new LineBorder(new Color(0, 0, 0), 2));
panelGameArea.add(lbls[i]);
}
.
.
.
.
} // End of Game()
class AllbtnBehvr implements MouseInputListener{ //Created this class as SubClass of Game Class
@Override
public void mouseClicked(MouseEvent src) {
// TODO Auto-generated method stub
JLabel b = (JLabel) src.getSource();
lblPlayerName2.setText(b.getText());
. }
.
.
.
.
} // End of AllbtnBehvr
}// End of Game Class
Here, I observed that, variable 'lblPlayerName2' and lbls[] defined outside of Game() Constructor, and it can be accessed, but other variables inside Game() cannot be accessed. Here, I want to ask,
How can I access to variables inside Game()? and
How can I access to variables inside Game() if I create class outside Game Class?
Thanks,
Hakoo Desai.