我們的應用程序顯示我們數據的2D視圖(主要是地圖),然後允許用戶切換到3D視圖。二維和三維視圖由定製的C++代碼生成,該代碼被SWIG加入到我們的Swing GUI中幷包裝在JComponent
中。這些JComponent
然後顯示在另一個父JComponent中。如何在調用`setVisible(true)`後調用JComponent?
我們的問題是,當我們從2D切換到3D視圖,然後回到2D視圖時,當我們調整窗口大小時,2D視圖不會調整大小。調整大小事件不會發送到2D視圖。
我們的應用程序在Linux(Fedora 11)下運行。我們正在運行Java版本1.6.0_12。
下面是一些示例代碼,我用兩個2 JButton
代替了2D視圖和3D視圖,它們產生相同的行爲。一旦進入3D然後回到2D,調整窗口大小不會導致調整2D視圖的大小。
/* TestFrame.java
* Compile with: $ javac TestFrame.java
* Run with: $ java TestFrame
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JButton;
public class TestFrame extends javax.swing.JFrame {
private boolean mode2D = true;
private JButton view2D = null;
private JButton view3D = null;
private Container parent = null;
public TestFrame() {
initComponents();
containerPanel.setLayout(new BorderLayout());
view2D = new JButton("2D View");
view2D.addComponentListener(new MyListener("2D VIEW"));
containerPanel.add(view2D);
}
private void changerButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (parent == null) {
parent = view2D.getParent();
}
if (mode2D) {
System.out.println("Going from 2D to 3D");
view2D.setVisible(false);
if (view3D != null) {
view3D.setVisible(true);
} else {
view3D = new JButton("3D View");
view3D.addComponentListener(new MyListener("3D VIEW"));
parent.add(view3D);
}
((JButton) evt.getSource()).setText("Change to 2D");
mode2D = false;
} else {
System.out.println("Going from 3D to 2D");
view3D.setVisible(false);
view2D.setVisible(true);
((JButton) evt.getSource()).setText("Change to 3D");
mode2D = true;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
private javax.swing.JPanel containerPanel;
private javax.swing.JButton changerButton;
private class MyListener implements ComponentListener {
private String name;
public MyListener(String name) {
this.name = name;
}
@Override
public void componentHidden(ComponentEvent event) {
System.out.println("@@@ [" + name + "] component Hidden");
}
@Override
public void componentResized(ComponentEvent event) {
System.out.println("@@@ [" + name + "] component Resized");
}
@Override
public void componentShown(ComponentEvent event) {
System.out.println("@@@ [" + name + "] component Shown");
}
@Override
public void componentMoved(ComponentEvent event) {
System.out.println("@@@ [" + name + "] component Moved");
}
};
@SuppressWarnings("unchecked")
private void initComponents() {
containerPanel = new javax.swing.JPanel();
changerButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
containerPanel.setBorder(new javax.swing.border.MatteBorder(null));
javax.swing.GroupLayout containerPanelLayout = new javax.swing.GroupLayout(containerPanel);
containerPanel.setLayout(containerPanelLayout);
containerPanelLayout.setHorizontalGroup(
containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 374, Short.MAX_VALUE)
);
containerPanelLayout.setVerticalGroup(
containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 239, Short.MAX_VALUE)
);
changerButton.setText("Change to 3D");
changerButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
changerButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(containerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(changerButton))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(containerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(changerButton)
.addContainerGap())
);
pack();
}
}
(我的Netbeans的道歉生成的GUI代碼)
我應該指出,當我們調用parent.remove(view2D)
和parent.add(view3D)
改變看法的我們的3D視圖變化的X Windows ID和我們無法取回我們的3D視圖。因此parent.remove(view2D)
和parent.add(view3D)
不是真正的解決方案,我們必須在JComponent
上調用setVisible(false)
和setVisible(true)
,其中包含我們的2D和3D視圖以隱藏和顯示它們。
任何幫助將不勝感激。
這聽起來過於複雜,它應該是足以只是從/到'父母'中刪除和添加組件。或者使用其他回覆中建議的CardLayoutManager。 – fish 2010-04-22 13:46:12