16-04-2011, 01:55
|
|
|
חבר מתאריך: 02.12.09
הודעות: 19
|
|
problem JAVA ActionListener
היי
אני בונה את המשחק איקס עיגול בJAVA, יש לי בעיה עם הactionlistener הוא מחזיר לי שגיאה יש למישהו רעיון תודה מראש לעוזרים... ועוד שאלה איך אני מעלה פה קוד בצורה נורמלית
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
publicclass tic_tac_toe extends JApplet implements ActionListener //,ItemListener
{
boolean[] empty={true, true,true,true,true,true,true,true,true};
JPanel panel[] =new JPanel[9];
booleanTwoPlayer= true, startWithRed= true; /// start with red mean the user start to play with red else(false) yellow
////////////////////////////////////////
JMenuBar bar;
JMenu game;
JMenu color;
JMenuItem p1;
JMenuItem p2;
JRadioButton red;
JRadioButton yellow;
/////////////////////////////////////////
publicvoid init()
{
addMouseListener(new myMouseListener());
}
public tic_tac_toe()
{
setLayout(new GridLayout(3,3,10,10));
for (int l=0; l<9; l++)
{
panel[l]= new JPanel();
add(panel[l]);
panel[l].setLayout(new BorderLayout());
// panel[l].addContainerListener(null);
}
//////////////////////////////////////////////////////
buildmenu();
}
privatevoid buildmenu()
{
bar= new JMenuBar();
buildgame();
buildcolor();
bar.add(game);
bar.add(color);
setJMenuBar(bar); ////// why doesn't work i want to add the bar to the JApplet
}
privatevoid buildgame()
{
p1= new JMenuItem("One player");
p1.addActionListener(new PlayerNum()); /////
p2= new JMenuItem("Two player");
p2.addActionListener(new PlayerNum()); /////
}
privatevoid buildcolor()
{
red= new JRadioButton("Play with red", true);
red.addActionListener(new PlayerNum()); ///// need to add action
yellow= new JRadioButton("Play with yellow");
yellow.addActionListener(new PlayerNum()); ///// need to add action
}
privateclass PlayerNum implements ActionListener ////// action for the menu
{
publicvoid actionPerformed(ActionEvent player)
{
if (p1.isSelected())
TwoPlayer= false;
if (p2.isSelected())
TwoPlayer= true;
if (yellow.isSelected())
startWithRed= false;
if (red.isSelected())
startWithRed= true;
}
}
privateclass myMouseListener extends MouseAdapter //implements ActionListener ///////// panel click
{
publicvoid mousePressed(MouseEvent event)
{
for (int i=0; i<9; i++)
{
if (event.equals(panel[i]))
{
if (empty[i])
{
empty[i]= false;
if (startWithRed)
{
//// draw red
}
}
}
}
}
}
}
נערך לאחרונה ע"י birendorf בתאריך 16-04-2011 בשעה 01:58.
|