2

大学から割り当てられたプロジェクトを完了しましたが、現在、プロジェクトのMDIを作成しようとしています。10個のjFrameとjFrameでもある1つのメインフォームを使用し、その後、1つのメニューバー、jFrameを呼び出すための10個のjButton、およびjFrameを呼び出す場所用の1つのjDesktopPaneを追加しました。以下のコードは、10個すべてのjButtonでjFrameplaceをjDesktopPaneに呼び出すために使用します。

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try
      {
        asd t = new asd();
        dskp.add(t);
        t.setVisible(true);

      }
      catch(Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
    } 

しかし、私と一緒に働いておらず、以下のエラーメッセージを表示しています:

java.lang.illegalargumentexception: adding a window to a container

jInternal Frameを使用しなかったため、これを実行してこの問題を解決する方法。コード付きの完全なGUIなどのjFrameですべての作業を行い、jInternal Frameですべての作業をやり直すことは、最終的なプロジェクトを提出するのに時間がかからないため、これではjInternaleFrameを使用できないと思います。

4

1 に答える 1

5

If you're desiring to placing windows intp a JDesktopPane, then you need to use JInternalFrames. This is your best solution whether it is appealing to you or not.

A lesson in this is that you should strive to avoid creating classes that extend Swing components, especially top-level components such as JFrames, and instead create classes that produce JPanels, components that are flexible enough to be placed anywhere such as into JFrames, JInternalFrames, JDialogs, JOptionPanes, other JPanels, etc...

Note that a kludge is to get the contentPane from your JFrame, put it into a JInternalFrame and put that into the JDesktopPane, either that or set the JInternalPanes's contentPane with that from the JFrame. i.e.,

asd t = new asd();
JInternalFrame internalFrame = new JInternalFrame();
internalFrame.setContentPane(t.getContentPane());
internalFrame.pack();

// set the internalFrame's location
// ...

internalFrame.setVisible(true);
dskp.add(internalFrame);

But again note that this is a kludge and carries potential traps.

于 2012-12-06T19:13:24.933 に答える