I am having trouble writing code to multiply the fields times the number put in to equal the total. I was able to make the clear button work
/*
* Order1.java
*
* Created on December 18, 2006, 6:16 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author test
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;
myFrame.setVisible(true) ;
}
}
class MyOrder1 extends JFrame implements ActionListener
{
private static final int WIDTH = 225 ;
private static final int HEIGHT = 150 ;
private static final int X_ORIGIN = 400 ;
private static final int Y_ORIGIN = 200 ;
// create new buttons and label them
private JButton Total ;
private JButton Reset ;
private JTextField ChickenField ;
private JTextField PorkField ;
private JTextField CattleField ;
//creates new label icons for my items
public MyOrder1()
{
String Response = new String() ;
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
JLabel Pork = new JLabel("Pork: $2.17 LB ") ;
JLabel Cattle = new JLabel("Cattle:$3.27 LB ") ;
ChickenField = new JTextField(5) ;
PorkField = new JTextField(5) ;
CattleField = new JTextField(5) ;
Total = new JButton("Total") ;
Total.addActionListener(this) ;
Reset = new JButton("Reset") ;
Reset.addActionListener(this) ;
// get the content pane
Container contentPane = getContentPane() ;
// create a new layout manager for the pane
FlowLayout aLayout = new FlowLayout() ;
// assign the new layout manager to the content pane
contentPane.setLayout(aLayout) ;
// add the "Exit" button to the content pane
contentPane.add(Chicken) ;
contentPane.add(ChickenField) ;
contentPane.add(Pork) ;
contentPane.add(PorkField) ;
contentPane.add(Cattle) ;
contentPane.add(CattleField) ;
contentPane.add(Total) ;
contentPane.add(Reset) ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource() ;
if (source == Reset)
{
ChickenField.setText(" ") ;
PorkField.setText(" ") ;
CattleField.setText(" ") ;
}
else if (source == Total)
9 1564
I am having trouble writing code to multiply the fields times the number put in to equal the total. I was able to make the clear button work
/*
* Order1.java
*
* Created on December 18, 2006, 6:16 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author test
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;
myFrame.setVisible(true) ;
}
}
class MyOrder1 extends JFrame implements ActionListener
{
private static final int WIDTH = 225 ;
private static final int HEIGHT = 150 ;
private static final int X_ORIGIN = 400 ;
private static final int Y_ORIGIN = 200 ;
// create new buttons and label them
private JButton Total ;
private JButton Reset ;
private JTextField ChickenField ;
private JTextField PorkField ;
private JTextField CattleField ;
//creates new label icons for my items
public MyOrder1()
{
String Response = new String() ;
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
JLabel Pork = new JLabel("Pork: $2.17 LB ") ;
JLabel Cattle = new JLabel("Cattle:$3.27 LB ") ;
ChickenField = new JTextField(5) ;
PorkField = new JTextField(5) ;
CattleField = new JTextField(5) ;
Total = new JButton("Total") ;
Total.addActionListener(this) ;
Reset = new JButton("Reset") ;
Reset.addActionListener(this) ;
// get the content pane
Container contentPane = getContentPane() ;
// create a new layout manager for the pane
FlowLayout aLayout = new FlowLayout() ;
// assign the new layout manager to the content pane
contentPane.setLayout(aLayout) ;
// add the "Exit" button to the content pane
contentPane.add(Chicken) ;
contentPane.add(ChickenField) ;
contentPane.add(Pork) ;
contentPane.add(PorkField) ;
contentPane.add(Cattle) ;
contentPane.add(CattleField) ;
contentPane.add(Total) ;
contentPane.add(Reset) ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource() ;
if (source == Reset)
{
ChickenField.setText(" ") ;
PorkField.setText(" ") ;
CattleField.setText(" ") ;
}
else if (source == Total)
To retrieve a value stored in a JTextField use
field.getText();
eg To get the value in the PorkField JTextField use - String porkValue = PorkField.getText();
So where are you getting the problem?
To retrieve a value stored in a JTextField use
field.getText();
eg To get the value in the PorkField JTextField use - String porkValue = PorkField.getText();
So where are you getting the problem?
I am having trouble putting in correct code to multiply the dollar amount times how many selected so when I hit the total button the dollar amount will show depending on how many items I put in the selection whether it is pork chicken or cattle
I am having trouble putting in correct code to multiply the dollar amount times how many selected so when I hit the total button the dollar amount will show depending on how many items I put in the selection whether it is pork chicken or cattle
Is the maths correct here?
Hi, there are still some things that need to be worked on in your code. So if the maths part is correct confirm so that we can do the next part.
Hello..
I am a newbie....
How about you try for this.... - import java.awt.*;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import javax.swing.*;
-
import java.lang.NumberFormatException;
-
-
public class Order1
-
{
-
public static void main(String[] args)
-
{
-
MyOrder1 myFrame = new MyOrder1() ;
-
-
myFrame.setVisible(true) ;
-
}
-
}
-
-
class MyOrder1 extends JFrame implements ActionListener
-
{
-
private static final int WIDTH = 225 ;
-
private static final int HEIGHT = 180 ;
-
private static final int X_ORIGIN = 400 ;
-
private static final int Y_ORIGIN = 200 ;
-
-
// create new buttons and label them
-
private JButton Total ;
-
private JButton Reset ;
-
private JTextField ChickenField ;
-
private JTextField PorkField ;
-
private JTextField CattleField ;
-
private JTextField TotalField ;
-
//creates new label icons for my items
-
public MyOrder1()
-
{
-
-
-
String Response = new String() ;
-
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
-
JLabel Pork = new JLabel (" Pork: $2.17 LB ") ;
-
JLabel Cattle = new JLabel(" Cattle: $3.27 LB ") ;
-
JLabel total = new JLabel(" Total : ");
-
ChickenField = new JTextField(5) ;
-
PorkField = new JTextField(5) ;
-
CattleField = new JTextField(5) ;
-
TotalField = new JTextField(5) ;
-
ChickenField.setText("0") ;
-
PorkField.setText("0") ;
-
CattleField.setText("0") ;
-
TotalField.setText(" ") ;
-
-
Total = new JButton("Total") ;
-
Total.addActionListener(this) ;
-
-
Reset = new JButton("Reset") ;
-
Reset.addActionListener(this) ;
-
-
-
-
// get the content pane
-
Container contentPane = getContentPane() ;
-
-
// create a new layout manager for the pane
-
FlowLayout aLayout = new FlowLayout() ;
-
-
// assign the new layout manager to the content pane
-
contentPane.setLayout(aLayout) ;
-
-
// add the "Exit" button to the content pane
-
contentPane.add(Chicken) ;
-
contentPane.add(ChickenField) ;
-
contentPane.add(Pork) ;
-
contentPane.add(PorkField) ;
-
contentPane.add(Cattle) ;
-
contentPane.add(CattleField) ;
-
contentPane.add(total) ;
-
contentPane.add(TotalField) ;
-
contentPane.add(Total) ;
-
contentPane.add(Reset) ;
-
-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
String chicken, pork, cattle;
-
float ayam=0, babi=0, sapi=0;
-
int i;
-
-
Object source = e.getSource() ;
-
-
if (source == Reset)
-
{
-
ChickenField.setText("0") ;
-
PorkField.setText("0") ;
-
CattleField.setText("0") ;
-
}
-
else if (source == Total)
-
{
-
i = 0;
-
try
-
{
-
chicken = ChickenField.getText();
-
ayam = Float.valueOf(chicken).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
try
-
{
-
pork = PorkField.getText();
-
babi = Float.valueOf(pork).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
try
-
{
-
cattle = CattleField.getText();
-
sapi = Float.valueOf(cattle).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
if (i==0)
-
{
-
float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
-
String tot= String.valueOf(total);
-
TotalField.setText(tot) ;
-
-
}
-
-
}
-
}
-
}
Integer.parseInt()
What is the function of it ?
I never use it before
Is it use to convert the string to integer?
And how about if the string is alphabet ? Do we need exception to make sure it doesn't error?? - /*
-
int chicken = Integer.parseInt(ChickenField.getText());
-
Hello..
I am a newbie....
How about you try for this.... - import java.awt.*;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import javax.swing.*;
-
import java.lang.NumberFormatException;
-
-
public class Order1
-
{
-
public static void main(String[] args)
-
{
-
MyOrder1 myFrame = new MyOrder1() ;
-
-
myFrame.setVisible(true) ;
-
}
-
}
-
-
class MyOrder1 extends JFrame implements ActionListener
-
{
-
private static final int WIDTH = 225 ;
-
private static final int HEIGHT = 180 ;
-
private static final int X_ORIGIN = 400 ;
-
private static final int Y_ORIGIN = 200 ;
-
-
// create new buttons and label them
-
private JButton Total ;
-
private JButton Reset ;
-
private JTextField ChickenField ;
-
private JTextField PorkField ;
-
private JTextField CattleField ;
-
private JTextField TotalField ;
-
//creates new label icons for my items
-
public MyOrder1()
-
{
-
-
-
String Response = new String() ;
-
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
-
JLabel Pork = new JLabel (" Pork: $2.17 LB ") ;
-
JLabel Cattle = new JLabel(" Cattle: $3.27 LB ") ;
-
JLabel total = new JLabel(" Total : ");
-
ChickenField = new JTextField(5) ;
-
PorkField = new JTextField(5) ;
-
CattleField = new JTextField(5) ;
-
TotalField = new JTextField(5) ;
-
ChickenField.setText("0") ;
-
PorkField.setText("0") ;
-
CattleField.setText("0") ;
-
TotalField.setText(" ") ;
-
-
Total = new JButton("Total") ;
-
Total.addActionListener(this) ;
-
-
Reset = new JButton("Reset") ;
-
Reset.addActionListener(this) ;
-
-
-
-
// get the content pane
-
Container contentPane = getContentPane() ;
-
-
// create a new layout manager for the pane
-
FlowLayout aLayout = new FlowLayout() ;
-
-
// assign the new layout manager to the content pane
-
contentPane.setLayout(aLayout) ;
-
-
// add the "Exit" button to the content pane
-
contentPane.add(Chicken) ;
-
contentPane.add(ChickenField) ;
-
contentPane.add(Pork) ;
-
contentPane.add(PorkField) ;
-
contentPane.add(Cattle) ;
-
contentPane.add(CattleField) ;
-
contentPane.add(total) ;
-
contentPane.add(TotalField) ;
-
contentPane.add(Total) ;
-
contentPane.add(Reset) ;
-
-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
String chicken, pork, cattle;
-
float ayam=0, babi=0, sapi=0;
-
int i;
-
-
Object source = e.getSource() ;
-
-
if (source == Reset)
-
{
-
ChickenField.setText("0") ;
-
PorkField.setText("0") ;
-
CattleField.setText("0") ;
-
}
-
else if (source == Total)
-
{
-
i = 0;
-
try
-
{
-
chicken = ChickenField.getText();
-
ayam = Float.valueOf(chicken).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
try
-
{
-
pork = PorkField.getText();
-
babi = Float.valueOf(pork).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
try
-
{
-
cattle = CattleField.getText();
-
sapi = Float.valueOf(cattle).floatValue();
-
}
-
catch (Exception d)
-
{
-
JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",
-
JOptionPane.ERROR_MESSAGE);
-
i=1;
-
}
-
if (i==0)
-
{
-
float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
-
String tot= String.valueOf(total);
-
TotalField.setText(tot) ;
-
-
}
-
-
}
-
}
-
}
The exception handling introduced here was the next step. It now makes the program more robust. Well done and thanks muliyono. I now have only the following comments:
I would normally use double instead of float though(for starters its bigger).
It does not look right to have the total being shown in a textfield too. A JLabel seems more appropriate since the total should not be editable.
Also if the inputs are quantities then using an int would make more sense that using a float.
Thanks that works perfectly, I appreciate your help
Thanks that works perfectly, I appreciate your help
Welcome. Remember to come back and help others as well where you can.
Oh and have a merry Christmas.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Elaine Jackson |
last post by:
Is there a function that takes a number with binary numeral a1...an to the
number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice
versa? (For example, the function's value at...
|
by: Batista, Facundo |
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing the code.
Thank you.
.. Facundo
|
by: Batista, Facundo |
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing/modifying the code.
Thank you.
.. Facundo
|
by: I.V. Aprameya Rao |
last post by:
hi
i have been wondering, how does python store its very long integers and
perform aritmetic on it.
i needed to implement this myself and was thinking of storing the digits
of an integer in a...
|
by: administrata |
last post by:
Hi! it's been about a week learning python!
I've read 'python programming for the absolute begginer'
I don't understand about % like...
107 % 4 = 3
7 % 3 = 1
I'm confused with division :/...
|
by: Amit Bhatia |
last post by:
Hi there.
I am cross posting this on comp.lang.c as well: sorry for same.
The problem I am facing is as follows:
For example:
double a= 0.15;
double b=2.4;
const double VERYTINY =1.e-10;
I...
|
by: Ken |
last post by:
Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?
Thanks!
Ken
|
by: James H. |
last post by:
Greetings! I'm new to Python and am struggling a little with "and" and
"or" logic in Python. Since Python always ends up returning a value
and this is a little different from C, the language I...
|
by: Sheldon |
last post by:
In c#.net what is the operator to raise a number to a power. I tried ^, but
while it compiles, it does not give back what I would expect?
Thanks
Sheldon
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |