JFrame内容未完全显示

时间:2012-12-26 01:46:15

标签: java swing jframe awt mixing

我正在做一个JFrame,其中我在我的数据库中动态添加标签。在这种情况下,有时会显示3个数据,有时会显示5个数据。

程序确实显示内容,但它只显示适合屏幕的数据。这意味着当我向下滚动时,看不到任何东西。 谁知道我哪里出错了?

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.");
    }

}

2 个答案:

答案 0 :(得分:3)

我突然想到的问题是你将重量轻且重量轻的组件(LabelPanel混合到一个JScrollPane上 - 这很少是好事想法。

改为使用JLabelJPanel

for-next-loop内部反复向p1添加p,这实际上什么都没有,事实上,p可能应该使用BorderLayout因为它只包含一个组件。

我还建议不要直接从JFrame扩展,它会在服务上限制UI组件的可重用性。最好使用JPanel作为基础并将其添加到JFrame的实例(或您可能想要使用的任何其他容器)。

通常不建议实现监听器接口,因为它往往会暴露您通常不希望人们调用的公共方法。最好使用内部或匿名类 - 恕我直言

答案 1 :(得分:1)

问题是你正在混合awt(重量级)和摇摆(轻量级)组件。请尝试仅使用JLabelJPanel

只是尝试一下,您可以检查下面的代码并在代码显示当前视图中的内容时正确显示:

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.");
}

}