Get the winner from Tic Tac Toe - java

so i have to hand in tomorrow a game of Tic Tac Toe. I managed to code pretty much all of it but i cant get it to get the Winner.
So far this is what i have:
package TicTacToe;
public class TicTacToeMain {
public static void main(String[] args) {
// TODO code application logic here
TicTacToe t = new TicTacToe();
t.setVisible(true);
}
}
package TicTacToe;
import javax.swing.JOptionPane;
public class TicTacToe extends javax.swing.JFrame {
public TicTacToe() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jRadioButton2 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton2.setText("jRadioButton2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setEnabled(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setEnabled(false);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setEnabled(false);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setEnabled(false);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setEnabled(false);
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setEnabled(false);
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setEnabled(false);
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setEnabled(false);
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setEnabled(false);
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("New Game");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setText("Reset");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setText("Quit");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jRadioButton1.setText("Player 1 ( X )");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton3.setText("Player 1 ( O )");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton1)
.addGap(62, 62, 62)
.addComponent(jRadioButton3)
.addGap(93, 93, 93))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton12)))
);
pack();
}// </editor-fold>
int count = 2;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton1.setText("X");
jButton1.setEnabled(false);
} else {
count++;
jButton1.setText("O");
jButton1.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton1.setText("O");
jButton1.setEnabled(false);
} else {
count++;
jButton1.setText("X");
jButton1.setEnabled(false);
}
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton2.setText("X");
jButton2.setEnabled(false);
} else {
count++;
jButton2.setText("O");
jButton2.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton2.setText("O");
jButton2.setEnabled(false);
} else {
count++;
jButton2.setText("X");
jButton2.setEnabled(false);
}
}
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton3.setText("X");
jButton3.setEnabled(false);
} else {
count++;
jButton3.setText("O");
jButton3.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton3.setText("O");
jButton3.setEnabled(false);
} else {
count++;
jButton3.setText("X");
jButton3.setEnabled(false);
}
}
}
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton5.setText("X");
jButton5.setEnabled(false);
} else {
count++;
jButton5.setText("O");
jButton5.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton5.setText("O");
jButton5.setEnabled(false);
} else {
count++;
jButton5.setText("X");
jButton5.setEnabled(false);
}
}
}
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton6.setText("X");
jButton6.setEnabled(false);
} else {
count++;
jButton6.setText("O");
jButton6.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton6.setText("O");
jButton6.setEnabled(false);
} else {
count++;
jButton6.setText("X");
jButton6.setEnabled(false);
}
}
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton4.setText("X");
jButton4.setEnabled(false);
} else {
count++;
jButton4.setText("O");
jButton4.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton4.setText("O");
jButton4.setEnabled(false);
} else {
count++;
jButton4.setText("X");
jButton4.setEnabled(false);
}
}
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton8.setText("X");
jButton8.setEnabled(false);
} else {
count++;
jButton8.setText("O");
jButton8.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton8.setText("O");
jButton8.setEnabled(false);
} else {
count++;
jButton8.setText("X");
jButton8.setEnabled(false);
}
}
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton9.setText("X");
jButton9.setEnabled(false);
} else {
count++;
jButton9.setText("O");
jButton9.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton9.setText("O");
jButton9.setEnabled(false);
} else {
count++;
jButton9.setText("X");
jButton9.setEnabled(false);
}
}
}
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton7.setText("X");
jButton7.setEnabled(false);
} else {
count++;
jButton7.setText("O");
jButton7.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton7.setText("O");
jButton7.setEnabled(false);
} else {
count++;
jButton7.setText("X");
jButton7.setEnabled(false);
}
}
}
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.setText("");
jButton1.setEnabled(false);
jButton2.setText("");
jButton2.setEnabled(false);
jButton3.setText("");
jButton3.setEnabled(false);
jButton4.setText("");
jButton4.setEnabled(false);
jButton5.setText("");
jButton5.setEnabled(false);
jButton6.setText("");
jButton6.setEnabled(false);
jButton7.setText("");
jButton7.setEnabled(false);
jButton8.setText("");
jButton8.setEnabled(false);
jButton9.setText("");
jButton9.setEnabled(false);
jRadioButton1.setSelected(false);
jRadioButton3.setSelected(false);
JOptionPane.showMessageDialog(null, "Seleccione ( X ) o ( O )");
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
jButton1.setEnabled(true);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
jButton8.setEnabled(true);
jButton9.setEnabled(true);
}
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton3.isSelected()) {
jButton1.setEnabled(true);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
jButton8.setEnabled(true);
jButton9.setEnabled(true);
}
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.setText("");
jButton1.setEnabled(false);
jButton2.setText("");
jButton2.setEnabled(false);
jButton3.setText("");
jButton3.setEnabled(false);
jButton4.setText("");
jButton4.setEnabled(false);
jButton5.setText("");
jButton5.setEnabled(false);
jButton6.setText("");
jButton6.setEnabled(false);
jButton7.setText("");
jButton7.setEnabled(false);
jButton8.setText("");
jButton8.setEnabled(false);
jButton9.setText("");
jButton9.setEnabled(false);
jRadioButton1.setSelected(false);
jRadioButton3.setSelected(false);
count=0;
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TicTacToe().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
// End of variables declaration
//if(jButton1.equals(jButton2) && jButton2.equals(jButton3)&& (jButton2) != "")) {
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}
}
The game works just fine, but i am not sure how to get the winner.
I am not sure where exactly in the code i need to place it.
First i tried
if((jButton1.getText())==(jButton2.getText())&&((jButton2.getText())==(jButton3.getText()){
ganador = true;
}
but it says it's missing the return statement. Then i changed the code in order to use .equals but it says its missing a return statement again .
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}
Also, i place this part of the code at the end but not sure if that's the problem.

The getText() function in AbstractButton returns a String, in java you need to compare Strings using the .equals(otherString) function
Edit: in regards to your missing return statement. That also means your code there isn't complete as all those methods are void. But if you say a function is: public boolean doSomething() then you need a return statement that actually returns a boolean such as: return ganador.

To further what Simon has said, you want to be implementing this along the lines of:
jButton1.getText().equals( jButton2.getText() )

Related

How do I count the number of wins for each, X and O for my program in java?

So I've been trying to figure out a way to count each time someone wins in a tic tac toe GUI game I am making. I already made methods to check the tic tac toe board for any winning combinations for either X or O. I am just stumped on how I could count each time the checkWinsX and checkWinsO are called. I tried to make another method that would add a point to either X or O, but I kinda failed at that lol.
public class ticTacToeFrame extends javax.swing.JFrame {
int turn = 0; //Deterines the turn
int winsX; //Tracks total wins for X
int winsO; //Tracks total wins for O
public ticTacToeFrame() {
initComponents();
}
public void clearAll() {
ulBUT.setText("");
umBUT.setText("");
urBUT.setText("");
mlBUT.setText("");
centerBUT.setText("");
mrBUT.setText("");
llBUT.setText("");
lmBUT.setText("");
lrBUT.setText("");
statusTF.setText("");
}
////////////////////////////////////////////////////////////////////////
// I tried to make something of this sort multiple times here to count the checkWins, and so far this is the one that would seem to start off the best in my head
////////////////////////////////////////////////////////////////////////
public static int wins() {
int winsX = 0;
int winsO = 0;
}
public void checkWinX(int wins) {
//Top row horizontal check for winning
if (ulBUT.getText().equals("X") && umBUT.getText().equals("X") && urBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Top Row!");
}
//Middle row horizontal check for winning
else if (mlBUT.getText().equals("X") && centerBUT.getText().equals("X") && mrBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Middle Row!");
}
//Bottom row horizontal check for winning
else if (llBUT.getText().equals("X") && lmBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Bottom Row!");
}
//Diagonal check for winning
else if (ulBUT.getText().equals("X") && centerBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
statusTF.setText("X Wins Diagonnaly!");
}
else if (urBUT.getText().equals("X") && centerBUT.getText().equals("X") && llBUT.getText().equals("X")) {
statusTF.setText("X Wins Diagonnaly!");
}
}
public void checkWinO() {
//Top row horizontal check for winning
if (ulBUT.getText().equals("O") && umBUT.getText().equals("O") && urBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Top Row!");
}
//Middle row horizontal check for winning
else if (mlBUT.getText().equals("O") && centerBUT.getText().equals("O") && mrBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Middle Row!");
}
//Bottom row horizontal check for winning
else if (llBUT.getText().equals("O") && lmBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Bottom Row!");
}
//Diagonal check for winning
else if (ulBUT.getText().equals("O") && centerBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
statusTF.setText("O Wins Diagonnaly!");
}
else if (urBUT.getText().equals("O") && centerBUT.getText().equals("O") && llBUT.getText().equals("O")) {
statusTF.setText("O Wins Diagonnaly!");
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ulPAN = new javax.swing.JPanel();
ulBUT = new javax.swing.JButton();
umBUT = new javax.swing.JButton();
urBUT = new javax.swing.JButton();
mlBUT = new javax.swing.JButton();
centerBUT = new javax.swing.JButton();
mrBUT = new javax.swing.JButton();
llBUT = new javax.swing.JButton();
lmBUT = new javax.swing.JButton();
lrBUT = new javax.swing.JButton();
statusTF = new javax.swing.JTextField();
oWinsTF = new javax.swing.JTextField();
xWinsTF = new javax.swing.JTextField();
resetGameBUT = new javax.swing.JButton();
turnTF = new javax.swing.JTextField();
tiesTF1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Tic Tac Toe | John Gironda");
setBackground(new java.awt.Color(87, 87, 87));
ulPAN.setLayout(new java.awt.GridLayout(3, 3));
ulBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ulBUTActionPerformed(evt);
}
});
ulPAN.add(ulBUT);
umBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
umBUTActionPerformed(evt);
}
});
ulPAN.add(umBUT);
urBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
urBUTActionPerformed(evt);
}
});
ulPAN.add(urBUT);
mlBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mlBUTActionPerformed(evt);
}
});
ulPAN.add(mlBUT);
centerBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
centerBUTActionPerformed(evt);
}
});
ulPAN.add(centerBUT);
mrBUT.setToolTipText("");
mrBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrBUTActionPerformed(evt);
}
});
ulPAN.add(mrBUT);
llBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
llBUTActionPerformed(evt);
}
});
ulPAN.add(llBUT);
lmBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lmBUTActionPerformed(evt);
}
});
ulPAN.add(lmBUT);
lrBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lrBUTActionPerformed(evt);
}
});
ulPAN.add(lrBUT);
statusTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
statusTFActionPerformed(evt);
}
});
oWinsTF.setText("Total O Wins: 0");
oWinsTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oWinsTFActionPerformed(evt);
}
});
xWinsTF.setText("Total X Wins: 0");
xWinsTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
xWinsTFActionPerformed(evt);
}
});
resetGameBUT.setText("RESET");
resetGameBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetGameBUTActionPerformed(evt);
}
});
turnTF.setText("Turn: X");
turnTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
turnTFActionPerformed(evt);
}
});
tiesTF1.setText("Total Ties: 0");
tiesTF1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tiesTF1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(ulPAN, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(statusTF)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tiesTF1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(oWinsTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(16, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(ulPAN, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(statusTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(oWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tiesTF1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ulBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (ulBUT.getText().equals("")) {
if (turn % 2 == 0) {
ulBUT.setText("X");
} else {
ulBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void umBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (umBUT.getText().equals("")) {
if (turn % 2 == 0) {
umBUT.setText("X");
} else {
umBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void urBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (urBUT.getText().equals("")) {
if (turn % 2 == 0) {
urBUT.setText("X");
} else {
urBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void mlBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (mlBUT.getText().equals("")) {
if (turn % 2 == 0) {
mlBUT.setText("X");
} else {
mlBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void centerBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (centerBUT.getText().equals("")) {
if (turn % 2 == 0) {
centerBUT.setText("X");
} else {
centerBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void mrBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (mrBUT.getText().equals("")) {
if (turn % 2 == 0) {
mrBUT.setText("X");
} else {
mrBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void llBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (llBUT.getText().equals("")) {
if (turn % 2 == 0) {
llBUT.setText("X");
} else {
llBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void lmBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (lmBUT.getText().equals("")) {
if (turn % 2 == 0) {
lmBUT.setText("X");
} else {
lmBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void lrBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (lrBUT.getText().equals("")) {
if (turn % 2 == 0) {
lrBUT.setText("X");
} else {
lrBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void resetGameBUTActionPerformed(java.awt.event.ActionEvent evt) {
clearAll();
}
private void statusTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void xWinsTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void oWinsTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void turnTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void tiesTF1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ticTacToeFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton centerBUT;
private javax.swing.JButton llBUT;
private javax.swing.JButton lmBUT;
private javax.swing.JButton lrBUT;
private javax.swing.JButton mlBUT;
private javax.swing.JButton mrBUT;
private javax.swing.JTextField oWinsTF;
private javax.swing.JButton resetGameBUT;
private javax.swing.JTextField statusTF;
private javax.swing.JTextField tiesTF1;
private javax.swing.JTextField turnTF;
private javax.swing.JButton ulBUT;
private javax.swing.JPanel ulPAN;
private javax.swing.JButton umBUT;
private javax.swing.JButton urBUT;
private javax.swing.JTextField xWinsTF;
// End of variables declaration
}
I have to tell you you are going about this all wrong. Start with a GameModel
public class TicTacToeModel {
Boolean[][] board; //use Boolean class rather than boolean primative so you can set board spaces that haven't been taken yet null
public void setSpace (int x, int y, boolean isX) throws SpaceTakenException { ... }
public boolean getSpace(int x, int y) { ... }
public Boolean calculateWinner() { ... } // calculate if there's a winner, and return its Boolean value if so. Otherwise null
}
Once that's done and working, you can adapt a GUI to your model.
In your wins() method, you could pass in a parameter indicating X or O and increment the respective counter variable.
For example:
public int win(int player) {
if(player == 0) {
winsX++;
}
else {
winsY++;
}
}
And then call this every time you check and find that a player has won.

Shuffling the images

I've made a picture puzzle game. actually I use Netbeans 8.2 GUI Builder to create the game, I have made Timer and move.
But I am confused how to shuffle the images. I've searched on various sites but it did not help.
This is my code:
package projetu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import static projetu.fasil.cont;
public class t02 extends javax.swing.JFrame {
static int cont = 0;
int i = 0;
private Timer timer;
private int currentSegundo = 0;
private int currentMinuto = 0;
private int currentHora = 0;
private int velocidade = 1000;
public t02() {
initComponents();
b9.setVisible(false);
}
public void contamuda() {
txtconta.setText(String.valueOf(cont++));
}
private void iniciarContagem() {
ActionListener action;
action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentSegundo++;
if (currentSegundo == 60) {
currentMinuto++;
currentSegundo = 0;
}
if (currentMinuto == 60) {
currentHora++;
currentMinuto = 0;
}
String hr = currentHora <= 9 ? "0" + currentHora : currentHora + "";
String min = currentMinuto <= 9 ? "0" + currentMinuto : currentMinuto + "";
String seg = currentSegundo <= 9 ? "0" + currentSegundo : currentSegundo + "";
tempu.setText(hr + ":" + min + ":" + seg);
}
};
this.timer = new Timer(velocidade, action);
this.timer.start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b4 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txtconta = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tempu = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout(3, 3));
b1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t11.gif"))); // NOI18N
b1.setBorder(null);
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setFocusable(false);
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});
jPanel1.add(b1);
b2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t12.gif"))); // NOI18N
b2.setBorder(null);
b2.setBorderPainted(false);
b2.setContentAreaFilled(false);
b2.setFocusable(false);
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});
jPanel1.add(b2);
b3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t13.gif"))); // NOI18N
b3.setBorder(null);
b3.setBorderPainted(false);
b3.setContentAreaFilled(false);
b3.setFocusable(false);
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});
jPanel1.add(b3);
b4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t14.gif"))); // NOI18N
b4.setBorder(null);
b4.setBorderPainted(false);
b4.setContentAreaFilled(false);
b4.setFocusable(false);
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});
jPanel1.add(b4);
b5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t15.gif"))); // NOI18N
b5.setBorder(null);
b5.setBorderPainted(false);
b5.setContentAreaFilled(false);
b5.setFocusable(false);
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});
jPanel1.add(b5);
b6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t16.gif"))); // NOI18N
b6.setBorder(null);
b6.setBorderPainted(false);
b6.setContentAreaFilled(false);
b6.setFocusable(false);
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});
jPanel1.add(b6);
b7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t17.gif"))); // NOI18N
b7.setBorder(null);
b7.setBorderPainted(false);
b7.setContentAreaFilled(false);
b7.setFocusable(false);
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});
jPanel1.add(b7);
b8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t18.gif"))); // NOI18N
b8.setBorder(null);
b8.setBorderPainted(false);
b8.setContentAreaFilled(false);
b8.setFocusable(false);
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});
jPanel1.add(b8);
b9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t19.gif"))); // NOI18N
b9.setBorder(null);
b9.setBorderPainted(false);
b9.setContentAreaFilled(false);
b9.setFocusable(false);
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});
jPanel1.add(b9);
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel1.setText("Muda :");
txtconta.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
txtconta.setText("0");
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel2.setText("Tempu =");
tempu.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
tempu.setText("00:00:00");
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais01/t02_lbl.jpg"))); // NOI18N
jLabel3.setAutoscrolls(true);
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(0, 89, Short.MAX_VALUE))
);
jButton1.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton1.setText("Komesa");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton3.setText("Valor");
jButton4.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton4.setText("Sai ");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton2.setText("Hili Imagen");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addContainerGap())
);
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel4.setText("Game puzzle tais no uma adat iha Timor Leste");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtconta, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tempu))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
.addComponent(txtconta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tempu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b1.getText());
b2.setIcon(b1.getIcon());
b1.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b1.getText());
b4.setIcon(b1.getIcon());
b1.setVisible(false);
b4.setVisible(true);
}
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b2.getText());
b1.setIcon(b2.getIcon());
b2.setVisible(false);
b1.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b2.getText());
b3.setIcon(b2.getIcon());
b2.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b2.getText());
b5.setIcon(b2.getIcon());
b2.setVisible(false);
b5.setVisible(true);
}
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b3.getText());
b2.setIcon(b3.getIcon());
b3.setVisible(false);
b2.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b3.getText());
b6.setIcon(b3.getIcon());
b3.setVisible(false);
b6.setVisible(true);
}
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b4.getText());
b1.setIcon(b4.getIcon());
b4.setVisible(false);
b1.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b4.getText());
b5.setIcon(b4.getIcon());
b4.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b4.getText());
b7.setIcon(b4.getIcon());
b4.setVisible(false);
b7.setVisible(true);
}
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b5.getText());
b2.setIcon(b5.getIcon());
b5.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b4.getText());
b4.setIcon(b4.getIcon());
b5.setVisible(false);
b4.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b5.getText());
b6.setIcon(b5.getIcon());
b5.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b5.getText());
b8.setIcon(b5.getIcon());
b5.setVisible(false);
b8.setVisible(true);
}
}
private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b9.isVisible() == false) {
b9.setText(b6.getText());
b9.setIcon(b6.getIcon());
b6.setVisible(false);
b9.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b6.getText());
b3.setIcon(b6.getIcon());
b6.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b6.getText());
b5.setIcon(b6.getIcon());
b6.setVisible(false);
b5.setVisible(true);
}
}
private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b4.isVisible() == false) {
b4.setText(b7.getText());
b4.setIcon(b7.getIcon());
b7.setVisible(false);
b4.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b7.getText());
b8.setIcon(b7.getIcon());
b7.setVisible(false);
b8.setVisible(true);
}
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b5.isVisible() == false) {
b5.setText(b8.getText());
b5.setIcon(b8.getIcon());
b8.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b7.getText());
b7.setIcon(b7.getIcon());
b8.setVisible(false);
b7.setVisible(true);
}
if (b9.isVisible() == false) {
b9.setText(b8.getText());
b9.setIcon(b8.getIcon());
b8.setVisible(false);
b9.setVisible(true);
}
}
private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b6.isVisible() == false) {
b6.setText(b9.getText());
b6.setIcon(b9.getIcon());
b9.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b9.getText());
b8.setIcon(b9.getIcon());
b9.setVisible(false);
b8.setVisible(true);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
iniciarContagem();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new hili_imagen().setVisible(true);
dispose();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new game().setVisible(true);
dispose();
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new t02().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel tempu;
private javax.swing.JLabel txtconta;
// End of variables declaration
}

Netbeans calculator error

I made a calculator in netbeans so far i added the plus and minus button to work. but lets say i do 5+5 it gives me 10 and if i add 5 more it give 15. so that parts correct. but if i do 9-5 it gives me -4, and if i subtrac 2 from the -4 i get positive 6. please help, im fairly new to java.
public class Calculator extends javax.swing.JFrame {
/**
* Creates new form Calculator
*/
public Calculator() {
initComponents();
}
double fNums = 0.0;
double sNums = 0.0;
boolean add = false;
boolean sub = false;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
display = new javax.swing.JTextField();
seven = new javax.swing.JButton();
eight = new javax.swing.JButton();
nine = new javax.swing.JButton();
plus = new javax.swing.JButton();
four = new javax.swing.JButton();
five = new javax.swing.JButton();
six = new javax.swing.JButton();
minus = new javax.swing.JButton();
one = new javax.swing.JButton();
two = new javax.swing.JButton();
three = new javax.swing.JButton();
multiply = new javax.swing.JButton();
zero = new javax.swing.JButton();
decimal = new javax.swing.JButton();
divide = new javax.swing.JButton();
enter = new javax.swing.JButton();
clear = new javax.swing.JButton();
jMenuBar2 = new javax.swing.JMenuBar();
jMenu4 = new javax.swing.JMenu();
Home = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
display.setEditable(false);
seven.setText("7");
seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sevenActionPerformed(evt);
}
});
eight.setText("8");
eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eightActionPerformed(evt);
}
});
nine.setText("9");
nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nineActionPerformed(evt);
}
});
plus.setText("+");
plus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
plusActionPerformed(evt);
}
});
four.setText("4");
four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fourActionPerformed(evt);
}
});
five.setText("5");
five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fiveActionPerformed(evt);
}
});
six.setText("6");
six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sixActionPerformed(evt);
}
});
minus.setText("-");
minus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
minusActionPerformed(evt);
}
});
one.setText("1");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});
two.setText("2");
two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
twoActionPerformed(evt);
}
});
three.setText("3");
three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
threeActionPerformed(evt);
}
});
multiply.setText("*");
multiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplyActionPerformed(evt);
}
});
zero.setText("0");
zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeroActionPerformed(evt);
}
});
decimal.setText(".");
decimal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
decimalActionPerformed(evt);
}
});
divide.setText("/");
divide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divideActionPerformed(evt);
}
});
enter.setText("Enter");
enter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enterActionPerformed(evt);
}
});
clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(display)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(54, Short.MAX_VALUE))
);
jMenu4.setText("File");
Home.setText("Home");
Home.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HomeActionPerformed(evt);
}
});
jMenu4.add(Home);
jMenuBar2.add(jMenu4);
jMenu5.setText("Edit");
jMenuBar2.add(jMenu5);
setJMenuBar(jMenuBar2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void HomeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Menu m = new Menu();
m.setVisible(true);
}
private void sixActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + six.getText());
}
private void divideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + divide.getText());
}
private void sevenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + seven.getText());
}
private void eightActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + eight.getText());
}
private void nineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + nine.getText());
}
private void plusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums + Double.parseDouble(display.getText());
display.setText("");
add = true;
}
private void fourActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + four.getText());
}
private void fiveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + five.getText());
}
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + one.getText());
}
private void twoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + two.getText());
}
private void threeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + three.getText());
}
private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + multiply.getText());
}
private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + zero.getText());
}
private void decimalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + decimal.getText());
}
private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText("");
fNums = 0;
sNums = 0;
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Home;
private javax.swing.JButton clear;
private javax.swing.JButton decimal;
private javax.swing.JTextField display;
private javax.swing.JButton divide;
private javax.swing.JButton eight;
private javax.swing.JButton enter;
private javax.swing.JButton five;
private javax.swing.JButton four;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton minus;
private javax.swing.JButton multiply;
private javax.swing.JButton nine;
private javax.swing.JButton one;
private javax.swing.JButton plus;
private javax.swing.JButton seven;
private javax.swing.JButton six;
private javax.swing.JButton three;
private javax.swing.JButton two;
private javax.swing.JButton zero;
// End of variables declaration
}
First: You are missing the == in enterActionPerformed. Use two = signs.
Second: If I understood correctly, everytime there's a '-', the number gets negated. Which is happening after the first number is written.
9-5 gives you 4 because you are negating the first number and adding it to the second:
(-9)+5 =-4
The error is in the following code, when you press the '-' button, you are doing 0-valueEntered.
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
`
`if(sub == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
You can check out my MathExpressionEvaluator repository from Github, as it can easily evaluate simple math expressions.
You can pass it a string expression from your calculator and display the answer that you get from it easily.
Change the Minus listener to
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = Double.parseDouble(display.getText()); <----------
display.setText("");
sub = true;
}
and enter action performed as
private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub == true){
sNums = fNums - Double.parseDouble(display.getText()); <----------
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
And Check

How to transform my toggle button value to int

Here is my simple calculator code.Im trying to add this code something special.I want this program to: it will only use 2 even numbers or 2 odd numbers for the four arithmetical operations. For instance,If user is trying to click an even number and a odd number at the same time,my code will warn like "You can click 2 even numbers or 2 odd numbers".How can I use the values that clicked by toggle buttons as int and use if(value%2==0) statement?
package tr.com.bites;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
public class CalculatorView_1 extends javax.swing.JFrame {
JToggleButton[] buttons = new JToggleButton[4];
public CalculatorView_1() {
initComponents();
buttons[0]=jToggleButton12;
buttons[1]=jToggleButton11;
buttons[2]=jToggleButton10;
buttons[3]=jToggleButton13;
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jToggleButton1 = new javax.swing.JToggleButton();
jToggleButton2 = new javax.swing.JToggleButton();
jToggleButton3 = new javax.swing.JToggleButton();
jToggleButton4 = new javax.swing.JToggleButton();
jToggleButton5 = new javax.swing.JToggleButton();
jToggleButton6 = new javax.swing.JToggleButton();
jToggleButton7 = new javax.swing.JToggleButton();
jToggleButton8 = new javax.swing.JToggleButton();
jToggleButton9 = new javax.swing.JToggleButton();
jToggleButton10 = new javax.swing.JToggleButton();
jToggleButton11 = new javax.swing.JToggleButton();
jToggleButton12 = new javax.swing.JToggleButton();
jToggleButton13 = new javax.swing.JToggleButton();
jToggleButton14 = new javax.swing.JToggleButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToggleButton1.setText("1");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jToggleButton2.setText("2");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton2ActionPerformed(evt);
}
});
jToggleButton3.setText("3");
jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton3ActionPerformed(evt);
}
});
jToggleButton4.setText("5");
jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton4ActionPerformed(evt);
}
});
jToggleButton5.setText("4");
jToggleButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton5ActionPerformed(evt);
}
});
jToggleButton6.setText("6");
jToggleButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton6ActionPerformed(evt);
}
});
jToggleButton7.setText("8");
jToggleButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton7ActionPerformed(evt);
}
});
jToggleButton8.setText("7");
jToggleButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton8ActionPerformed(evt);
}
});
jToggleButton9.setText("9");
jToggleButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton9ActionPerformed(evt);
}
});
jToggleButton10.setText("*");
jToggleButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton10ActionPerformed(evt);
}
});
jToggleButton11.setText("-");
jToggleButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton11ActionPerformed(evt);
}
});
jToggleButton12.setText("+");
jToggleButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton12ActionPerformed(evt);
}
});
jToggleButton13.setText("/");
jToggleButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton13ActionPerformed(evt);
}
});
jToggleButton14.setText("=");
jToggleButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton14ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton9))
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton6))
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton3)))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jToggleButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton12)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton11)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton10))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton1)
.addComponent(jToggleButton2)
.addComponent(jToggleButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton5)
.addComponent(jToggleButton4)
.addComponent(jToggleButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton8)
.addComponent(jToggleButton7)
.addComponent(jToggleButton9))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton13)
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton14)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(90, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("1");
valueCheck("1",jToggleButton1);
}
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("2");
valueCheck("2",jToggleButton2);
}
private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("3");
valueCheck("3",jToggleButton3);
}
private void jToggleButton5ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("4");
valueCheck("4",jToggleButton5);
}
private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("5");
valueCheck("5",jToggleButton4);
}
private void jToggleButton6ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("6");
valueCheck("6",jToggleButton6);
}
private void jToggleButton8ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("7");
valueCheck("7",jToggleButton8);
}
private void jToggleButton7ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("8");
valueCheck("8",jToggleButton7);
}
private void jToggleButton9ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("9");
valueCheck("9",jToggleButton9);
}
private void jToggleButton12ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("+");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("+"))
jToggleButton12.setSelected(true);
}
private void jToggleButton11ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("-");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("-"))
jToggleButton11.setSelected(true);
}
private void jToggleButton10ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("*");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("*"))
jToggleButton10.setSelected(true);
}
private void jToggleButton13ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("/");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("/"))
jToggleButton13.setSelected(true);
}
private void jToggleButton14ActionPerformed(java.awt.event.ActionEvent evt) {
long total=-1;
int firstNumber=-1;
int secondNumber=-1;
String process=null;
for (String secilenler : values) {
if(secilenler.equals("+"))
process="+";
else if(secilenler.equals("-"))
process="-";
else if(secilenler.equals("*"))
process="*";
else if(secilenler.equals("/"))
process="/";
else
{
if(firstNumber==-1)
firstNumber= Integer.parseInt(secilenler);
else
secondNumber= Integer.parseInt(secilenler);
}
}
if(process.equals("+"))
total=firstNumber+secondNumber;
else if(process.equals("-"))
total=firstNumber-secondNumber;
else if(process.equals("*"))
total=firstNumber*secondNumber;
else
total=firstNumber/secondNumber;
jTextField1.setText(""+total);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new CalculatorView_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton10;
private javax.swing.JToggleButton jToggleButton11;
private javax.swing.JToggleButton jToggleButton12;
private javax.swing.JToggleButton jToggleButton13;
private javax.swing.JToggleButton jToggleButton14;
private javax.swing.JToggleButton jToggleButton2;
private javax.swing.JToggleButton jToggleButton3;
private javax.swing.JToggleButton jToggleButton4;
private javax.swing.JToggleButton jToggleButton5;
private javax.swing.JToggleButton jToggleButton6;
private javax.swing.JToggleButton jToggleButton7;
private javax.swing.JToggleButton jToggleButton8;
private javax.swing.JToggleButton jToggleButton9;
// End of variables declaration
private List<String> values=new ArrayList<String>();
public void processValue(String strValue)
{
if(values.contains(strValue))
values.remove(strValue);
else
values.add(strValue);
}
private void valueCheck(String value,JToggleButton button) {
if(values.size()>2)
{
processValue(value);
button.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");
}
}
private void evenOdd(int valuee,JToggleButton buton)
{
}
}
JToggleButton has two states , test with JToggleButton.isSelected
after code executed is required to reset state of JToggleButton.setSelected(false), because Mouse / Key Events toggle with JToggleButton
JToggleButton (JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");) isn't proper JComponent for Calculator (keys 0-9, + - =), use JButton instead, there are various and different ways how to do it an properly, start with ActionCommand or put/getClientProperty
for better help sooner post an SSCCE
For a simple calculator, I'd recommend using JButtons as opposed to JToggleButtons. With JButtons, you could simply use:
StringBuilder intbuilder = new StringBuilder();
int firstnumber;
int secondnumber;
String operation;
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
intbuilder.append("5");
}
});
multiplybutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
firstnumber = Integer.parseInt(intbuilder.toString());
operation = "*";
}
});
And once the equals sign is pressed, just have perform the operation on the two numbers depending on the String operation.

How to stop calling a method from another method in java?

I have a calculator code and the problem is:
public void valueCheck(String value,JToggleButton button) {
if(values.size()>2)
{
processValue(value);
button.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");
}
}
There is a method valueCheck and:
public boolean evenOdd(JToggleButton buton)
{
if(values.size()==2)
{
System.out.println((String)values.get(0));
System.out.println((String)values.get(1));
String number1 = new String(values.get(0));
String number2 = new String(values.get(1));
if(evenNumbers.contains(number1))
{
if(evenNumbers.contains(number2))
{
processValue(number1);
processValue(number2);
}
else if(oddNumbers.contains(number2))
{
buton.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
}
}
else if(oddNumbers.contains(number1))
{
if(oddNumbers.contains(number2))
{
processValue(number1);
processValue(number2);
}
else if(evenNumbers.contains(number2))
{
buton.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
}
}
}
return false;
}
there is a method evenOdd.
My problem is: I dont want valueCheck method to run for evenOdd method.When evenOdd method runs valueCheck must be stopped calling.Is there a event to do this?I want to stop calling valueCheck when evenOdd is running
Here is my whole code.
package tr.com.bites;
import com.sun.org.apache.xml.internal.utils.StopParseException;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
public class CalculatorView_1 extends javax.swing.JFrame {
JToggleButton[] buttons = new JToggleButton[4];
public CalculatorView_1() {
initComponents();
buttons[0]=jToggleButton12;
buttons[1]=jToggleButton11;
buttons[2]=jToggleButton10;
buttons[3]=jToggleButton13;
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jToggleButton1 = new javax.swing.JToggleButton();
jToggleButton2 = new javax.swing.JToggleButton();
jToggleButton3 = new javax.swing.JToggleButton();
jToggleButton4 = new javax.swing.JToggleButton();
jToggleButton5 = new javax.swing.JToggleButton();
jToggleButton6 = new javax.swing.JToggleButton();
jToggleButton7 = new javax.swing.JToggleButton();
jToggleButton8 = new javax.swing.JToggleButton();
jToggleButton9 = new javax.swing.JToggleButton();
jToggleButton10 = new javax.swing.JToggleButton();
jToggleButton11 = new javax.swing.JToggleButton();
jToggleButton12 = new javax.swing.JToggleButton();
jToggleButton13 = new javax.swing.JToggleButton();
jToggleButton14 = new javax.swing.JToggleButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToggleButton1.setText("1");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jToggleButton2.setText("2");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton2ActionPerformed(evt);
}
});
jToggleButton3.setText("3");
jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton3ActionPerformed(evt);
}
});
jToggleButton4.setText("5");
jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton4ActionPerformed(evt);
}
});
jToggleButton5.setText("4");
jToggleButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton5ActionPerformed(evt);
}
});
jToggleButton6.setText("6");
jToggleButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton6ActionPerformed(evt);
}
});
jToggleButton7.setText("8");
jToggleButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton7ActionPerformed(evt);
}
});
jToggleButton8.setText("7");
jToggleButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton8ActionPerformed(evt);
}
});
jToggleButton9.setText("9");
jToggleButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton9ActionPerformed(evt);
}
});
jToggleButton10.setText("*");
jToggleButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton10ActionPerformed(evt);
}
});
jToggleButton11.setText("-");
jToggleButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton11ActionPerformed(evt);
}
});
jToggleButton12.setText("+");
jToggleButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton12ActionPerformed(evt);
}
});
jToggleButton13.setText("/");
jToggleButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton13ActionPerformed(evt);
}
});
jToggleButton14.setText("=");
jToggleButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton14ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton9))
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton6))
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton3)))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jToggleButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton12)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton11)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton10))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton1)
.addComponent(jToggleButton2)
.addComponent(jToggleButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton5)
.addComponent(jToggleButton4)
.addComponent(jToggleButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton8)
.addComponent(jToggleButton7)
.addComponent(jToggleButton9))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton13)
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton14)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(90, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("1");
valueCheck("1",jToggleButton1);
evenOdd(jToggleButton1);
}
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("2");
valueCheck("2",jToggleButton2);
evenOdd(jToggleButton2);
}
private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("3");
valueCheck("3",jToggleButton3);
evenOdd(jToggleButton3);
}
private void jToggleButton5ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("4");
valueCheck("4",jToggleButton5);
evenOdd(jToggleButton5);
}
private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("5");
valueCheck("5",jToggleButton4);
evenOdd(jToggleButton4);
}
private void jToggleButton6ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("6");
valueCheck("6",jToggleButton6);
evenOdd(jToggleButton6);
}
private void jToggleButton8ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("7");
valueCheck("7",jToggleButton8);
evenOdd(jToggleButton8);
}
private void jToggleButton7ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("8");
valueCheck("8",jToggleButton7);
evenOdd(jToggleButton7);
}
private void jToggleButton9ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("9");
valueCheck("9",jToggleButton9);
evenOdd(jToggleButton9);
}
private void jToggleButton12ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("+");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("+"))
jToggleButton12.setSelected(true);
}
private void jToggleButton11ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("-");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("-"))
jToggleButton11.setSelected(true);
}
private void jToggleButton10ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("*");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("*"))
jToggleButton10.setSelected(true);
}
private void jToggleButton13ActionPerformed(java.awt.event.ActionEvent evt) {
processValue("/");
for(JToggleButton b: buttons)
b.setSelected(false);
if(values.contains("/"))
jToggleButton13.setSelected(true);
}
private void jToggleButton14ActionPerformed(java.awt.event.ActionEvent evt) {
long total=-1;
int firstNumber=-1;
int secondNumber=-1;
String process=null;
for (String secilenler : values) {
if(secilenler.equals("+"))
process="+";
else if(secilenler.equals("-"))
process="-";
else if(secilenler.equals("*"))
process="*";
else if(secilenler.equals("/"))
process="/";
else
{
if(firstNumber==-1)
firstNumber= Integer.parseInt(secilenler);
else
secondNumber= Integer.parseInt(secilenler);
}
}
if(process.equals("+"))
total=firstNumber+secondNumber;
else if(process.equals("-"))
total=firstNumber-secondNumber;
else if(process.equals("*"))
total=firstNumber*secondNumber;
else
total=firstNumber/secondNumber;
jTextField1.setText(""+total);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new CalculatorView_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton10;
private javax.swing.JToggleButton jToggleButton11;
private javax.swing.JToggleButton jToggleButton12;
private javax.swing.JToggleButton jToggleButton13;
private javax.swing.JToggleButton jToggleButton14;
private javax.swing.JToggleButton jToggleButton2;
private javax.swing.JToggleButton jToggleButton3;
private javax.swing.JToggleButton jToggleButton4;
private javax.swing.JToggleButton jToggleButton5;
private javax.swing.JToggleButton jToggleButton6;
private javax.swing.JToggleButton jToggleButton7;
private javax.swing.JToggleButton jToggleButton8;
private javax.swing.JToggleButton jToggleButton9;
// End of variables declaration
private List<String> values = new ArrayList<String>();
private List<String> evenNumbers = new ArrayList<String>();
{
evenNumbers.add("2");
evenNumbers.add("4");
evenNumbers.add("6");
evenNumbers.add("8");
}
private List<String> oddNumbers = new ArrayList<String>();
{
oddNumbers.add("1");
oddNumbers.add("3");
oddNumbers.add("5");
oddNumbers.add("7");
oddNumbers.add("9");
}
public void processValue(String strValue)
{
if(values.contains(strValue))
values.remove(strValue);
else
values.add(strValue);
}
public void valueCheck(String value,JToggleButton button) {
if(values.size()>2)
{
processValue(value);
button.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");
}
}
public boolean evenOdd(JToggleButton buton)
{
if(values.size()==2)
{
System.out.println((String)values.get(0));
System.out.println((String)values.get(1));
String number1 = new String(values.get(0));
String number2 = new String(values.get(1));
if(evenNumbers.contains(number1))
{
if(evenNumbers.contains(number2))
{
processValue(number1);
processValue(number2);
}
else if(oddNumbers.contains(number2))
{
buton.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
}
}
else if(oddNumbers.contains(number1))
{
if(oddNumbers.contains(number2))
{
processValue(number1);
processValue(number2);
}
else if(evenNumbers.contains(number2))
{
buton.setSelected(false);
JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
}
}
}
return false;
}
}
Your problem is not related to multithreading. Multithreading involves multiple threads, thus the word itself. In your situation, what you want to do can be achieved with simple if-else statement and proper return value from the involved methods.
I see that your method evenOdd() already returns boolean. You can take advantage from that. After displaying your error message in evenOdd(), make it to return false. Then, when the toggle button is pressed, test if evenOdd() returns true then call valueCheck(), else, if it returns false (error message was displayed), do not call it.
if(evenOdd(jToggleButton1))
valueCheck("1", jToggleButton1);
Also, your current method names are confusing. Try playing with the way you name your methods. Methods should be named in such way that when you see it, you already know what it does.

Categories