Results 1 to 12 of 12
Thread: JApplet/Application Menu Help
- 04-06-2011, 03:42 PM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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.
- 04-06-2011, 07:41 PM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
can anyone figure this out? i cant
-
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.
- 04-06-2011, 10:23 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
There i re-edited my post take a look now
- 04-06-2011, 10:36 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Does this even compile? What happens if you delete the opening brace after e.getActionCommand();?Java Code:public void actionPerformed(ActionEvent e){ String arg = e.getActionCommand();{ if (arg.equals("Exit")){ System.exit(0); } }
- 04-06-2011, 10:44 PM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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.
- 04-06-2011, 10:56 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 04-06-2011, 10:59 PM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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
- 04-06-2011, 11:14 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
You need to cut this from its current location and paste it into the constructor.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);
- 04-07-2011, 02:44 AM #10
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
where is that? sorry im very new
-
You can surely search your code and find this block, right?
- 04-07-2011, 03:34 AM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
Similar Threads
-
JFrame to JApplet or JApplet to JApplet
By ramesh.8189 in forum AWT / SwingReplies: 13Last Post: 02-08-2009, 06:14 AM -
How to enable save as option in menu after the application has been launched
By plenitude in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 12-26-2008, 09:31 AM -
Fill a menu dynamically when menu is shown
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:47 PM -
React to menu action and checkbox menu
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:50 PM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 07:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks