| | |
Components don't show in the content panel
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 4
Reputation:
Solved Threads: 0
I am just learning Java. I am trying to do a GUI to calculate road trip data. My program compiles, and a window opens, but only the top title line shows. Nothing shows in the center or south areas. What do I need to change? Here is my file.
I would appreciate any help. I just can't figure out why nothing shows in the center of the frame.
Java Syntax (Toggle Plain Text)
* This class calculates and prints * Total Gallons Used, Total Fuel Cost, * and Travel Time from data entered by user. */ /** text files imported to aid in formatting decimals, * and creating GUI interface */ [ import java.text.DecimalFormat; import java.text.NumberFormat; import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class RoadTripCalculationFrame extends JFrame implements ActionListener { //Variables declared to be visible to all methods JTextArea output; JTextField distanceTf; JTextField mpgTf; JTextField speedTf; JTextField costTf; NumberFormat formatter = new DecimalFormat("#0.00"); public RoadTripCalculationFrame() { } //This constructor used when RoadTripCalculationFrame //object is created from main() method public RoadTripCalculationFrame(String title) { super(title); Container myContainer = getContentPane(); BorderLayout layout = new BorderLayout(); myContainer.setLayout(layout); JLabel titleL = new JLabel ("Road Trip Calculator"); JLabel distanceL = new JLabel("Distance:"); distanceTf = new JTextField(10); JLabel mpgL = new JLabel("MPG:"); mpgTf = new JTextField(10); JLabel speedL = new JLabel("Speed:"); speedTf = new JTextField(10); JLabel costpergallonL = new JLabel("Cost per Gallon:"); costTf = new JTextField(10); JButton calculateB = new JButton("Calculate"); calculateB.addActionListener(this); JButton clearB = new JButton("Clear"); clearB.addActionListener(this); JPanel north = new JPanel(); north.add(titleL); JPanel center = new JPanel(); center.setLayout(new GridLayout(2,5)); center.add(distanceL); center.add(distanceTf); center.add(mpgL); center.add(mpgTf); center.add(speedL); center.add(speedTf); center.add(costpergallonL); center.add(costTf); center.add(calculateB); center.add(clearB); output = new JTextArea(100, 80); myContainer.add(north, BorderLayout.NORTH); myContainer.add(center, BorderLayout.CENTER); myContainer.add(output, BorderLayout.SOUTH); } //This method is called when the buttons are pressed public void actionPerformed(ActionEvent e) { JButton myButton = (JButton)e.getSource(); if (myButton.getText().equals("Calculate")){ double distance = Double.parseDouble(distanceTf.getText()); double mpg = Double.parseDouble(mpgTf.getText()); double speed = Double.parseDouble(speedTf.getText()); double cost = Double.parseDouble(costTf.getText()); double gallonsUsed = distance / mpg; double fuelCost = gallonsUsed * cost; double travel = distance / speed; double hours = travel; double decimalValue = distance % speed; double seconds = (decimalValue / speed) * 3600; output.setText("Total Gallons Used:" + gallonsUsed +", Total Fuel Cost: $" + fuelCost + ", Travel Time:" + hours + "hrs, " + seconds + "seconds"); } else { output.setText(""); distanceTf.setText(""); mpgTf.setText(""); speedTf.setText(""); costTf.setText(""); } } //Entry point (main method) for the program public static void main(String []args){ RoadTripCalculationFrame frame = new RoadTripCalculationFrame("Road Trip Calculation"); frame.setSize(500, 300); frame.setVisible(true); } }
I would appreciate any help. I just can't figure out why nothing shows in the center of the frame.
Last edited by jackieblock; Nov 8th, 2008 at 3:23 pm.
•
•
Join Date: Sep 2008
Posts: 1,629
Reputation:
Solved Threads: 205
1. You need to use code tags. Read the rules stickied at the top of the forum.
2. Read this.
http://java.sun.com/docs/books/tutor...out/index.html
2. Read this.
http://java.sun.com/docs/books/tutor...out/index.html
haven't read the code in detail, and haven't worked on Swing app's for a while, but last time I made some, I sometimes had that problem when not using
Java Syntax (Toggle Plain Text)
this.pack(); this.show();
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Overwrite method getPreferredSize() in JTextArea:
quuba
Java Syntax (Toggle Plain Text)
outputArea = new JTextArea() { public Dimension getPreferredSize() { return new Dimension(100, 80); } };
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
jackieblock - simple replace
with
quuba
Java Syntax (Toggle Plain Text)
output = new JTextArea(100, 80);
Java Syntax (Toggle Plain Text)
output = new JTextArea() { public Dimension getPreferredSize() { return new Dimension(100, 80); } };
![]() |
Similar Threads
- new OS Partition might be too small-- (Windows NT / 2000 / XP)
- assist to show the applet (Java)
- trojans...now nothing opens and I get a paint can't open error (Viruses, Spyware and other Nasties)
- Help! My Computer is Infested with Spyware/Viruses (Viruses, Spyware and other Nasties)
- Hacktool.Rootkit Issue (Viruses, Spyware and other Nasties)
- I lack focus... (Java)
- Aurora Nightmares- please help (Viruses, Spyware and other Nasties)
- PC REALLY slow...especially at startup (Viruses, Spyware and other Nasties)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- hijackthis.log - New User (hope i did it right) (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: help...want to cr8 mobile game "go"
- Next Thread: Slight Problem with arrays.
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






