JPanel에서 사용할 사용자 정의 LayoutManager를 만들어야합니다.) (
http://download.oracle.com/javase/tutorial/uiswing/layout/custom.htmlJPanel은 사용자 정의 LayoutManager의 addLayoutComponent()를 호출하지 않습니다.
(그것은 layoutContainer의 전화 않습니다 나는 JPanel의에 구성 요소를 추가 할 때
그러나 JPanel에 그것을해야하더라도, 내 사용자 지정의 LayoutManager의 addLayoutComponent() 메소드를 호출하지 않습니다 , 예상대로)
바라건대, 누군가 내가 잘못하고있는 것을 말해 줄 수 있기를 바랍니다.
JPanel에서 addLayoutComponent()를 호출하는 방법은 무엇입니까?
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
createAndShowGUI();
}
private static void createAndShowGUI()
{
JButton button = new JButton("Test");
button.setBounds(64, 64, 128, 64);
JPanel panel = new JPanel(new CustomLayoutManager());
//FIXME: Missing call to CustomLayoutManager.addLayoutComponent()
panel.add(button);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.toFront();
}
public static class CustomLayoutManager implements LayoutManager
{
public void addLayoutComponent(String name, Component comp)
{
System.out.println("addLayoutComponent");
}
public void layoutContainer(Container parent)
{
System.out.println("layoutContainer");
}
public Dimension minimumLayoutSize(Container parent)
{
System.out.println("minimumLayoutSize");
return new Dimension();
}
public Dimension preferredLayoutSize(Container parent)
{
System.out.println("preferredLayoutSize");
return new Dimension();
}
public void removeLayoutComponent(Component comp)
{
System.out.println("removeLayoutComponent");
}
}
}
간결한 답변에 많은 감사드립니다. LayoutManager2를 구현하는 것이 유일한 "안정적인"방법 인 것처럼 보입니다. Container.add (Component 구성 요소, Object 제약 조건)를 사용하면 필요에 따라 LayoutManager2.addLayoutComponent (구성 요소 구성 요소, 객체 제약 조건) 메서드가 호출됩니다. – Meyer
질문 거기 제약과 putProperty 사이에 몇 가지 차이점은 동일하지 않습니다 (나는 DocumentListener에 사용), 아마도 내 질문에 대한 지식 (에 대한 knowledges의 부족 ...) +1 – mKorbel
@ mKorbel, 제약 조건이 사용되는 개체입니다 레이아웃 관리자가 구성 요소의 레이아웃을 제어하는 데 도움을줍니다. 제약 조건을 어떻게 사용할 수 있는지에 대한 예제는 GridBagLayout 및 GridBagConstraints를 참조하십시오. putProperty()가 DocumentListener 또는 LayoutManager에서 사용되는 곳인지 잘 모르겠습니다. 그래서 당신이 무엇을 요구하는지 잘 모르겠습니다. – camickr