Java的Swing似乎正在改变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() );
}
其中refershRelevantOrders() 只是更新一个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() 中的转换代码失败。感谢您的任何想法!
I am having some pretty strange symptoms with JPanels and CardLayout. Essentially, I have cards which act as "pages" since I can only hold 12 cells on the grid, and I display each page as a card, and clicking -> and <- buttons change pages accordingly (or that's the idea). Because this data is representing a changing model, every five seconds I was going to have new cards be added displaying updated information, and simply remove the old pages.
Essentially, I construct the initial pages when the program starts:
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() );
}
Where refershReleventOrders() just updates an List, and drawPanel() is something like this:
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);
}
}
}
The timer task created in the constructor calls refreshRelevantOrders() an drawPanel(). The initial print output looks like this:
Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout
But when the timer executes, this is displayed:
Panel type is javax.swing.GroupLayout
And of course, the casting code in drawPanel() fails. Thanks for any ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重写 setLayout() 方法,并在那里打印旧的/新的布局类名称(在调用超级实现之前或之后)。这样您就可以看到代码的哪一部分在面板上设置了 GroupLayout。
Override setLayout() method, and print the old / new layout class names there (before or after calling the super implementation). This way you will see which part of your code sets GroupLayout on the panel.