الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في الكود

مغلق
بدأه ABDULLAH1003 في 21 يوليو 2004 · 5 رد · 978 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

شباب انا مبتدى في الجافا وعندي خطأو ما عرفت اصححه تكفون ساعدوني

وهذ البرنامج

package aa;

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

public class XO extends JFrame {
     public JButton  position[][];
     private  ButtonHandler b;

  public XO() {
   super("Tic Tac Toe Game");



   Container boardPanel=getContentPane();
   boardPanel.setLayout(new FlowLayout());
       ButtonHandler handler=new ButtonHandler();
   //create button
     position=new JButton[9][9];
 // int   dim = 9 * 55;



    boardPanel.setLayout(new GridLayout(9, 9));


    for (int i = 0; i < 9; ++i)
    {
      for (int j = 0; j <9; ++j)
      {
        position[j] = new JButton("?");
        position[j].addActionListener(handler);


        boardPanel.add(position[j]);
       // position[j].setEnabled(enabled);
      }
    }
       setSize(300,300);
       setVisible(true);
  } //end constactor

   public static void main(String args[])
   {
   XO ap= new XO();

     ap.addWindowListener (
   new WindowAdapter(){
   public void windowClosing(WindowEvent e)
    {System.exit (0); }
     }
     );
     } //end main


   private class  ButtonHandler implements ActionListener{

   public void actionP(ActionEvent e)
   {
   JOptionPane.showInternalMessageDialog(XO.this,"hello");
   }



  }


     } //end of XO class
#2
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class XO extends JFrame {
    public JButton  position[][];
    private  ButtonHandler b;

 public XO() {
  super("Tic Tac Toe Game");



  Container boardPanel=getContentPane();
  boardPanel.setLayout(new FlowLayout());
      ButtonHandler handler=new ButtonHandler();
  //create button
    position=new JButton[9][9];
// int   dim = 9 * 55;



   boardPanel.setLayout(new GridLayout(9, 9));


   for (int i = 0; i < 9; ++i)
   {
     for (int j = 0; j <9; ++j)
     {
       position[j] = new JButton("?");
       position[j].addActionListener(handler);


       boardPanel.add(position[j]);
      // position[j].setEnabled(enabled);
     }
   }
      setSize(300,300);
      setVisible(true);
 } //end constactor

  public static void main(String args[])
  {
  XO ap= new XO();

    ap.addWindowListener (
  new WindowAdapter(){
  public void windowClosing(WindowEvent e)
   {System.exit (0); }
    }
    );
    } //end main


  private class  ButtonHandler implements ActionListener{

  public void actionPerformed(ActionEvent e)
  {
  JOptionPane.showInternalMessageDialog(XO.this,"hello");
  }



 }


    } //end of XO class

تم تعديل هذه المشاركة بواسطة ahabdeen في 21 يوليو 2004 في 20:20

ahabdeen.jpg

<marquee>

مش موجود بصفة دايمة للامتحانات

</marquee>

#3

بما أنك تحقق الـ Interface

ActionListener يجب أن تقوم بتحقيق الطريقة actionPerformed و أنت لم تقم بذلك حيث أنك عرفت طريقة أخرى public void actionP في السطر 59 .

#4

بما أنك تحقق الـ Interface

ActionListener يجب أن تقوم بتحقيق الطريقة actionPerformed و أنت لم تقم بذلك حيث أنك عرفت طريقة أخرى public void actionP في السطر 59 .

#5

الله يعطيك العافيه يا اخوي

هذا نفس الكود بس زودت عليه بعض الاشياء

المشكله انا ابغاه اذا ضغت على button يحط فيه X

بس ما قدرت اسويها

//Title:        Your Product Name
//Version:      
//Copyright:    Copyright (c) 1999
//Author:       Your Name
//Company:      Your Company
//Description:  Your description

package aa;

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

public class XO extends JFrame {
  static JButton  position[][];
      private java.awt.MenuBar menuBar1;
        private java.awt.Menu menu1;
    private java.awt.MenuItem menuNewGame;
    private java.awt.MenuItem menuExit;
    private java.awt.MenuItem menuAbout;
  public XO() {
   super("Tic Tac Toe Game");

       menuBar1 = new java.awt.MenuBar();
        menu1 = new java.awt.Menu();
        menuNewGame = new java.awt.MenuItem();
        menuExit = new java.awt.MenuItem();
        menuAbout = new java.awt.MenuItem();
        
        menu1.setLabel("File");
        menuNewGame.setLabel("New Game");
        menuNewGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newGameActionPerformed(evt);
            }
        });

        menu1.add(menuNewGame);
        menuExit.setLabel("Exit");
        menuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               exitActionPerformed(evt);
            }
        });
          menu1.add(menuExit);
        menuAbout.setLabel("About...");
        menuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               aboutActionPerformed(evt);
            }
        });

        menu1.add(menuAbout);
        menuBar1.add(menu1);

         addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
        setMenuBar(menuBar1);

   Container boardPanel=getContentPane();
   boardPanel.setLayout(new FlowLayout());
       //ButtonHandler handler=new ButtonHandler();
   //create button
     position=new JButton[9][9];
 // int   dim = 9 * 55;



    boardPanel.setLayout(new GridLayout(9, 9));


    for (int i = 0; i < 9; ++i)
    {
      for (int j = 0; j <9; ++j)
      {
        position[j] = new JButton("");
        position[ i ][j].setActionCommand( Integer.toString( i ) );

       position[ i ][j].addActionListener( new ActionListener() {
                public void actionPerformed( ActionEvent evt ) {

                    buttonAction( evt.getActionCommand(),evt.getActionCommand());
                    }
            } );


        boardPanel.add(position[j]);
       // position[j].setEnabled(enabled);
      }
    }
       setSize(500,500);
       setVisible(true);



  } //end constactor
  private void aboutActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog( XO.this, "Tic Tac Toe is Created By \n abdyllah alubiey && Faisl Amro. \nI hope you will enjoy it!",
                                        "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE );
                                        }

   private void exitActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit( 0 );
    }

     private void newGameActionPerformed(java.awt.event.ActionEvent evt) {
        setNewGame();
    }

    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit( 0 );
    }
   public static void main(String args[])
   {
   XO ap= new XO();

     ap.addWindowListener (
   new WindowAdapter(){
   public void windowClosing(WindowEvent e)
    {System.exit (0); }
     }
     );
     } //end main

      public static void setNewGame() {
        for ( int i = 0; i < 9; i++ ) {
        for(int j=0;j<9;j++){

            position[ i][j].setText( "" );

        }//end of for
        }

    }//end of setNewGame()
  private void buttonAction( String btn,String btn1 ) {
        int i = Integer.parseInt( btn );
       int j=Integer.parseInt(btn1);
        //if ( position[ i ][ 5].getText() == "" ) {
            position[ i][j].setText( " X " );

            //checkGameStatus();
           // nextMove();
        //}//end of if
        /*else
            JOptionPane.showMessageDialog( XO.this,
                        "This square is already occupied, \nplease try another one.",
                                            "Oops...", JOptionPane.ERROR_MESSAGE );*/
    }//end of buttonAction()

     } //end of XO class
#6

Hello ,

I think here is what you want , I advice you to write the algorithm for your program (or game) on papers , test it and make sure that it'll work before you start to program it , because the less you care about your algorithm the more errors and difficulties you face during the coding .

Good luck.

//Title:        Your Product Name
//Version:      
//Copyright:    Copyright (c) 1999
//Author:       Your Name
//Company:      Your Company
//Description:  Your description

package aa;

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

public class XO extends JFrame {
 static JButton  position[][];
   
   private java.awt.MenuBar menuBar1;
   private java.awt.Menu menu1;
   private java.awt.MenuItem menuNewGame;
   private java.awt.MenuItem menuExit;
   private java.awt.MenuItem menuAbout;

 public XO() {
  super("Tic Tac Toe Game");

       menuBar1 = new java.awt.MenuBar();
       menu1 = new java.awt.Menu();
       menuNewGame = new java.awt.MenuItem();
       menuExit = new java.awt.MenuItem();
       menuAbout = new java.awt.MenuItem();
       
       menu1.setLabel("File");
       menuNewGame.setLabel("New Game");
       menuNewGame.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               newGameActionPerformed(evt);
           }
       });

       menu1.add(menuNewGame);
       menuExit.setLabel("Exit");
       menuExit.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
              exitActionPerformed(evt);
           }
       });
         menu1.add(menuExit);
       menuAbout.setLabel("About...");
       menuAbout.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
              aboutActionPerformed(evt);
           }
       });

       menu1.add(menuAbout);
       menuBar1.add(menu1);

        addWindowListener(new java.awt.event.WindowAdapter() {
           public void windowClosing(java.awt.event.WindowEvent evt) {
               exitForm(evt);
           }
       });
       
       setMenuBar(menuBar1);

  Container boardPanel=getContentPane();
  boardPanel.setLayout(new FlowLayout());
      //ButtonHandler handler=new ButtonHandler();
  //create button
    position=new JButton[9][9];
// int   dim = 9 * 55;



   boardPanel.setLayout(new GridLayout(9, 9));


   for (int i = 0; i < 9; ++i)
   {
     for (int j = 0; j <9; ++j)
     {
       position[j] = new JButton("");
       position[j].setActionCommand( Integer.toString( i ) );

      position[ i ][j].addActionListener( new ActionListener() {
      
      public void actionPerformed( ActionEvent evt ) {
   	 //Here is the modification
    JButton b = (JButton)evt.getSource();
     	 b.setText("X");

             //      buttonAction(evt.getActionCommand(),evt.getActionCommand());
                   }
           } );


       boardPanel.add(position[j]);
      // position[j].setEnabled(enabled);
     }
   }
      setSize(500,500);
      setVisible(true);



 } //end constactor
 private void aboutActionPerformed(java.awt.event.ActionEvent evt) {
       JOptionPane.showMessageDialog( XO.this, "Tic Tac Toe is Created By \n abdyllah alubiey && Faisl Amro. \nI hope you will enjoy it!",
                                       "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE );
                                       }

  private void exitActionPerformed(java.awt.event.ActionEvent evt) {
       System.exit( 0 );
   }

    private void newGameActionPerformed(java.awt.event.ActionEvent evt) {
       setNewGame();
   }

   private void exitForm(java.awt.event.WindowEvent evt) {
       System.exit( 0 );
   }
  public static void main(String args[])
  {
  XO ap= new XO();

    ap.addWindowListener (
  new WindowAdapter(){
  public void windowClosing(WindowEvent e)
   {System.exit (0); }
    }
    );
    } //end main

     public static void setNewGame() {
       for ( int i = 0; i < 9; i++ ) {
       for(int j=0;j<9;j++){

           position[ i][j].setText( "" );

       }//end of for
       }

   }//end of setNewGame()
 private void buttonAction(String btn,String btn1 ) {
       int i = Integer.parseInt( btn );
       int j=Integer.parseInt(btn1);
       //if ( position[ i ][ 5].getText() == "" ) {
           position[ i][j].setText( " X " );

           //checkGameStatus();
          // nextMove();
       //}//end of if
       /*else
           JOptionPane.showMessageDialog( XO.this,
                       "This square is already occupied, \nplease try another one.",
                                           "Oops...", JOptionPane.ERROR_MESSAGE );*/
   }//end of buttonAction()

    } //end of XO class

هذا الموضوع مغلق.

مواضيع مشابهة