我遇到了Java佈局問題。我試圖通過五個WorldWindowGLCanvas的網格將JLabel放置在最頂端的後面。喜歡的東西:JLabel在重繪其他組件時調整大小
------------------------------------------------
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
+1 rel。示例如何Thread.sleep(int)塊EDT http://stackoverflow.com/a/7944388/714968 – mKorbel 2012-02-03 23:06:54
啊謝謝。那就是訣竅。我仍然習慣於GUI的東西。我也會研究這個線程的背景。感謝您的高舉。 – Brandon 2012-02-03 23:11:18
@Brandon:不客氣。我認爲GridBagLayout的部分問題在於,您要給所有組件提供相同的weightx和weighty常量,因此當GUI擴展時,所有組件都會增加。你使用相同的錨和填充。我自己,我討厭使用GridBagLayout,並避免它,除非必要。 – 2012-02-03 23:18:20