JDialog

Hey all,

I want to not only have the button event handler display the next but i also want to keep a running total of the buttons pressed. For some reason java does not like int total=total +25; It will not let me do that. When i create a new variable say total1 it says i havent initialized it. Can someone help me here?

/*

* CoinDeposit.java

*

* Created on June 1, 2007, 2:23 PM

*/

/**

*

* @author pberardi

*/

import javax.swing.JOptionPane;

publicclass CoinDepositextends javax.swing.JDialog{

/** Creates new form CoinDeposit */

public CoinDeposit(java.awt.Frame parent,boolean modal){

super(parent, modal);

initComponents();

}

/** 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.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

fiveCentButton =new javax.swing.JButton();

tenCentButton =new javax.swing.JButton();

twentyfiveCentButton =new javax.swing.JButton();

display =new javax.swing.JLabel();

total =new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

fiveCentButton.setText("deposit 5 cents");

fiveCentButton.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

fiveCentButtonActionPerformed(evt);

}

});

tenCentButton.setText("deposit 10 cents");

tenCentButton.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

tenCentButtonActionPerformed(evt);

}

});

twentyfiveCentButton.setText("deposit 25 cents");

twentyfiveCentButton.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

twentyfiveCentButtonActionPerformed(evt);

}

});

display.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

display.setText(" ");

display.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout layout =new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(132, 132, 132)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,false)

.addComponent(twentyfiveCentButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(tenCentButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(fiveCentButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

.addGroup(layout.createSequentialGroup()

.addGap(138, 138, 138)

.addComponent(display))

.addGroup(layout.createSequentialGroup()

.addGap(163, 163, 163)

.addComponent(total)))

.addContainerGap(157, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(33, 33, 33)

.addComponent(fiveCentButton)

.addGap(21, 21, 21)

.addComponent(tenCentButton)

.addGap(28, 28, 28)

.addComponent(twentyfiveCentButton)

.addGap(39, 39, 39)

.addComponent(display)

.addGap(32, 32, 32)

.addComponent(total)

.addContainerGap(64, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

privatevoid twentyfiveCentButtonActionPerformed(java.awt.event.ActionEvent evt){

[b] total.setText ("Your total is");[/b]

display.setText("You just deposited twenty-five cents");// TODO add your handling code here:

}

privatevoid tenCentButtonActionPerformed(java.awt.event.ActionEvent evt){

total.setText ("Your total is");

display.setText("You just deposited ten cents");// TODO add your handling code here:

}

privatevoid fiveCentButtonActionPerformed(java.awt.event.ActionEvent evt){

total.setText ("Your total is");

display.setText("You just deposited five cents");// TODO add your handling code here:

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new CoinDeposit(new javax.swing.JFrame(),true).setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JLabel display;

private javax.swing.JButton fiveCentButton;

private javax.swing.JButton tenCentButton;

private javax.swing.JLabel total;

private javax.swing.JButton twentyfiveCentButton;

// End of variables declaration

}

[8359 byte] By [pberardi1a] at [2007-11-27 6:16:17]
# 1

Netbeans? god it makes ugly code!

How about an int var that holds the total?

public class CoinDeposit extends javax.swing.JDialog {

private int myTotal = 0;

Then update it every time a button is pressed:

private void twentyfiveCentButtonActionPerformed(java.awt.event.ActionEvent evt) {

myTotal += 25;

showTotal("twenty-five cents");

}

private void tenCentButtonActionPerformed(java.awt.event.ActionEvent evt) {

myTotal += 10;

showTotal("ten cents");

}

private void fiveCentButtonActionPerformed(java.awt.event.ActionEvent evt) {

myTotal += 5;

showTotal("five cents");

}

private void showTotal(String howMuch)

{

total.setText ("Your total is " + myTotal);

display.setText("You just deposited " + howMuch);

}

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:27:45 > top of Java-index,Java Essentials,New To Java...
# 2

> Hey all,

> I want to not only have the button event handler

> display the next but i also want to keep a running

> total of the buttons pressed. For some reason java

> does not like int total=total +25; It will not let

> me do that. When i create a new variable say total1

> it says i havent initialized it. Can someone help me

> here?

What?

You declare a variable as

int total = 0;

and then you do

total = total + 25; // or total += 25;

to increase the value.

Kaj

kajbja at 2007-7-12 17:27:45 > top of Java-index,Java Essentials,New To Java...
# 3

> You declare a variable as

> > int total = 0;

>

> and then you do

> > total = total + 25; // or total += 25;

>

> to increase the value.

>

> Kaj

Great minds think alike!

petes1234a at 2007-7-12 17:27:45 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks,I was putting under the super statement in the instance and not in the public class and it wasn't initializing....duh!
pberardi1a at 2007-7-12 17:27:45 > top of Java-index,Java Essentials,New To Java...