아주 간단한 프로그램을 만들고 있습니다. 이 클래스는 MainJframeClass, JDesktopPaneClass, JinternalFrameClass1 및 JinternalFrameClass2 클래스로 만들었습니다. 필자는 jdesktoppaneclass를 인스턴스화하고 desktoppane1로 이름을 지정하고 MainJframeclass에 추가했습니다. 또한 2 개의 jinternalframes를 인스턴스화하고 internal1 및 internal2라는 이름을 지정했습니다. 자, 메인 프레임에 버튼이 있습니다. 제가 누르면, 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. JDesktopPane 및 JInternalFrame 도움!
- 2. JInternalFrame 및 Keybinding
- 3. 모달 닫기 JInternalFrame
- 4. 모든 JInternalFrame 이벤트 듣기 - Java
- 5. JInternalFrame 현재 위치를 유지하면서 최소화합니다.
- 6. Java : getInputMap (WHEN_IN_FOCUSED_WINDOW) from JInternalFrame
- 7. JInternalFrame to front and focussed
- 8. JInternalFrame 및 일부 버튼 사용
- 9. Java Swing : JInternalFrame : 대화 상자 팝업이 필요합니다.
- 10. RichFaces jquery를 사용하여 탭에 다른 클래스 추가하기
- 11. Html.TextBox를 사용하여 CSS 클래스 추가하기
- 12. quirksmode에서 요소에 클래스 추가하기
- 13. TableAdapter에 부분 클래스 추가하기
- 14. XDV를 사용하여 본문 요소에 클래스 추가하기
- 15. CodeIgniter에서 form_dropdown에 클래스 추가하기
- 16. C++ 템플릿 클래스를 목록에 추가하기
- 17. CATALINA_OPTS를 사용하여 Tomcat 클래스 패스에 추가하기
- 18. 콘텐츠 페이지에 CSS 클래스 추가하기
- 19. Xcode에 클래스 추가하기
- 20. Java 클래스 추가하기
- 21. 다른 일반 클래스를 사용하는 일반 클래스
- 22. 다른 클래스 내에서만 클래스를 생성하도록 제한하려면 어떻게합니까?
- 23. 다른 클래스를 확장 할 수있는 클래스 제한
- 24. 모든 테이블 헤더에 클래스 추가하기
- 25. android UI - 새로운 클래스 추가하기
- 26. 싱글 톤 클래스를 사용하여 ActiveRecord 연결을 동적으로 덮어 쓰기/추가하기
- 27. Ext.Button 클래스를 Box 구성 요소에 추가하기
- 28. viewController보기를 다른 viewController에 추가하기
- 29. Jquery를 사용하여 요소의 다른 클래스 가져 오기
- 30. 다른 클래스의 UIView에 물건 추가하기