Someone try my code. And help me with the txtQty TextField.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import javax.swing.*;

public class Cashier extends Applet implements ActionListener, KeyListener  {

    private final double applePrice = 15.75,
                         orangePrice = 13.50,
                         pomeloPrice = 95.25;

    Panel topPanel,
          eastPanel,
          westPanel;

    Label lblTitle,
          lblQty,
          lblTotal;

    JRadioButton radApple,
                 radOrange,
                 radPomelo;

    TextField txtQty;

    JTextArea txtScreen;

    Button btnDone,
           btnPrint,
           btnCancel,
           btnExit,
           btnStock;

    ButtonGroup bg;


    static double appleprice=0, orangeprice=0, pomeloprice=0;
    static double totalprice=0;
    static String strApples="", strOranges = "", strPomelos = "";

    public void init(){

        setSize(600,490);
        setVisible(true);
        setLayout(null);

        add(lblTitle=new Label("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 Label("Quantity:"));
        lblQty.setBounds(70,200,150,50);

        add(txtQty=new TextField("1",2));
        txtQty.setBounds(150,210,70,30);

        add(lblTotal=new Label("P0.00"));
        lblTotal.setBounds(150,500,500,500);

        add(btnDone=new Button("Done"));
        btnDone.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("----------------------------------"+"\n-------------WELCOME--------------" 
                +"\n----------------------------------"+ "\nProduct  |  Quantity  |  Price"));
        txtScreen.setFont(new Font("Courier",Font.PLAIN,15));
        txtScreen.setBackground(new Color(64,64,64));
        txtScreen.setForeground(new Color(0,255,255));
        txtScreen.setBounds(250,100,300,320);
        txtScreen.setEditable(false);

        radApple.addActionListener(this);
        radOrange.addActionListener(this);
        radPomelo.addActionListener(this);
        txtQty.addKeyListener(this);
        btnPrint.addActionListener(this);
        btnDone.addActionListener(this);
    }

    public void start(){
        txtQty.selectAll();
    }
    public void keyPressed(KeyEvent arg0) {}
    public void keyTyped(KeyEvent arg0) {}

    public void keyReleased(KeyEvent e) { 

        // Trap all non-valid numbers
        try {
            Integer.parseInt(txtQty.getText());
        }
        catch (NumberFormatException fe) {
            txtQty.setText("0");
        }

        refreshPrice();
    }

    public void actionPerformed(ActionEvent e) {

        String strReceipt = txtScreen.getText();
        if(e.getSource()== btnPrint){
            JOptionPane.showMessageDialog(null, strReceipt,"Invoice",JOptionPane.INFORMATION_MESSAGE);
        }

        if(e.getSource()==btnDone){
            txtScreen.append("\n" + "\n" + "\n");
            txtScreen.append("          Total Price: " + lblTotal.getText());
            bg.clearSelection();

        }
        refreshPrice();
    }

    private void refreshPrice() {

        int qty = Integer.parseInt(txtQty.getText());
        double totalprice;

        if (radApple.isSelected()){
            appleprice += 15.75 * qty;
            txtScreen.append("\n");
            txtScreen.append("  Apples -----  x" + txtQty.getText()+ " ----- P" + appleprice);
        }
        if (radOrange.isSelected()){
            orangeprice += 12.50 * qty;
            txtScreen.append("\n");
            txtScreen.append(" Oranges -----  x" + txtQty.getText()+ " ----- P" + orangeprice);
        }
        if (radPomelo.isSelected()){
            pomeloprice += 95.25 * qty;
            txtScreen.append("\n");
            txtScreen.append(" Pomelos -----  x" + txtQty.getText()+ " ----- P" + pomeloprice);
        }
        totalprice=appleprice+orangeprice+pomeloprice;
        lblTotal.setText("$"+(totalprice));
        }
    public static void main(String[]args){
        Cashier wew = new Cashier();
    }
}

Recommended Answers

All 4 Replies

hai iciaguevara,

Could you explain me breifly about what the problem is?

so that we can try to give a way for the solution.

let me know

hai iciaguevara,

yeah i got your problem.

you haven't set the bounadries properly for quantity textfeild because its overlapping with the above feilds whatever you declared in the code.

i tested your code at my side

please set the boudaries for the two fields(quantity label and qantity text filed) as shown bellow

for label of quantity try to set boundaries

lblQty.setBounds(70,220,70,30);

and for textfield of quantity

txtQty.setBounds(150,220,70,30);

i think these 2 might the problem for not displaying the textfield in your code

let me know status as well as if you have any doubts in my clarification

happy coding

Is this GUI generated by a tool? I think this problem is more proof that creating a GUI in this style should not be done by hand.

You should either use a tool to lay out the GUI visually or use a java.awt.LayoutManager to arrange your components. If you don't have a LayoutManager that you like and you don't have a GUI design tool that you like, then you would be better off writing your own LayoutManager rather than placing each component one at a time.

I agree with bguild

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.