This is the output:
https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-prn1/541619_462450913826230_932486202_n.jpg
This happens when I input "1" in the quantity textfield.
(The quantity textfield turns null after inputing a value)
But when I put "1" in the quantity textfield and click Oranges, it overwrites the Apples.
https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash4/299813_462450910492897_2137805386_n.jpg
WHat i wanted to happen is that it'll tally my selections. Like this:
((I ONLY EDITED THIS))
https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-prn1/602666_462450900492898_1863944306_n.jpg
These are my codes so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener{
JLabel lblTitle, lblQty, lblTotal;
JRadioButton radApple, radOrange, radPomelo;
JTextField txtQty;
JTextArea txtScreen;
Button btnAdd, btnPrint, btnCancel, btnExit, btnStock;
ButtonGroup bg;
Main(){
getContentPane();
setSize(550,490);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Tricia's Grocery Store");
add(lblTitle=new JLabel("Tricia's Grocery Store"));
lblTitle.setFont(new Font("Broadway", Font.PLAIN, 35));
lblTitle.setBounds(50,0,600,80);
add(radApple=new JRadioButton("Apples ---------- P15.75"));
add(radOrange=new JRadioButton("Oranges -------- P12.50"));
add(radPomelo=new JRadioButton("Pomelos -------- P95.25"));
radApple.setBounds(70,100,150,50);
radOrange.setBounds(70,130,155,50);
radPomelo.setBounds(70,160,155,50);
bg = new ButtonGroup();
bg.add(radApple);
bg.add(radOrange);
bg.add(radPomelo);
add(lblQty=new JLabel("Quantity:"));
lblQty.setBounds(70,200,150,50);
add(txtQty=new JTextField(2));
txtQty.setBounds(150,210,70,30);
add(lblTotal=new JLabel("P0.00"));
lblTotal.setBounds(150,500,500,500);
add(btnAdd=new Button("Add Item"));
btnAdd.setBounds(70,270,80,20);
add(btnPrint=new Button("Print Receipt"));
btnPrint.setBounds(150,270,80,20);
add(btnCancel=new Button("Reset Values"));
btnCancel.setBounds(70,290,80,20);
add(btnExit=new Button("Close"));
btnExit.setBounds(150,290,80,20);
add(txtScreen=new JTextArea());
txtScreen.setFont(new Font("Courier",Font.PLAIN,15));
txtScreen.setBounds(250,100,270,320);
//txtScreen.setEditable(false);
btnAdd.addActionListener(this);
radApple.addActionListener(this);
radOrange.addActionListener(this);
radPomelo.addActionListener(this);
txtQty.addActionListener(this);
}
public static void main(String[]args){
Main main = new Main();
}
public void actionPerformed(ActionEvent e) {
double appleprice=0, orangeprice=0, pomeloprice=0;
int qty = Integer.parseInt(txtQty.getText());
String strApples= "", strOranges = "", strPomelos = "";
if (radApple.isSelected()){
appleprice += 15.75 * qty;
strApples = ("Apples ------ x" + txtQty.getText()+ " ----- P" + appleprice);
txtScreen.setText(strApples + strOranges + strPomelos);
txtQty.setText(null);
}
if (radOrange.isSelected()){
orangeprice += 12.50 * qty;
strOranges = ("Oranges ------ x" + txtQty.getText()+ " ----- P" + orangeprice);
txtScreen.setText(strApples + strOranges + strPomelos);
txtQty.setText(null);
}
if (radPomelo.isSelected()){
pomeloprice += 95.25 * qty;
strPomelos = ("Pomelos ------ x" + txtQty.getText()+ " ----- P" + pomeloprice);
txtScreen.setText(strApples + strOranges + strPomelos);
txtQty.setText(null);
}
}
}