0
我有一些与JPanels和CardLayout很奇怪的症状。从本质上讲,我拥有充当“页面”的卡片,因为我只能在网格上放置12个单元格,并将每个页面显示为一张卡片,并单击 - >和相应地更改页面(或者这就是想法)。因为这些数据代表了不断变化的模型,所以我每隔五秒钟就会添加一张显示更新信息的新卡片,然后只删除旧的页面。Java的摆动似乎正在改变布局的布局
从本质上讲,我构建初始页面在程序启动时:
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()铸造代码失败。感谢您的任何想法!
好主意,我发现这个面板的一些布局代码正在改变布局(由另一个开发者编写)。谢谢! – 2010-05-10 07:12:25