繁体   English   中英

Java的Swing似乎正在从Card Layout更改布局

[英]Java's Swing seems to be changing the layout from Card Layout

我在使用JPanels和CardLayout时出现了一些非常奇怪的症状。 本质上,我有一些卡可以充当“页面”,因为我只能在网格上容纳12个单元格,并且将每个页面显示为卡,然后单击->和<-按钮相应地更改页面(或者就是这个想法)。 因为此数据代表着一个不断变化的模型,所以我每隔五秒钟就要添加一张新卡,以显示更新的信息,并只需删除旧页面即可。

本质上,我在程序启动时构造初始页面:

public OrderSimPanel() {
    super( new CardLayout() );

    // Create the map
    frames = new TreeMap<Integer, String>();

    // Update and draw
    refreshRelevantOrders();
    drawPanel();

    // Set a timer for future updating
    java.util.Timer timer = new java.util.Timer();
    timer.schedule(new UpdateTask(), 5000);

    System.out.println("Constructor DOES get called");
    System.out.println("Panel type is " + this.getLayout().getClass().getName() );
}

其中ReferehReleventOrders()仅更新一个List,而drawPanel()如下所示:

private void drawPanel() {
    System.out.println("Panel type is " + this.getLayout().getClass().getName() );

    // Set all old frames to deprecated
    for( Integer k : frames.keySet() ) {
         String o = frames.get( k );
         frames.remove(k);
         k = -k;
         frames.put(k, o);
    }

    // Frame to add to
    JPanel frame = new JPanel( new GridLayout(3,4) );
    int f = 0;

    // Create new cells in groups of 12
    for( int i = 0; i < orders.size(); i++ ) {
        // Pagination powers activate!
        int c = i % 12;
        f = i / 12;

        // Create a new panel if we've run out of room on this one
        if ( c == 0 && i > 0 ) {
            // Add old frame
            String id = new Double( Math.random() ).toString();
            frames.put(f, id);
            add( frame, id );

            // Create new one
            frame = new JPanel( new GridLayout(3,4) );
        }

        // Add the order cell to the panel
        frame.add(new OrderCellPanel( orders.get(i) ));

        i++;
    }

    // Add last frame
    String id = new Double( Math.random() ).toString();
    frames.put(f, id);
    add( frame, id );

    // Set active frame to 0'th frame
    ((CardLayout)(this.getLayout())).show(this, frames.get(0));

    // Erase other frames and from map
    for( Integer k : frames.keySet() ) {
        if( k < 0 ) {
            this.remove(0);
            frames.remove(k);
        }
    }
}

在构造函数中创建的计时器任务将refreshRelevantOrders()称为drawPanel()。 初始打印输出如下所示:

Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout

但是,当计时器执行时,将显示以下内容:

Panel type is javax.swing.GroupLayout

当然,drawPanel()中的转换代码也会失败。 感谢您的任何想法!

覆盖setLayout()方法,并在那里(在调用super实现之前或之后)打印旧的/新的布局类名称。 这样,您将在面板上看到代码的哪一部分设置了GroupLayout。

暂无
暂无

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

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