Results 1 to 12 of 12
  1. #1
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default JApplet/Application Menu Help

    I had my program running well and i just added an "exit" button to my menu and it comes up fine but you can't see the rest of the program at all. Can someone tell me what i've done wrong. I believe the problem has to do with the ActionListener part of the applet.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import java.awt.Color;
    
    public class TicTacToe1 extends JApplet implements ActionListener{
    
    private char whoseTurn = 'X';
    private Cell[][] cells = new Cell[3][3];
    private JLabel jlblStatus = new JLabel("X's turn to play");
    private JFrame window = new JFrame("Tic-Tac-Toe");
    
    
    /** Initialize UI */
    
    
    public TicTacToe1() {
    
    JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
           menuBar.add(fileMenu);
    
    JMenuItem exitAction = new JMenuItem("Exit");
      fileMenu.add(exitAction);
        exitAction.setActionCommand("Exit");
          exitAction.addActionListener(this);
    
    }
    
      public void actionPerformed(ActionEvent e){
               String arg = e.getActionCommand();{
    		  if (arg.equals("Exit")){
    			  System.exit(0);
           }
    {
    
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
        for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            p.add(cells[i][j] = new Cell());
    
    
    p.setBorder(new LineBorder(Color.red, 1));
      jlblStatus.setBorder(new LineBorder(Color.black, 1));
         add(p, BorderLayout.CENTER);
         add(jlblStatus, BorderLayout.SOUTH);
    
    }
       }
          }
    
    
    public boolean isFull() {
         for (int i = 0; i < 3; i++)
         for (int j = 0; j < 3; j++)
            if (cells[i][j].getToken() == ' ')
      return false;
      return true;
    }
    
    
    public boolean isWon(char token) {
        for (int i = 0; i < 3; i++)
            if ((cells[i][0].getToken() == token)
                 && (cells[i][1].getToken() == token)
                 && (cells[i][2].getToken() == token)) {
    return true;
    }
    
          for (int j = 0; j < 3; j++)
              if ((cells[0][j].getToken() == token)
               && (cells[1][j].getToken() == token)
               && (cells[2][j].getToken() == token)) {
    return true;
    }
    
             if ((cells[0][0].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][2].getToken() == token)) {
    return true;
    }
    
            if ((cells[0][2].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][0].getToken() == token)) {
    return true;
    }
    
    return false;
    }
    
    
    public class Cell extends JPanel {
                 private char token = ' ';
    
    public Cell() {
               setBorder(new LineBorder(Color.black, 1)); // Set cell's border
               addMouseListener(new MouseListener()); // Register listener
    }
    
    
    public char getToken() {
               return token;
    }
    
    public void setToken(char c) {
                token = c;
                repaint();
    }
    
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    
    if (token == 'X') {
                   g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
                   g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
    }
    else if (token == 'O') {
                   g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
         }
    }
    
    private class MouseListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
    
    if (token == ' ' && whoseTurn != ' ') {
               setToken(whoseTurn); // Set token in the cell
    
    
    if (isWon(whoseTurn)) {
               jlblStatus.setText(whoseTurn + " won! The game is over");
               whoseTurn = ' '; // Game is over
    }
    else if (isFull()) {
               jlblStatus.setText("Draw! The game is over");
               whoseTurn = ' '; // Game is over
    }
    else {
              whoseTurn = (whoseTurn == 'X') ? 'O': 'X'; // Change the turn
              jlblStatus.setText(whoseTurn + "'s turn"); // Display whose turn
    
                   }
               }
           }
       }
    }
    
    /** This main method enables the applet to run as an application */
    public static void main(String[] args) {
             JFrame frame = new JFrame("TicTacToe");
                TicTacToe1 applet = new TicTacToe1();
                     frame.add(applet, BorderLayout.CENTER);
    
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
        }
    }
    Last edited by Ryan10; 04-06-2011 at 10:59 PM.

  2. #2
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    can anyone figure this out? i cant

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again as has been said to you many times before if you want help quicker you really must fix your code formatting. It really is very bad, and your code is very hard to read.

  4. #4
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    There i re-edited my post take a look now

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    public void actionPerformed(ActionEvent e){
    
    	  String arg = e.getActionCommand();{
    		  if (arg.equals("Exit")){
    			  System.exit(0);
    
           }
    }
    Does this even compile? What happens if you delete the opening brace after e.getActionCommand();?

  6. #6
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    i did have it working but i messed around with it..with the code i gave you i got a few errors


    F:\TicTacToe\TicTacToe1.java:38: incompatible types
    found : javax.swing.JMenuItem
    required: java.awt.MenuItem
    MenuItem exitAction = new JMenuItem("Exit");
    ^
    F:\TicTacToe\TicTacToe1.java:39: cannot find symbol
    symbol : method add(java.awt.MenuItem)
    location: class javax.swing.JMenu
    fileMenu.add(exitAction);
    ^
    Last edited by Ryan10; 04-06-2011 at 10:51 PM.

  7. #7
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Read the compiler messages; they don't lie to you. The first one, and maybe the second as well, can be fixed by changing MenuItem to JMenuItem.

  8. #8
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    ok now there is no more errors..the application comes up with the menu and the exit feature works. Now where is all my other stuff? its just a blank page with the menu bar

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
        for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            p.add(cells[i][j] = new Cell());
    
    
    p.setBorder(new LineBorder(Color.red, 1));
      jlblStatus.setBorder(new LineBorder(Color.black, 1));
         add(p, BorderLayout.CENTER);
         add(jlblStatus, BorderLayout.SOUTH);
    You need to cut this from its current location and paste it into the constructor.

  10. #10
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    where is that? sorry im very new

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  12. #12
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    yes i did thanks everything works now i gave all of you higher rep.

Similar Threads

  1. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM
  2. How to enable save as option in menu after the application has been launched
    By plenitude in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 12-26-2008, 09:31 AM
  3. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •