非常に簡単なプログラムを作成しています。 私はこのクラスを作成しました: MainJframeClass、JDesktopPaneClass、JinternalFrameClass1およびJinternalFrameClass2。 私はjdesktoppaneclassをインスタンス化してdesktoppane1という名前を付け、MainJframeclassに追加しました。私はまた、2つのjinternalframesをインスタンス化し、internal1とinternal2という名前を付けました。今、mainjframeclassのボタンがあります。私が押すと、internal1をdesktoppane1に追加します。今私の問題はinternal1のどこかに配置されたボタンを使ってdesktoppane1にinternal2を追加する方法です。なぜ私はdesktoppane1に別のボタンを追加してinternal2を追加できるのか知っています。しかし、私はすでにそれをやっている、私はちょうどこの問題を解決したい。あなたが私を助けることができるならば。途中で私の英語のために申し訳ありません。他のjinternalframeクラスを使用してjdesktoppaneにjinternalframeクラスを追加する
2
A
答えて
3
これは単なる参考事項です。 JDesktopPaneに何かを追加するコードは、その参照を持っている必要があります。そのため、その参照を、コンストラクタパラメータまたはメソッドパラメータのどちらかを介して必要とするクラスに渡す必要があります。
編集例えば1
:internal1のどこかに置いボタンを使用してdesktoppane1するinternal2を追加する方法を
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class ReferenceExample extends JPanel {
private JDesktopPane desktop = new JDesktopPane();
private Random random = new Random();
public ReferenceExample() {
JButton addInternalFrameBtn = new JButton("Add Internal Frame");
addInternalFrameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addInternalFrame();
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(addInternalFrameBtn);
setPreferredSize(new Dimension(600, 450));
setLayout(new BorderLayout());
add(new JScrollPane(desktop), BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
public void addInternalFrame() {
MyInternalFrame intFrame = new MyInternalFrame(ReferenceExample.this);
int x = random.nextInt(getWidth() - intFrame.getPreferredSize().width);
int y = random.nextInt(getHeight() - intFrame.getPreferredSize().height);
intFrame.setLocation(x, y);
desktop.add(intFrame);
intFrame.setVisible(true);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Reference Eg");
frame.getContentPane().add(new ReferenceExample());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyInternalFrame extends JInternalFrame {
// pass in the reference in the constructor
public MyInternalFrame(final ReferenceExample refEg) {
setPreferredSize(new Dimension(200, 200));
setClosable(true);
JButton addInternalFrameBtn = new JButton("Add Internal Frame");
addInternalFrameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// use the reference here
refEg.addInternalFrame();
}
});
JPanel panel = new JPanel();
panel.add(addInternalFrameBtn);
getContentPane().add(panel);
pack();
}
}
1
は、デスクトップウィンドウへの参照を取得するには、次のようなコードを使用することができ、あなたのボタンに追加しました。
:ActionListenerのでContainer container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource()); if (container != null) { JDesktopPane desktop = (JDesktopPane)container; JInternalFrame frame = new JInternalFrame(...); desktop.add(frame); }
関連する問題
- 1. JInternalFrameでJLabelを追加するには?
- 2. JFrameにJInternalFrameを追加する方法
- 3. JdesktopPaneの中央にJinternalFrameを設定する方法は?
- 4. 起動時にJDesktopPane内のJInternalFrameを開く方法
- 5. JDesktopPaneおよびJInternalFrameが役立ちます。
- 6. jInternalFrameを使用する際の欠点
- 7. JInternalFrameカスタムオンスクリーンキーボード
- 8. Java JInternalFrame to JPanel
- 9. モーダルJInternalFrameを閉じる
- 10. JPanelからJInternalFrameを隠す
- 11. JInternalFrameのサイズを設定するには
- 12. JInternalFrameがオーバーラップしているJInternalFrameを再描画しないようにする方法
- 13. JInternalFrame to front and focussed
- 14. Eclipse SWT Shell to JInternalFrame
- 15. クラスにクラスを追加してsetAttributeをIDに使用する
- 16. JDesktopPaneの中央にあるJInternalFrameを開くにはどうすればよいですか?
- 17. JInternalFrameのフォーカスを維持する
- 18. JFreechartのJInternalFrameでの例外
- 19. jQuery他のクラスがある場合にクラスにクラスを追加
- 20. JInternalFrame再描画の問題
- 21. JInternalFrameのサイズチェックを強制的に行う
- 22. JavaでJInternalFrameを最大化する
- 23. Java Swingで[Esc]キーを使用してJInternalFrameを最小化する方法は?
- 24. MacOSでJInternalFrameを宣言しない
- 25. Javascriptのボタンに他のクラスを追加するクラス
- 26. Swing:アクティブになる方法JInternalFrame
- 27. 他のクラス/メソッドを使用するクラス
- 28. wordpressのクラスを使用してイメージを追加するには?
- 29. 他のクラスから他のクラスのJPanelをcardLayoutに追加する
- 30. エスケープキーを押してJInternalFrameを閉じる方法は?