문제

그것들을위한 질문 친숙한 ~와 함께 Miglayout

죄송합니다.이 질문에 더 적절한 이름을 생각할 수 없었습니다 ...

다음과 같이 보일 레이아웃을 만들려고합니다.

+---------+---------+
|  btn1   |  btn2   |
+---------+---------+
|                   |
|       btn3        |
|                   |
+-------------------+

창이 크기가 조정되면 구성 요소 BTN1 및 BTN2는 X 축 (각각의 절반)을 채워야하며, 구성 요소 BTN3은 X 축과 사용 가능한 모든 공간을 y 축의 모든 공간을 채워야합니다.

이것을 어떻게 달성 하시겠습니까?

다음은 다음과 같은 코드입니다.

public static void main(String[] args)
{
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = window.getContentPane();

    cp.setLayout(new MigLayout(""));
    cp.add(new JButton("btn1"), "");
    cp.add(new JButton("btn2"), "");
    cp.add(new JButton("btn3"), "");

    window.pack();
    window.setVisible(true);
}
도움이 되었습니까?

해결책

이것은 Miglayout에서 매우 쉽습니다.

setLayout(new MigLayout("fill"));

add(new JButton("button 1"), "w 50%");
add(new JButton("button 2"), "w 50%, wrap");
add(new JButton("button 3"), "grow, push, span");

Pstanton의 원래 질문을 읽으면 필요한 레이아웃 지침이 그가 공식화 한 방법에 매우 가깝다고 생각합니다. 그것이 내가 miglayout에 대해 좋아하는 것입니다 :)

다른 팁

나는 miglayout을 사용한 적이 없지만 다음과 같은 것이어야합니다.

...
cp.add(new JButton("btn1"));
cp.add(new JButton("btn2"), "wrap");
cp.add(new JButton("btn3"), "span");
...

그래서 당신은 다음과 같은 것을 원하십니까 :

example image

아주 스윙 레이아웃 데모 "흐름 방향"아래

다음은 해당 샘플의 코드입니다.

JTabbedPane tabbedPane = new JTabbedPane();

tabbedPane.addTab("Layout: flowx, Cell: flowx", createFlowPanel("", "flowx"));
tabbedPane.addTab("Layout: flowx, Cell: flowy", createFlowPanel("", "flowy"));
tabbedPane.addTab("Layout: flowy, Cell: flowx", createFlowPanel("flowy", "flowx"));
tabbedPane.addTab("Layout: flowy, Cell: flowy", createFlowPanel("flowy", "flowy"));

public JPanel createFlowPanel(String gridFlow, String cellFlow) {
    MigLayout lm = new MigLayout("center, wrap 3," + gridFlow,
                                 "[110,fill]",
                                 "[110,fill]");

    JPanel panel = createTabPanel(lm);

    for (int i = 0; i < 9; i++) {
        JButton b = createButton("" + (i + 1));
        b.setFont(b.getFont().deriveFont(20f));
        panel.add(b, cellFlow);
    }

    JButton b = createButton("5:2");
    b.setFont(b.getFont().deriveFont(20f));
    panel.add(b, cellFlow + ",cell 1 1");

    return panel;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top