0

我正在做一个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.");
    }

}
4

2 回答 2

3

我最直接的问题是你混合了重量级和重量级的组件(Label&Panel到 a 上JScrollPane)——这很少是一个好主意。

使用JLabelandJPanel代替。

在你里面你反复for-next-loop添加p1p这实际上什么也没做,事实上,p可能应该使用 a BorderLayoutseen 因为它只包含一个组件。

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

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

于 2012-12-26T04:06:38.977 回答
1

问题是您正在混合 awt(重量级)和 swing(轻量级)组件。尝试使用 onlyJLabelJPanel

只是为了尝试,您可以检查以下代码,它会正确显示,而您的代码仅显示当前视图中的内容:

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

}
于 2012-12-26T04:15:14.173 回答