简体   繁体   English

JFrame内容未完全显示

[英]JFrame contents not fully displayed

I am doing a JFrame in which the label inside I am dynamically add in from my database. 我正在做一个JFrame ,其中的标签是从数据库中动态添加的。 In this case, sometimes it will be 3 data to be displayed and sometimes it will have 5 data to be displayed. 在这种情况下,有时将要显示3个数据,有时将要显示5个数据。

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. 突然出现在我眼前的问题是您正在混合重量级和轻量级的组件(将LabelPanelJScrollPane )-这很少是一个好主意。

Use JLabel and JPanel instead. 请改用JLabelJPanel

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. for-next-loop内部for-next-loop您会反复向p添加p1 ,实际上并没有实现任何效果,实际上, p可能应该使用BorderLayout ,因为它只包含一个组件。

I'd also recommend not extending directly from a JFrame , it serverly limits the reusability of you UI components. 我还建议不要直接从JFrame扩展它,它在服务器上限制了UI组件的可重用性。 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). 最好使用JPanel作为基础并将其添加到JFrame的实例(或您可能要使用的任何其他容器)中。

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. 问题是您正在混合awt(重量级)和swing(轻量级)组件。 Try to use only JLabel , JPanel etc. 尝试仅使用JLabelJPanel等。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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