![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 1
Rep Power: 0
![]() |
JLabel question
i am trying to display the output in jlabels for this shipping cost program i have coded. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel. I had no problems making labels for the input in my designFrame method, but when I try to make new labels for the mainPanel in displayoutput they simply do not display. here is the 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));
}
} |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
Re: JLabel question
Haha. Now you have me invested in your problem. I've worked on more complicated GUIs but none that involved adding components to a window that was already open. There's probably an easy solution. If you were using a better Layout, rather than the default, that would be good too.
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
Re: JLabel question
Call validate() after adding the components to the panel.
The sizes of components on different systems will mess up your layout, as will resizing the window. Look into other layout managers and/or consider grouping elements in multiple panels. You should also think about altering the visibility components in question (via setVisible()) rather than adding/removing them from the panel. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
Re: JLabel question
I suggest, in your case, using BorderLayout, and making two panels. Then add your components to whatever sector you want.
H/o Last edited by Fall Back Son; Apr 3rd, 2008 at 2:10 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question regarding data input into PHP/Mysql using something other than GET | davil | PHP | 6 | Nov 20th, 2007 8:06 AM |
| C++ template and namespace question | Soulstorm | C++ | 3 | Jan 22nd, 2006 2:46 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |