Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Help coderanch get a
new server
by contributing to the
fundraiser
michael wrzos
Greenhorn
+ Follow
news
4
Posts
2
Threads
since Apr 03, 2011
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads
Recent posts by michael wrzos
ui manager getColor and link JOptionpane OK button to code
Hi, Got most of this but having a couple of problems. How to assign color value to Strings "X" and "O" after the count and linking the OK button to code reseting the game. Any suggestions? Michael
package mike.org; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TicTacToeSwing extends JFrame implements ActionListener { //declare and instantiate JButtons private JButton jbtnTicTacToe1 = new JButton(""); private JButton jbtnTicTacToe2 = new JButton(""); private JButton jbtnTicTacToe3 = new JButton(""); private JButton jbtnTicTacToe4 = new JButton(""); private JButton jbtnTicTacToe5 = new JButton(""); private JButton jbtnTicTacToe6 = new JButton(""); private JButton jbtnTicTacToe7 = new JButton(""); private JButton jbtnTicTacToe8 = new JButton(""); private JButton jbtnTicTacToe9 = new JButton(""); private JButton jbtnExit = new JButton("Exit"); private JButton jbtnReset = new JButton("Reset"); private String sLetter = ""; private int count = 0; private boolean win = false; private JLabel lblPlayerX, lblPlayerO; private JTextField jtfPlayerX, jtfPlayerO; //declare and instantiate JFrame components JPanel jpnlMain = new JPanel(); JPanel jpnlTop = new JPanel(); JPanel jpnlCenter = new JPanel(); JPanel jpnlBottom = new JPanel(); //class constructor public TicTacToeSwing(String sTitle) { //prepare the JFrame/window super(sTitle); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set layouts and add Game Buttons jpnlMain.setLayout(new BorderLayout()); jpnlMain.add(jpnlCenter, BorderLayout.CENTER); jpnlCenter.setLayout (new GridLayout(3,3,10,10)); jpnlCenter.add(jbtnTicTacToe1); jpnlCenter.add(jbtnTicTacToe2); jpnlCenter.add(jbtnTicTacToe3); jpnlCenter.add(jbtnTicTacToe4); jpnlCenter.add(jbtnTicTacToe5); jpnlCenter.add(jbtnTicTacToe6); jpnlCenter.add(jbtnTicTacToe7); jpnlCenter.add(jbtnTicTacToe8); jpnlCenter.add(jbtnTicTacToe9); //set layout and add JLabels and JTextfields to the top panel jpnlMain.add(jpnlTop, BorderLayout.NORTH); jpnlTop.setLayout (new GridLayout(1,4,10,10)); JLabel lblPlayerX = new JLabel("Player X"); JLabel lblPlayerO = new JLabel("Player O"); JTextField jtfPlayerX = new JTextField(); JTextField jtfPlayerO = new JTextField(); jpnlTop.add(lblPlayerX); jpnlTop.add(jtfPlayerX); jpnlTop.add(lblPlayerO); jpnlTop.add(jtfPlayerO); // set layout Bottom panel jpnlMain.add(jpnlBottom, BorderLayout.SOUTH); jpnlBottom.setLayout (new GridLayout(1,2,10,10)); // code the Reset and Exit JButtons JButton jbtnReset = new JButton("Reset"); jbtnReset.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jbtnTicTacToe1.setText(""); jbtnTicTacToe1.setEnabled(true); jbtnTicTacToe2.setText(""); jbtnTicTacToe2.setEnabled(true); jbtnTicTacToe3.setText(""); jbtnTicTacToe3.setEnabled(true); jbtnTicTacToe4.setText(""); jbtnTicTacToe4.setEnabled(true); jbtnTicTacToe5.setText(""); jbtnTicTacToe5.setEnabled(true); jbtnTicTacToe6.setText(""); jbtnTicTacToe6.setEnabled(true); jbtnTicTacToe7.setText(""); jbtnTicTacToe7.setEnabled(true); jbtnTicTacToe8.setText(""); jbtnTicTacToe8.setEnabled(true); jbtnTicTacToe9.setText(""); jbtnTicTacToe9.setEnabled(true); count = 0; } }); JButton jbtnExit = new JButton("Exit"); jbtnExit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }); jpnlBottom.add(jbtnReset); jpnlBottom.add(jbtnExit); jbtnTicTacToe1.addActionListener(this); jbtnTicTacToe2.addActionListener(this); jbtnTicTacToe3.addActionListener(this); jbtnTicTacToe4.addActionListener(this); jbtnTicTacToe5.addActionListener(this); jbtnTicTacToe6.addActionListener(this); jbtnTicTacToe7.addActionListener(this); jbtnTicTacToe8.addActionListener(this); jbtnTicTacToe9.addActionListener(this); // make the JFrame visible Container ca = getContentPane(); ca.setBackground(Color.BLUE); ca.add(jpnlMain); setContentPane(ca); setVisible(true); }//end constructor TicTacToeSwing //logic for whose turn is it public void actionPerformed(ActionEvent e ) { //******************************************* count++; if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){ sLetter = "X" ; } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){ sLetter = "O"; } //Determine X or O on buttons if(e.getSource() == jbtnTicTacToe1){ jbtnTicTacToe1.setText(sLetter); jbtnTicTacToe1.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe2){ jbtnTicTacToe2.setText(sLetter); jbtnTicTacToe2.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe3){ jbtnTicTacToe3.setText(sLetter); jbtnTicTacToe3.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe4){ jbtnTicTacToe4.setText(sLetter); jbtnTicTacToe4.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe5){ jbtnTicTacToe5.setText(sLetter); jbtnTicTacToe5.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe6){ jbtnTicTacToe6.setText(sLetter); jbtnTicTacToe6.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe7){ jbtnTicTacToe7.setText(sLetter); jbtnTicTacToe7.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe8){ jbtnTicTacToe8.setText(sLetter); jbtnTicTacToe8.setEnabled(false); } else if(e.getSource() == jbtnTicTacToe9){ jbtnTicTacToe9.setText(sLetter); jbtnTicTacToe9.setEnabled(false); } //horizontal wins if( jbtnTicTacToe1.getText() == jbtnTicTacToe2.getText() && jbtnTicTacToe2.getText() == jbtnTicTacToe3.getText() && jbtnTicTacToe1.getText() != ""){ win = true; } else if(jbtnTicTacToe4.getText() == jbtnTicTacToe5.getText() && jbtnTicTacToe5.getText() == jbtnTicTacToe6.getText() && jbtnTicTacToe4.getText() != ""){ win = true; } else if(jbtnTicTacToe7.getText() == jbtnTicTacToe8.getText() && jbtnTicTacToe8.getText() == jbtnTicTacToe9.getText() && jbtnTicTacToe7.getText() != ""){ win = true; } //vertical wins else if(jbtnTicTacToe1.getText() == jbtnTicTacToe4.getText() && jbtnTicTacToe4.getText() == jbtnTicTacToe7.getText() && jbtnTicTacToe1.getText() != ""){ win = true; } else if(jbtnTicTacToe2.getText() == jbtnTicTacToe5.getText() && jbtnTicTacToe5.getText() == jbtnTicTacToe8.getText() && jbtnTicTacToe2.getText() != ""){ win = true; } else if(jbtnTicTacToe3.getText() == jbtnTicTacToe6.getText() && jbtnTicTacToe6.getText() == jbtnTicTacToe9.getText() && jbtnTicTacToe3.getText() != ""){ win = true; } //diagonal wins else if(jbtnTicTacToe1.getText() == jbtnTicTacToe5.getText() && jbtnTicTacToe5.getText() == jbtnTicTacToe9.getText() && jbtnTicTacToe1.getText() != ""){ win = true; } else if(jbtnTicTacToe3.getText() == jbtnTicTacToe5.getText() && jbtnTicTacToe5.getText() == jbtnTicTacToe7.getText() && jbtnTicTacToe3.getText() != ""){ win = true; } else { win = false; } //Show message win or tie if(win == true){ JOptionPane.showMessageDialog(null, sLetter + " WINS!"); } else if(count == 9 && win == false){ JOptionPane.showMessageDialog(null, "Tie Game!"); } //***************************************** } // Main method public static void main(String[] args){ TicTacToeSwing winTicTacToe = new TicTacToeSwing("Tic Tac Toe Swing"); }//end main }//end class [javadoc]TicTacToeSwing[/javadoc]
show more
13 years ago
Swing / AWT / SWT
Null pointer exception in button event handler.
I am still working on taking the output out of the loop. Right now it is outputing each prime several times, hence the slowdown.Thanks Mike
show more
13 years ago
Java in General
Null pointer exception in button event handler.
Thanks for the help. Should have used Int.ParseInteger. Mike
show more
13 years ago
Java in General
Null pointer exception in button event handler.
Hi, First time on this forum. Wonder if anyone could help me with a null pointer exception. Thanks, Michael
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PrimeNumbersSwing extends JFrame //implements ActionListener { private static final int WIDTH = 400; private static final int HEIGHT = 300; //private final static String newline = "\n"; // Declare JFrame components: private JLabel MaxNumberL; private JTextField MaxNumberTF; private JTextArea PrimeNumbersTA; private JButton PrimeNumbersB , exitB , clearB; private PrimeNumbersButtonHandler pbHandler; private ClearButtonHandler cbHandler; private ExitButtonHandler ebHandler; private JScrollPane scrollingResult; // Constructor public PrimeNumbersSwing() { // Set the title and Size: setTitle("Maximum Number to Test"); setSize(WIDTH, HEIGHT); // Instantiate the JLabel components: MaxNumberL = new JLabel("Enter the Maximum Number: ", SwingConstants.LEFT); // Instantiate the JTextFields:H MaxNumberTF = new JTextField(10); // Make the JTextArea scrollable: PrimeNumbersTA = new JTextArea(10,1); scrollingResult = new JScrollPane(PrimeNumbersTA); // Instantiate and register the Calculate button for click events: PrimeNumbersB = new JButton("Prime Numbers"); pbHandler = new PrimeNumbersButtonHandler(); PrimeNumbersB.addActionListener(pbHandler); // Instantiate and register the Clear button for click events: clearB = new JButton("Clear"); cbHandler = new ClearButtonHandler(); clearB.addActionListener(cbHandler); // Instantiate and register the Exit button for click events: exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); // Start to add the components to the JFrame: Container pane = getContentPane(); pane.setLayout(new GridLayout(4, 2)); pane.add(MaxNumberL); pane.add(MaxNumberTF); pane.add(scrollingResult); pane.add(PrimeNumbersB); pane.add(clearB); pane.add(exitB); // Show the JFrame and set code to respond to the user clicking on the X: setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }//end constructor // Calculate Button Event Handler private class PrimeNumbersButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int MaxNumber = Integer.getInteger(MaxNumberTF.getText()); String strOutput = ""; //initialize boolean isPrime; for(int i = 2; i < MaxNumber; i++) //initialize, set limit, add to count { isPrime = true; //check to see if the number is prime for(int j = 2; j < i ; j++) { if(i % j == 0) { isPrime = false; break; } // end if if(isPrime) { strOutput = strOutput + i + "\n"; PrimeNumbersTA.append(String.valueOf(strOutput) + "\n"); } } // inner for //Clear Button Event Handler } // outer for //PrimeNumbersTA.append(String.valueOf(strOutput) + "\n"); } // end action performed } // end class private class ClearButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { MaxNumberTF.setText(""); PrimeNumbersTA.setText(""); } // Exit Button Event Handler } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String [] args ) { // The only thing this main does is Instantiate the JFrame: // Note the name of the object is the name of the class from above. PrimeNumbersSwing PrimeNumbersProg = new PrimeNumbersSwing(); } }
show more
13 years ago
Java in General