1 Attachment(s)
Problem with App, used a template, not working correctly.
Hello,
Im having a problem with my second Applet for my java programming class. The professor gave out a template sheet to use as a reference and I filled in everything as needed to produce the program. It just isnt working correctly. :(-:Thank you for any help or advice. :)-:
It should look like this:
Attachment 3547
and heres the code:
____________________________________
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.text.*;
public class Assignment8 extends JFrame implements ActionListener, ItemListener
{
double sPrice = 6.99, mPrice = 8.99, lPrice = 10.99;
double totalPrice = sPrice;
ButtonGroup sizeGrp = new ButtonGroup();
JRadioButton sRB = new JRadioButton("Small",true);
JRadioButton mRB = new JRadioButton("Medium",false);
JRadioButton lRB = new JRadioButton("Large",false);
double sausagePrice = 1.49, pepperoniPrice = 1.49, salamiPrice = 1.49,
olivesPrice = 0.99, mushroomsPrice = 0.99, anchoviesPrice = 0.99;
JCheckBox sausageBox = new JCheckBox("Sausage",false);
JCheckBox pepperoniBox = new JCheckBox("Pepperoni",false);
JCheckBox salamiBox = new JCheckBox("Salami",false);
JCheckBox olivesBox = new JCheckBox("Olives",false);
JCheckBox mushroomsBox = new JCheckBox("Mushrooms",false);
JCheckBox anchoviesBox = new JCheckBox("Anchovies",false);
JButton exit = new JButton("Exit");
JPanel topPanel = new JPanel(new FlowLayout());
JPanel middlePanel = new JPanel(new FlowLayout());
JPanel bottomPanel = new JPanel(new FlowLayout());
JTextField totPrice = new JTextField(10);
Container con = getContentPane();
NumberFormat currency = NumberFormat.getCurrencyInstance();
public Assignment8()
{
super("Pizza Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setLayout(new BorderLayout());
sizeGrp.add(sRB);
sRB.addItemListener(this);
sizeGrp.add(mRB);
mRB.addItemListener(this);
sizeGrp.add(lRB);
lRB.addItemListener(this);
Border topBorder = BorderFactory.createEtchedBorder();
topBorder = BorderFactory.createTitledBorder(topBorder,"Size");
topPanel.add(sRB);
topPanel.add(mRB);
topPanel.add(lRB);
Border middleBorder = BorderFactory.createEtchedBorder();
middleBorder = BorderFactory.createTitledBorder(middleBorder,"Toppings");
middlePanel.add(sausageBox);
middlePanel.add(pepperoniBox);
middlePanel.add(salamiBox);
middlePanel.add(olivesBox);
middlePanel.add(mushroomsBox);
middlePanel.add(anchoviesBox);
Border bottomBorder = BorderFactory.createEtchedBorder();
bottomBorder = BorderFactory.createTitledBorder(bottomBorder);
totPrice.setText(currency.format(totalPrice));
bottomPanel.add(totPrice);
bottomPanel.add(exit);
exit.addActionListener(this);
con.add(bottomPanel, BorderLayout.SOUTH);
}
public static void main(String[] arguments)
{
Assignment8 aFrame = new Assignment8();
aFrame.setSize(450,225);
aFrame.setResizable(false);
aFrame.setVisible(true);
}
public void itemStateChanged(ItemEvent check)
{
Object source = check.getItem();
if(source == sRB)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += sPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= sPrice;
}
if(source == mRB)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += mPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= mPrice;
}
if(source == sausageBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += sausagePrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= sausagePrice;
}
if(source == pepperoniBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += pepperoniPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= pepperoniPrice;
}
if(source == salamiBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += salamiPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= salamiPrice;
}
if(source == olivesBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += olivesPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= olivesPrice;
}
if(source == mushroomsBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += mushroomsPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= mushroomsPrice;
}
if(source == anchoviesBox)
{
int select = check.getStateChange();
if(select == ItemEvent.SELECTED)
totalPrice += anchoviesPrice;
else if(select == ItemEvent.DESELECTED)
totalPrice -= anchoviesPrice;
}
totPrice.setText(currency.format(totalPrice));
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == exit)
System.exit(0);
}
}
Re: Problem with Applet, used a template, not working correctly.
Quote:
It just isnt working correctly
Please explain what is not working correctly.
Re: Problem with Applet, used a template, not working correctly.
The only part working is the text field and the exit button.
Re: Problem with Applet, used a template, not working correctly.
That's good that some of it works. Now pick one part that does not work and explain all about what happens or does not happen.
Post moved from Applet section. Nothing to do with java applets.
Re: Problem with App, used a template, not working correctly.
The top two panels are blank, I can't see my radio buttons or check boxes.
Re: Problem with App, used a template, not working correctly.
Where do you add those panels to the GUI so they can be shown?
Re: Problem with App, used a template, not working correctly.
Re: Problem with App, used a template, not working correctly.
What is the line number where you add the top panel to the GUI.
Re: Problem with App, used a template, not working correctly.
Re: Problem with App, used a template, not working correctly.
Are you using the line numbers from your post? Here are lines 79,80 and 81 Code:
public static void main(String[] arguments)
{
Assignment8 aFrame = new Assignment8();
Line 80 is the {
Look at the code you posted and use the line number from there.
Hnt: You have NOT added the top panel to the GUI
Re: Problem with App, used a template, not working correctly.
I was stating the number 80 from a smart phone and I see that it was not the exact line 80 now that I am logged into a regular computer.
But I'll look into the "hint" you suggested.