How to stop calling a method from another method in java? - 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.

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.

How to delete text from a JTextField when you press a JButton (JAVA)

My program has to print messages in the JTextField when the Resume Producers button is pressed, and it needs to delete the messages when the Resume Consumers is pressed.
The thing is that I don't know how to delete the messages when the Resume Consumers is pressed.
Code:
package buffer;
import java.util.ArrayList;
import javax.swing.JTextField;
public class Buffer
{
private String message;
private boolean full=false;
Topass pass = new Topass();
public ArrayList<String> list = new ArrayList<String> ();
JTextField tf;
String content="";
public Buffer(JTextField t1){
tf=t1;
}
public synchronized void put(String message)
{
list.add(message);
}
public synchronized void take(String message)
{
list.remove(message);
}
public synchronized void sendMessage(String msg)
{
while(full && list.size()>=30)
{
try
{
wait();
} catch (InterruptedException ex) { }
}
pass.look();
full=true;
message=msg;
list.add(message);
for(int i=0; i<list.size(); i++){
content=content+list.get(i);
}
tf.setText(content);
notifyAll();
}
public synchronized String receivesMessage()
{
while(!full)
{
try
{
wait();
} catch (InterruptedException ex) { }
}
pass.look();
full=false;
notifyAll();
return message;
}
}
package buffer;
import static java.lang.Thread.sleep;
import javax.swing.JButton;
public class Consumer extends Thread
{
private int numMessages;
private Buffer miBuffer;
private String readers;
public Consumer(String reader, Buffer miBuffer)
{
this.numMessages=numMessages;
this.miBuffer=miBuffer;
this.readers=reader;
}
#Override
public void run()
{
while(true){
try
{
sleep((int)(300+400*Math.random()));
} catch(InterruptedException e){ }
System.out.println(readers + " Has read " + miBuffer.receivesMessage());
}
}
}
package buffer;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
public class Producer extends Thread
{
private String prefix;
private int numMessages;
private Buffer miBuffer;
public Producer(String prefix, int n, Buffer buffer)
{
this.prefix=prefix;
numMessages=n;
miBuffer=buffer;
}
public void run()
{
numMessages = 99;
for(int i=1; i<=numMessages; i++)
{
try
{
sleep((int)(500+400*Math.random()));
} catch(InterruptedException e){ }
miBuffer.sendMessage(prefix+i);
//miBuffer.meter(prefijo+i);
}
}
}
package buffer;
public class Topass
{
private boolean closed=false;
public synchronized void look()
{
while(closed)
{
try
{
wait();
} catch(InterruptedException ie){ }
}
}
public synchronized void open()
{
closed=false;
notifyAll();
}
public synchronized void closed()
{
closed=true;
}
}
package buffer;
import java.awt.Container;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import static jdk.nashorn.internal.objects.ArrayBufferView.buffer;
/**
*
* #author Bogdan
*/
public class DigitalBuffer extends javax.swing.JFrame
{
Buffer buffer;
Producer p;
Consumer c;
Topass paso = new Topass();
/**
* Creates new form BufferGrafico
*/
public DigitalBuffer() {
initComponents();
buffer = new Buffer(t1);
Producer A = new Producer("A",99,buffer);
Producer B = new Producer("B",99,buffer);
Producer C = new Producer("C",99,buffer);
Producer D = new Producer("D",99,buffer);
Consumer Luis = new Consumer("LUIS", buffer);
Consumer Juan = new Consumer("JUAN", buffer);
Consumer Maria = new Consumer("MARIA", buffer);
Consumer Ana = new Consumer ("ANA", buffer);
A.start();
B.start();
C.start();
D.start();
Luis.start();
Juan.start();
Maria.start();
Ana.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() {
t1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
JButton1 = new javax.swing.JButton();
JButton2 = new javax.swing.JButton();
JButton3 = new javax.swing.JButton();
JButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
t1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t1ActionPerformed(evt);
}
});
jLabel2.setText("Buffer content");
JButton1.setText("Stop Producers");
JButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JButton1ActionPerformed(evt);
}
});
JButton2.setText("Resume Producers");
JButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JButton2ActionPerformed(evt);
}
});
JButton3.setText("Stop Consumers");
JButton3.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
JButton3AncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
JButton3AncestorRemoved(evt);
}
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
});
JButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JButton3ActionPerformed(evt);
}
});
JButton4.setText("Resume Consumers");
JButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JButton4ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(91, 91, 91)
.addComponent(JButton1)
.addGap(80, 80, 80)
.addComponent(JButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(102, 102, 102))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(104, 104, 104)
.addComponent(JButton3)
.addGap(77, 77, 77)
.addComponent(JButton4)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addComponent(jLabel1))
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(JButton1)
.addComponent(JButton2))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(JButton3)
.addComponent(JButton4))
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void t1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void JButton1ActionPerformed(java.awt.event.ActionEvent evt) {
buffer.pass.closed();
}
private void JButton2ActionPerformed(java.awt.event.ActionEvent evt) {
buffer.pass.open();
}
private void JButton3ActionPerformed(java.awt.event.ActionEvent evt) {
buffer.pass.closed();
}
private void JButton4ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void JButton3AncestorAdded(javax.swing.event.AncestorEvent evt) {
// TODO add your handling code here:
}
private void JButton3AncestorRemoved(javax.swing.event.AncestorEvent 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(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DigitalBuffer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DigitalBuffer().setVisible(true);
}
});
}
// Variables declaration - do not modify
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.JTextField t1;
// End of variables declaration
}
The thing is that i don't know how to delete the messages when the Resume Consumers is pressed
You add an ActionListener to the button. In the ActionListener you reset the text. See the section from the Swing tutorial on How to Us Buttons for more information and examples. Or, there is also a section on How to Write an ActionListener.
You can replace the text in the text field by using:
textField.setText(...);
Example #1 (Requires Java 8+):
button.addActionListener(e -> textField.setText(""));
Example #2:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
In both cases, the textField will need to have been declared as final.

RMI ClassCastException to remote interface

Hi i am having a problem with RMI.
I have this error when i run it.
java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to rmi.converter
This is the interface
converter.java
import java.rmi.RemoteException;
public interface converter {
double CelciusKelvin (double a) throws RemoteException;
double CelciusFahrenheit (double a) throws RemoteException;
double FahrenheitKelvin (double a) throws RemoteException;
double FahrenheitCelcius (double a) throws RemoteException;
double KelvinCelcius (double a) throws RemoteException;
double KelvinFahrenheit (double a) throws RemoteException;
double AtmosPascal (double a) throws RemoteException;
double AtmosPSI (double a) throws RemoteException;
double PascalAtmos (double a) throws RemoteException;
double PascalPSI (double a) throws RemoteException;
double PSIAtmos (double a) throws RemoteException;
double PSIPascal (double a) throws RemoteException;
}
This is the remote method
converterImp.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class converterImp extends UnicastRemoteObject implements converter {
public converterImp() throws RemoteException
{
}
double convert;
#Override
public double CelciusKelvin(double a) throws RemoteException {
convert = a+273.15;
return convert;
}
#Override
public double CelciusFahrenheit(double a) throws RemoteException {
convert = (a*1.8)+32;
return convert;
}
#Override
public double FahrenheitKelvin(double a) throws RemoteException {
convert = (a+459.67)*5/9;
return convert;
}
#Override
public double FahrenheitCelcius(double a) throws RemoteException {
convert=(a-32)/(9/5);
return convert;
}
#Override
public double KelvinCelcius(double a) throws RemoteException {
convert = a - 273.15;
return convert;
}
#Override
public double KelvinFahrenheit(double a) throws RemoteException {
convert=(a-273.15)* 1.8 + 32.00;
return convert;
}
#Override
public double AtmosPascal(double a) throws RemoteException {
convert = a*101325;
return convert;
}
#Override
public double AtmosPSI(double a) throws RemoteException {
convert = a*14.6959488;
return convert;
}
#Override
public double PascalAtmos(double a) throws RemoteException {
convert = a/101325;
return convert;
}
#Override
public double PascalPSI(double a) throws RemoteException {
convert=a*0.000145037;
return convert;
}
#Override
public double PSIAtmos(double a) throws RemoteException {
convert = a*0.0680459639;
return convert;
}
#Override
public double PSIPascal(double a) throws RemoteException {
convert =a*6894.75729;
return convert;
}
}
This is my GUI Client.
package projectrmiclient;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import rmi.converter;
public class ProjectRMIClient extends javax.swing.JFrame {
public ProjectRMIClient() {
initComponents();
}
String num = "";
String str = "";
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
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();
jLabel1 = new javax.swing.JLabel();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jButton23 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextPane1.setEditable(false);
jScrollPane1.setViewportView(jTextPane1);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("0");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setText("Cancel");
jButton11.setToolTipText("");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jLabel1.setText("Temperature");
jButton12.setText("C~F");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jButton13.setText("F~C");
jButton14.setText("K~C");
jButton15.setText("C~K");
jButton16.setText("F~K");
jButton17.setText("K~F");
jButton18.setText("A~PSI");
jButton19.setText("PSI~A");
jButton20.setText("Pas~A");
jButton21.setText("A~Pas");
jButton22.setText("PSI-Pas");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
jButton23.setText("Pas~A");
jLabel2.setText("Pressure");
jLabel3.setText("Result");
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.TRAILING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton11)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGap(36, 36, 36)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton22, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton21, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton23, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton16, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel2))
.addContainerGap(20, 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(34, 34, 34)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton11)
.addComponent(jButton10))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton15)
.addComponent(jButton12))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton16)
.addComponent(jButton13))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton17)
.addComponent(jButton14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton18)
.addComponent(jButton21))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton22)
.addComponent(jButton19))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton20)
.addComponent(jButton23))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
str = evt.getActionCommand();
num = num + str;
jTextPane1.setText(num);// TODO add your handling code here:
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
jTextPane1.setText("");
}
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// fire to localhost port 1099
Registry myRegistry = LocateRegistry.getRegistry("127.0.0.1", 1099);
// search for myMessage service
converter impl = (converter) myRegistry.lookup("PTConverter");
if(!jTextPane1.getText().isEmpty())
{
double input = Double.parseDouble(jTextPane1.getText());
// call server's method
double result=impl.CelciusFahrenheit(input);
String answer = result + "Fahrenheit";
jLabel4.setText(answer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* #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(ProjectRMIClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProjectRMIClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProjectRMIClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProjectRMIClient.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 ProjectRMIClient().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 jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
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.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
}
I developed the GUI using GUI Builder on netbeans.
The problem is when i pressed jButton12. it will show the error as stated.
You appear to have created a copy of your remote interface in a different package for the client. You can't do that. The remote interface has to be the same on both sides: The Same, and that does not include changing its package, or indeed any anything else.

Get the winner from Tic Tac Toe

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() )

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.

Categories