的Java Swing似乎正在改变从卡片布局布局
-
25-09-2019 - |
题
我有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() );
}
在哪里refershReleventOrders()刚刚更新的列表,并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()方法,并打印旧/新布局的类名存在(前或调用超级实施后)。这样,你会看到你的代码的一部分将GroupLayout的面板上。
不隶属于 StackOverflow