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.

* 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.

Recommended Answers

All 6 Replies

I have done that. Can anyone help? Thank you!!

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

this.pack();
this.show();

well, one problem is that you never added your myContainer to the frame. So nothing will show up. Also, I'm not sure about the use of
Container myContainer = getContentPane();

Overwrite method getPreferredSize() in JTextArea:

outputArea = new JTextArea() {
            public Dimension getPreferredSize() {
                return new Dimension(100, 80);
            }
        };

quuba

jackieblock - simple replace

output = new JTextArea(100, 80);

with

output = new JTextArea() {
            public Dimension getPreferredSize() {
                return new Dimension(100, 80);
            }
        };

quuba

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.