简体   繁体   中英

JFrame contents not fully displayed

I am doing a JFrame in which the label inside I am dynamically add in from my database. In this case, sometimes it will be 3 data to be displayed and sometimes it will have 5 data to be displayed.

The program do show the contents, but it shows only the data that fits the screen. That means when I scroll down, nothing can be seen. Anyone know where I get wrong?

public class Trip extends JFrame implements ActionListener {
    ConnectionDB database = new ConnectionDB();

    public Trip() throws SQLException
    {
        super("Trip");
        Container c = getContentPane();
        c.setLayout(new BorderLayout());

        Definition a =new Definition();
        int NoOfAS = a.travelTime(User.UserID);

        Panel p = new Panel(new GridLayout(1,2));
        Panel p1 = new Panel(new GridLayout(NoOfAS,1));

        Label title = new Label("Itinerary");
        title.setFont(new Font("Serif", Font.BOLD, 48));
        c.add(title,"North");

        JScrollPane scroll = new JScrollPane(p);
        c.add(scroll,"Center");


        for(int i=0;i<NoOfAS;i++)
        {

            String ASNAME = database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);

            Label no = new Label("No:"+ (i+1) +" "+ASNAME);
            no.setFont(new Font("Times New Noman",Font.BOLD,20));
            Label descp = new Label(database.retrieveAS_Des(ASNAME,"AS_Description"));
            Label SugTime = new Label("Suggested Time:"+database.retrieveAS_Des(ASNAME,"AS_Time"));
            Label bus = new Label("Bus:"+database.retrieveAS_Des(ASNAME,"AS_Transport"));
            Label fee = new Label("Fees:"+database.retrieveAS_Des(ASNAME,"AS_Price"));
            Label line = new Label("---------------------------------------------------");
            line.setForeground (Color.red);
            Panel p2 = new Panel(new GridLayout(6,1));

            p2.add(no);
            p2.add(descp);
            p2.add(SugTime);
            p2.add(bus);
            p2.add(fee);
            p2.add(line);

            p1.add(p2);
            p.add(p1);
        }     
        // setSize(900,1700);
         pack();
         show();
    }

      public static void main(String args[]) throws SQLException
      {
    StdPlan app = new StdPlan( );
        app.addWindowListener(new WindowAdapter( ){});

      }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

The immediate issue that jumps out at me is you're mixing heavy weight and light weight components ( Label & Panel onto a JScrollPane ) - this is very rarely a good idea.

Use JLabel and JPanel instead.

Inside you for-next-loop you are adding p1 to p repeatedly, this actually achieves nothing, in fact, p probably should be using a BorderLayout seen as it only contains a single component.

I'd also recommend not extending directly from a JFrame , it serverly limits the reusability of you UI components. Better to use a JPanel as the base and add it to a instance of a JFrame (or any other container you might like to use).

Direcltly implementing listener interfaces is generally discouraged, as it tends to expose public methods that you don't normally want people calling. Better to use a inner or anonymous class instead - IMHO

The problem is you are mixing awt (heavyweight) and swing (lightweight) components. Try to use only JLabel , JPanel etc.

Just to try you can check below code and it shows properly while your code shows what is in current view only:

public class Test extends JFrame implements ActionListener {
//ConnectionDB database = new ConnectionDB();

public Test() throws SQLException
{
    super("Trip");
    Container c = getContentPane();
    c.setLayout(new BorderLayout());

   // Definition a =new Definition();
    int NoOfAS = 15;//a.travelTime(User.UserID);

    JPanel p = new JPanel(/*new GridLayout(1,2)*/);
    JPanel p1 = new JPanel(new GridLayout(NoOfAS,1));

    JLabel title = new JLabel("Itinerary");
    title.setFont(new Font("Serif", Font.BOLD, 48));
    c.add(title,BorderLayout.NORTH);

    JScrollPane scroll = new JScrollPane(p);
    c.add(scroll,BorderLayout.CENTER);


    for(int i=0;i<NoOfAS;i++)
    {

        String ASNAME = i + "";//database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);

        JLabel no = new JLabel("No:"+ (i+1) +" "+ASNAME);
        no.setFont(new Font("Times New Noman",Font.BOLD,20));
        JLabel descp = new JLabel(ASNAME);
        JLabel SugTime = new JLabel(ASNAME);
        JLabel bus = new JLabel(ASNAME);
        JLabel fee = new JLabel(ASNAME);
        JLabel line = new JLabel("---------------------------------------------------");
        line.setForeground (Color.red);
        JPanel p2 = new JPanel(new GridLayout(6,1));

        p2.add(no);
        p2.add(descp);
        p2.add(SugTime);
        p2.add(bus);
        p2.add(fee);
        p2.add(line);

        p1.add(p2);
        p.add(p1);
    }     
    // setSize(900,1700);
     pack();
     show();
}

  public static void main(String args[]) throws SQLException
  {
Test app = new Test( );
    app.addWindowListener(new WindowAdapter( ){});

  }

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}

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