JLabel 在重绘其他组件时调整大小
我的 Java 布局有问题。我试图在最顶部放置一个 JLabel,然后放置五个 WorldWindowGLCanvas 的网格。类似于:
------------------------------------------------
JLabel
------------------------------------------------
|
WWCanvas | WWCanvas
|
------------------------------------------------
|
WWCanvas | WWCanvas
|
我目前正在 JPanel 内使用 GridBagLayout。当程序第一次启动时,一切看起来都正常。然而,在第一次重绘所有 WorldWindowGLCanvas 的顶部 JLabel 时,其大小立即加倍并占用太多空间。以下是一些相关代码:
JFrame f = new JFrame("ScintDisplay");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
JLabel timeLabel = new JLabel(getTime(), JLabel.CENTER);
timeLabel.setSize(new Dimension(200,200));
WorldWindowGLCanvas wwd1 = new WorldWindowGLCanvas();
wwd1.setPreferredSize(new java.awt.Dimension(960, 1000));
WorldWindowGLCanvas wwd2 = new WorldWindowGLCanvas();
wwd2.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd3 = new WorldWindowGLCanvas();
wwd3.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd4 = new WorldWindowGLCanvas();
wwd4.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd5 = new WorldWindowGLCanvas();
wwd5.setPreferredSize(new java.awt.Dimension(500, 480));
wwd1.setModel(new BasicModel());
wwd2.setModel(new BasicModel());
wwd3.setModel(new BasicModel());
wwd4.setModel(new BasicModel());
wwd5.setModel(new BasicModel());
addComponent(f, wwd2, 0, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd1, 1, 2, 2, 8, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd3, 3, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd4, 0, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd5, 3, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, timeLabel, 0, 0, 4, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
f.setSize(1920, 1200);
f.setVisible(true);
double lat1 = getTimeLon();
for( ; ; )
{
//System.out.println(lat);
timeLabel.setText(getTime());
System.out.println(timeLabel.getSize());
wwd1.getView().setEyePosition(Position.fromDegrees(0, lat1, 10500000));
wwd2.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd3.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd4.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd5.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd1.redraw();
wwd2.redraw();
wwd3.redraw();
wwd4.redraw();
wwd5.redraw();
Thread.sleep(30000);
lat1 = getTimeLon();
}
private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
我以前从未使用过重绘的 GL 组件做过 GUI。我错过了什么吗?这是布局问题吗?或者与重画有关?
谢谢, -B
I'm having a problem with my Java layout. I'm trying to place a JLabel across the very top follow by a grid of five WorldWindowGLCanvas's. Something like:
------------------------------------------------
JLabel
------------------------------------------------
|
WWCanvas | WWCanvas
|
------------------------------------------------
|
WWCanvas | WWCanvas
|
I'm currently using a GridBagLayout inside of a JPanel. When the program first starts everything looks as it should. However, upon the first redraw of all the WorldWindowGLCanvas's the top JLabel immediately doubles in size and takes up was too much space. Here's some relevant code:
JFrame f = new JFrame("ScintDisplay");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
JLabel timeLabel = new JLabel(getTime(), JLabel.CENTER);
timeLabel.setSize(new Dimension(200,200));
WorldWindowGLCanvas wwd1 = new WorldWindowGLCanvas();
wwd1.setPreferredSize(new java.awt.Dimension(960, 1000));
WorldWindowGLCanvas wwd2 = new WorldWindowGLCanvas();
wwd2.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd3 = new WorldWindowGLCanvas();
wwd3.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd4 = new WorldWindowGLCanvas();
wwd4.setPreferredSize(new java.awt.Dimension(500, 480));
WorldWindowGLCanvas wwd5 = new WorldWindowGLCanvas();
wwd5.setPreferredSize(new java.awt.Dimension(500, 480));
wwd1.setModel(new BasicModel());
wwd2.setModel(new BasicModel());
wwd3.setModel(new BasicModel());
wwd4.setModel(new BasicModel());
wwd5.setModel(new BasicModel());
addComponent(f, wwd2, 0, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd1, 1, 2, 2, 8, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd3, 3, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd4, 0, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, wwd5, 3, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(f, timeLabel, 0, 0, 4, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
f.setSize(1920, 1200);
f.setVisible(true);
double lat1 = getTimeLon();
for( ; ; )
{
//System.out.println(lat);
timeLabel.setText(getTime());
System.out.println(timeLabel.getSize());
wwd1.getView().setEyePosition(Position.fromDegrees(0, lat1, 10500000));
wwd2.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd3.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd4.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd5.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
wwd1.redraw();
wwd2.redraw();
wwd3.redraw();
wwd4.redraw();
wwd5.redraw();
Thread.sleep(30000);
lat1 = getTimeLon();
}
private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
I've never done a GUI with GL components that redraw before. Am I missing something? Is this a layout problem. or something to do with redrawing?
Thanks,
-B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图要求 GridBagLayout 做太多事情,而应该使用嵌套的 JPanel 来实现此目的。当然,让中心 JPanel 使用 GridBagLayout,但让它由另一个使用 BorderLayout 的 JPanel 保存。将中心面板添加到 BorderLayout.CENTER 位置,并将 JLabel 添加到主 BorderLayout - 使用 JPanel 的 BorderLayout.NORTH 或 BorderLayout.PAGE_START 位置。还要确保 JLabel 已设置为显示其文本居中。
另外,从 Swing 线程的角度来看,您的代码看起来很危险。该不间断循环和 Thread.sleep(...) 似乎是在 Swing 事件线程或 EDT 上调用的,这可能会导致您的应用程序冻结。考虑使用 SwingWorker 来允许在后台线程上调用此代码。
You're trying to ask GridBagLayout to do too much and instead should use nested JPanels to achieve this. Have the center JPanel use GridBagLayout, sure, but have it held by another JPanel that uses BorderLayout. Add the center panel to the BorderLayout.CENTER position, and the JLabel to the main BorderLayout-using JPanel's BorderLayout.NORTH or BorderLayout.PAGE_START position. Also be sure that the JLabel has been set to display it's text centered.
Also, you've got code there that looks dangerous from a Swing threading point of view. That non-stopping loop and
Thread.sleep(...)
appear to be called on the Swing event thread or EDT, and this can cause your application to freeze. Consider using a SwingWorker to allow this code to be called on a background thread.