Solve Java JLabel Problem: Display Output on mainPanel

  • Comp Sci
  • Thread starter clook
  • Start date
  • Tags
    Java
In summary, the conversation is about a person trying to display an output in a JLabel using Java. They have created individual JLabels for the package ID, weight, and shipping cost, and have a mainPanel to display them. They have also added a button for calculating the shipping cost. The code includes methods for getting input, calculating the shipping cost, and displaying the output. The person is having trouble with the output labels not showing up and wonders if it has something to do with positioning or if using NetBeans would be easier.
  • #1
clook
35
0
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:
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  ");
	
	
	//Variables
	String packageIdentificationString;
	double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;	public static void main(String[] args)
	{
		shippingCharge shippingTotal = new shippingCharge();
	}
		
	public shippingCharge()
	{
		designFrame();
		setSize(500,500);
		setVisible(true);
		
	}
	
	public void designFrame()
	{
		mainPanel.add(new JLabel("Package ID"));
		mainPanel.add(packageIdentificationTextField);
		mainPanel.add(new JLabel("Pounds"));
		mainPanel.add(poundsTextField);
		mainPanel.add(new JLabel("Ounces"));
		mainPanel.add(ouncesTextField);
		mainPanel.add(displayButton);
		
		
        add(mainPanel);
		//add listener to the  object
        packageIdentificationTextField.addActionListener(this);
		displayButton.addActionListener(this);
		
	}
	
	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)
	{
		getInput();
		calculateShipping();
		displayOutput();
		
		
	}
	
	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:
Physics news on Phys.org
  • #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.
 
  • #3


It is likely that the output labels are not showing up because they have not been added to the mainPanel. In the displayOutput() method, you are creating JLabels but not adding them to the mainPanel. You can add them by using the add() method on the mainPanel, just like you did in the designFrame() method. For example:

mainPanel.add(new JLabel("Package ID:" + packageIdentificationString));

Also, make sure to call the revalidate() and repaint() methods on the mainPanel after adding the labels to ensure they are displayed on the GUI. Additionally, you may need to set the layout of the mainPanel to a layout that allows for multiple components to be added, such as a GridLayout or BorderLayout. I hope this helps solve the issue.
 

1) What is a Java JLabel?

A Java JLabel is a GUI component that displays a single line of read-only text or an image. It is commonly used to label other components in a graphical user interface.

2) How can I solve the Java JLabel problem of displaying output on mainPanel?

To solve this problem, you can first create a JLabel with the desired text or image. Then, add the JLabel to the mainPanel using the add() method. Finally, make sure to call the revalidate() and repaint() methods on the mainPanel to update the display.

3) How do I set the position of a JLabel on a mainPanel?

The position of a JLabel on a mainPanel can be set using layout managers. You can use methods such as setLocation() and setBounds() to specify the position and size of the JLabel on the mainPanel.

4) Can a Java JLabel be updated dynamically?

Yes, a Java JLabel can be updated dynamically by using the setText() or setIcon() methods. These methods allow you to change the text or image displayed on the JLabel at runtime.

5) How can I change the font or color of a JLabel?

To change the font or color of a JLabel, you can use the setFont() and setForeground() methods. These methods allow you to specify the desired font and color for the JLabel.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Computing and Technology
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top