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.
Related
I've made a picture puzzle game. actually I use Netbeans 8.2 GUI Builder to create the game, I have made Timer and move.
But I am confused how to shuffle the images. I've searched on various sites but it did not help.
This is my code:
package projetu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import static projetu.fasil.cont;
public class t02 extends javax.swing.JFrame {
static int cont = 0;
int i = 0;
private Timer timer;
private int currentSegundo = 0;
private int currentMinuto = 0;
private int currentHora = 0;
private int velocidade = 1000;
public t02() {
initComponents();
b9.setVisible(false);
}
public void contamuda() {
txtconta.setText(String.valueOf(cont++));
}
private void iniciarContagem() {
ActionListener action;
action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentSegundo++;
if (currentSegundo == 60) {
currentMinuto++;
currentSegundo = 0;
}
if (currentMinuto == 60) {
currentHora++;
currentMinuto = 0;
}
String hr = currentHora <= 9 ? "0" + currentHora : currentHora + "";
String min = currentMinuto <= 9 ? "0" + currentMinuto : currentMinuto + "";
String seg = currentSegundo <= 9 ? "0" + currentSegundo : currentSegundo + "";
tempu.setText(hr + ":" + min + ":" + seg);
}
};
this.timer = new Timer(velocidade, action);
this.timer.start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b4 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txtconta = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tempu = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout(3, 3));
b1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t11.gif"))); // NOI18N
b1.setBorder(null);
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setFocusable(false);
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});
jPanel1.add(b1);
b2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t12.gif"))); // NOI18N
b2.setBorder(null);
b2.setBorderPainted(false);
b2.setContentAreaFilled(false);
b2.setFocusable(false);
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});
jPanel1.add(b2);
b3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t13.gif"))); // NOI18N
b3.setBorder(null);
b3.setBorderPainted(false);
b3.setContentAreaFilled(false);
b3.setFocusable(false);
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});
jPanel1.add(b3);
b4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t14.gif"))); // NOI18N
b4.setBorder(null);
b4.setBorderPainted(false);
b4.setContentAreaFilled(false);
b4.setFocusable(false);
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});
jPanel1.add(b4);
b5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t15.gif"))); // NOI18N
b5.setBorder(null);
b5.setBorderPainted(false);
b5.setContentAreaFilled(false);
b5.setFocusable(false);
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});
jPanel1.add(b5);
b6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t16.gif"))); // NOI18N
b6.setBorder(null);
b6.setBorderPainted(false);
b6.setContentAreaFilled(false);
b6.setFocusable(false);
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});
jPanel1.add(b6);
b7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t17.gif"))); // NOI18N
b7.setBorder(null);
b7.setBorderPainted(false);
b7.setContentAreaFilled(false);
b7.setFocusable(false);
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});
jPanel1.add(b7);
b8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t18.gif"))); // NOI18N
b8.setBorder(null);
b8.setBorderPainted(false);
b8.setContentAreaFilled(false);
b8.setFocusable(false);
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});
jPanel1.add(b8);
b9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t19.gif"))); // NOI18N
b9.setBorder(null);
b9.setBorderPainted(false);
b9.setContentAreaFilled(false);
b9.setFocusable(false);
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});
jPanel1.add(b9);
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel1.setText("Muda :");
txtconta.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
txtconta.setText("0");
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel2.setText("Tempu =");
tempu.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
tempu.setText("00:00:00");
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais01/t02_lbl.jpg"))); // NOI18N
jLabel3.setAutoscrolls(true);
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(0, 89, Short.MAX_VALUE))
);
jButton1.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton1.setText("Komesa");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton3.setText("Valor");
jButton4.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton4.setText("Sai ");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton2.setText("Hili Imagen");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addContainerGap())
);
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel4.setText("Game puzzle tais no uma adat iha Timor Leste");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtconta, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tempu))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
.addComponent(txtconta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tempu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b1.getText());
b2.setIcon(b1.getIcon());
b1.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b1.getText());
b4.setIcon(b1.getIcon());
b1.setVisible(false);
b4.setVisible(true);
}
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b2.getText());
b1.setIcon(b2.getIcon());
b2.setVisible(false);
b1.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b2.getText());
b3.setIcon(b2.getIcon());
b2.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b2.getText());
b5.setIcon(b2.getIcon());
b2.setVisible(false);
b5.setVisible(true);
}
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b3.getText());
b2.setIcon(b3.getIcon());
b3.setVisible(false);
b2.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b3.getText());
b6.setIcon(b3.getIcon());
b3.setVisible(false);
b6.setVisible(true);
}
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b4.getText());
b1.setIcon(b4.getIcon());
b4.setVisible(false);
b1.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b4.getText());
b5.setIcon(b4.getIcon());
b4.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b4.getText());
b7.setIcon(b4.getIcon());
b4.setVisible(false);
b7.setVisible(true);
}
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b5.getText());
b2.setIcon(b5.getIcon());
b5.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b4.getText());
b4.setIcon(b4.getIcon());
b5.setVisible(false);
b4.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b5.getText());
b6.setIcon(b5.getIcon());
b5.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b5.getText());
b8.setIcon(b5.getIcon());
b5.setVisible(false);
b8.setVisible(true);
}
}
private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b9.isVisible() == false) {
b9.setText(b6.getText());
b9.setIcon(b6.getIcon());
b6.setVisible(false);
b9.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b6.getText());
b3.setIcon(b6.getIcon());
b6.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b6.getText());
b5.setIcon(b6.getIcon());
b6.setVisible(false);
b5.setVisible(true);
}
}
private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b4.isVisible() == false) {
b4.setText(b7.getText());
b4.setIcon(b7.getIcon());
b7.setVisible(false);
b4.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b7.getText());
b8.setIcon(b7.getIcon());
b7.setVisible(false);
b8.setVisible(true);
}
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b5.isVisible() == false) {
b5.setText(b8.getText());
b5.setIcon(b8.getIcon());
b8.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b7.getText());
b7.setIcon(b7.getIcon());
b8.setVisible(false);
b7.setVisible(true);
}
if (b9.isVisible() == false) {
b9.setText(b8.getText());
b9.setIcon(b8.getIcon());
b8.setVisible(false);
b9.setVisible(true);
}
}
private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b6.isVisible() == false) {
b6.setText(b9.getText());
b6.setIcon(b9.getIcon());
b9.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b9.getText());
b8.setIcon(b9.getIcon());
b9.setVisible(false);
b8.setVisible(true);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
iniciarContagem();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new hili_imagen().setVisible(true);
dispose();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new game().setVisible(true);
dispose();
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new t02().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel tempu;
private javax.swing.JLabel txtconta;
// End of variables declaration
}
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.
I made a calculator in netbeans so far i added the plus and minus button to work. but lets say i do 5+5 it gives me 10 and if i add 5 more it give 15. so that parts correct. but if i do 9-5 it gives me -4, and if i subtrac 2 from the -4 i get positive 6. please help, im fairly new to java.
public class Calculator extends javax.swing.JFrame {
/**
* Creates new form Calculator
*/
public Calculator() {
initComponents();
}
double fNums = 0.0;
double sNums = 0.0;
boolean add = false;
boolean sub = false;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
display = new javax.swing.JTextField();
seven = new javax.swing.JButton();
eight = new javax.swing.JButton();
nine = new javax.swing.JButton();
plus = new javax.swing.JButton();
four = new javax.swing.JButton();
five = new javax.swing.JButton();
six = new javax.swing.JButton();
minus = new javax.swing.JButton();
one = new javax.swing.JButton();
two = new javax.swing.JButton();
three = new javax.swing.JButton();
multiply = new javax.swing.JButton();
zero = new javax.swing.JButton();
decimal = new javax.swing.JButton();
divide = new javax.swing.JButton();
enter = new javax.swing.JButton();
clear = new javax.swing.JButton();
jMenuBar2 = new javax.swing.JMenuBar();
jMenu4 = new javax.swing.JMenu();
Home = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
display.setEditable(false);
seven.setText("7");
seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sevenActionPerformed(evt);
}
});
eight.setText("8");
eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eightActionPerformed(evt);
}
});
nine.setText("9");
nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nineActionPerformed(evt);
}
});
plus.setText("+");
plus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
plusActionPerformed(evt);
}
});
four.setText("4");
four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fourActionPerformed(evt);
}
});
five.setText("5");
five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fiveActionPerformed(evt);
}
});
six.setText("6");
six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sixActionPerformed(evt);
}
});
minus.setText("-");
minus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
minusActionPerformed(evt);
}
});
one.setText("1");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});
two.setText("2");
two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
twoActionPerformed(evt);
}
});
three.setText("3");
three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
threeActionPerformed(evt);
}
});
multiply.setText("*");
multiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplyActionPerformed(evt);
}
});
zero.setText("0");
zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeroActionPerformed(evt);
}
});
decimal.setText(".");
decimal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
decimalActionPerformed(evt);
}
});
divide.setText("/");
divide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divideActionPerformed(evt);
}
});
enter.setText("Enter");
enter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enterActionPerformed(evt);
}
});
clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(display)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(54, Short.MAX_VALUE))
);
jMenu4.setText("File");
Home.setText("Home");
Home.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HomeActionPerformed(evt);
}
});
jMenu4.add(Home);
jMenuBar2.add(jMenu4);
jMenu5.setText("Edit");
jMenuBar2.add(jMenu5);
setJMenuBar(jMenuBar2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void HomeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Menu m = new Menu();
m.setVisible(true);
}
private void sixActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + six.getText());
}
private void divideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + divide.getText());
}
private void sevenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + seven.getText());
}
private void eightActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + eight.getText());
}
private void nineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + nine.getText());
}
private void plusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums + Double.parseDouble(display.getText());
display.setText("");
add = true;
}
private void fourActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + four.getText());
}
private void fiveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + five.getText());
}
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + one.getText());
}
private void twoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + two.getText());
}
private void threeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + three.getText());
}
private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + multiply.getText());
}
private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + zero.getText());
}
private void decimalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + decimal.getText());
}
private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText("");
fNums = 0;
sNums = 0;
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Home;
private javax.swing.JButton clear;
private javax.swing.JButton decimal;
private javax.swing.JTextField display;
private javax.swing.JButton divide;
private javax.swing.JButton eight;
private javax.swing.JButton enter;
private javax.swing.JButton five;
private javax.swing.JButton four;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton minus;
private javax.swing.JButton multiply;
private javax.swing.JButton nine;
private javax.swing.JButton one;
private javax.swing.JButton plus;
private javax.swing.JButton seven;
private javax.swing.JButton six;
private javax.swing.JButton three;
private javax.swing.JButton two;
private javax.swing.JButton zero;
// End of variables declaration
}
First: You are missing the == in enterActionPerformed. Use two = signs.
Second: If I understood correctly, everytime there's a '-', the number gets negated. Which is happening after the first number is written.
9-5 gives you 4 because you are negating the first number and adding it to the second:
(-9)+5 =-4
The error is in the following code, when you press the '-' button, you are doing 0-valueEntered.
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
`
`if(sub == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
You can check out my MathExpressionEvaluator repository from Github, as it can easily evaluate simple math expressions.
You can pass it a string expression from your calculator and display the answer that you get from it easily.
Change the Minus listener to
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = Double.parseDouble(display.getText()); <----------
display.setText("");
sub = true;
}
and enter action performed as
private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub == true){
sNums = fNums - Double.parseDouble(display.getText()); <----------
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
And Check
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.
I want to print a page by clicking a button but the following code shows run time error. I have used the JFrame form as the printing page and added a JButton and a JTextField. There is the error in finding the symbols (JButton and JTextField). Any one can help ?
package yelloclient;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.geom.*;
/**
*
* #author Pavan Poudel
*/
#SuppressWarnings("unchecked")
public class PrintDoc extends javax.swing.JFrame {
/**
* Creates new form PrintDoc
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrintDoc().setVisible(true);
}
});
}
private void PrintDoc() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Print");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setText("Hello, how are you ?");
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(164, 164, 164)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PrintDoc pr = new PrintDoc();
PrintForm.printComponent(pr);
}
}
class PrintForm implements Printable {
private Component component;
public static void printComponent(Component c) {
new yelloclient.txtPrint(c).print();
}
public PrintForm(Component component) {
this.component = component;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error in printing !!! " + pe);
}
}
public int print(Graphics g, PageFormat pf, int pageIndex) {
RepaintManager currentManager = RepaintManager.currentManager(component);
if (pageIndex > 0)
{
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/** Disable double buffering as double buffering slows the printing*/
currentManager.setDoubleBufferingEnabled(false);
component.paint(g2d);
currentManager.setDoubleBufferingEnabled(true);
return(PAGE_EXISTS);
}
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}
The error is inside the function
private void initComponents(){ ... }
such that
cannot find symbol
symbol: variable jButton1
location: class PrintDoc
----
(Alt-Enter shows hints)
and so on.