1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: JLabel java question.

  1. Mar 31, 2008 #1
    i am trying to display the output in a jlabel. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel:

    Code (Text):

    import javax.swing.*;

    import java.awt.event.*;

    public class shippingCharge extends JFrame implements ActionListener

        //objects and data types created here
        JPanel mainPanel = new JPanel();
        JTextField packageIdentificationTextField = new JTextField(6);
        JTextField poundsTextField = new JTextField(10);
        JTextField ouncesTextField = new JTextField(10);
        JButton displayButton = new JButton("Calculate  ");
        String packageIdentificationString;
        double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;

        public static void main(String[] args)
            shippingCharge shippingTotal = new shippingCharge();
        public shippingCharge()
        public void designFrame()
            mainPanel.add(new JLabel("Package ID"));
            mainPanel.add(new JLabel("Pounds"));
            mainPanel.add(new JLabel("Ounces"));
            //add listener to the  object
        public void getInput()
            packageIdentificationString = packageIdentificationTextField.getText();
            poundDouble = Double.parseDouble(poundsTextField.getText());
            ouncesDouble = Double.parseDouble(ouncesTextField.getText());
        public void calculateShipping()
            final double SHIPPING_RATE = .12;
            final double OUNCES_PER_POUND = 16;
            poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
            shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;

        public void actionPerformed(ActionEvent evt)
        public void displayOutput()
        mainPanel.add(new JLabel("Package ID:" + packageIdentificationString));
            mainPanel.add(new JLabel("Weight:" + poundDouble + "lbs" + ouncesDouble + "oz."));
            mainPanel.add(new JLabel("Shipping Cost:" + shippingCostDouble));
    here is the code but the output labels aren't showing up. what gives? does it have something to do with positioning?
    Last edited: Mar 31, 2008
  2. jcsd
  3. Apr 9, 2008 #2
    Have you tried NetBeans?? You can graphically make the applet/gui of the program you are writing, it is a lot easier than just writing code. All you have to do is make it and add functionality to the buttons.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook