简体   繁体   中英

Java Swing improperly showing up

I am having a problem...I am trying to put components on a panel...and they arent showing up the way I would expect them too.

here is an example...

here is the code:

package UserInterface;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
/**
 *
 * @author 
 */
public class FlightPrice extends JPanel {
    private JPanel buttonPane;
    private JPanel dataPane;
    private JLabel outbound;
    private JLabel outDate;
    private JTable departFlights;
    private JLabel inbound;
    private JLabel inDate;
    private JTable returnFlights;
    private JLabel flightCost;
    private JTable priceTable;
    private JButton purchase;
    private JButton goBack;
    private JButton cancel;

     public FlightPrice()
    {
        initComponents();
    }

    private void initComponents()
    {
        BorderLayout borderLayout = new BorderLayout();
        //iniyliazing the componenets\\
        Object[][] data1 =  {{"239", "United", "8:00 AM MCO", "9:05 AM ATL", "767", "25F"}};    
        String[]  name = {"", "",}; 
        Object [][] data2 = {{"612", "United", "4:35 PM ATL", "5:45 PM MCO", "A320", "19A"}};
        String [] name2 = {"",""};
        Object [][] data3 = {{"Adult", "1", "546.00", "56.00", "602.00"}};
        String [] name3 = {"",""};

        buttonPane = new JPanel();
        dataPane = new JPanel(new GridBagLayout());
        outbound = new JLabel("Depart: Orlando TO Atlanta");
        outDate = new JLabel("Date: March 1, 2014");
        departFlights = new JTable(data1, name);
        inbound = new JLabel("Return: Atlanta TO Orlando");
        inDate = new JLabel("Date: March 19, 2014");
        returnFlights = new JTable(data2, name2);
        flightCost = new JLabel("Air Fare Cost");
        priceTable = new JTable(data3, name3);
        purchase = new JButton("Purchase");
        goBack = new JButton("goBack");
        cancel = new JButton("Cancel");

        //adds componetns to the panel\\

        GridBagConstraints c = new GridBagConstraints();
        //adding info into the tables\\
        c.fill = GridBagConstraints.HORIZONTAL;

        c.gridx = 0;
        c.gridx = 0;
        dataPane.add(outbound, c);
        c.gridx = 0;
        c.gridy = 1;
        dataPane.add(outDate, c);
        c.gridx = 0;
        c.gridy = 2;
        dataPane.add(departFlights,c);
        c.gridx = 0;
        c.gridy = 3;
        dataPane.add(inbound, c);
        c.gridx = 0;
        c.gridy = 4;
        dataPane.add(inDate, c);
        c.gridx = 0;
        c.gridy = 5;
        dataPane.add(returnFlights, c);
        c.gridx = 0;
        c.gridy = 6;
        dataPane.add(flightCost, c);
        c.gridx = 0;
        c.gridy = 7;
        dataPane.add(flightCost, c);

        // adding components to buttonPane\\
        buttonPane.add(goBack);
        buttonPane.add(purchase);
        buttonPane.add(cancel);


        //adding panels to the JFrame\\
        add(dataPane);
        add(buttonPane, BorderLayout.SOUTH);

}
}

this is a class that displays extends JPanel. It hold all the components added to the dataPane. The dataPane is then added to the main pane. help?

http://imageshack.com/a/img198/8795/rqnj.png

what it is suppose to look like:

http://imageshack.com/a/img585/7088/83j9.png

You say BorderLayout borderLayout = new BorderLayout(); but you never pass that layout to a panel using something like this.setLayout(borderLayout) .

In addition, when you add(dataPane) , I recommend that you should specify where, since you intend to use a BorderLayout. Even though CENTER is the default, I recommend saying, add(dataPane, BorderLayout.CENTER) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM