2016-09-08 6 views
0

GridBagLayout을 사용하여 인터페이스를 디자인했습니다. 인터페이스는 북쪽에 JTabbedPane을 설정하고 크기를 조정할 때 양방향을 채우며 JTabbedPane 바로 아래에 두 개의 JButton이 있습니다.여분의 JPanel을 컨테이너로 사용하지 않고 동일한 GridBagLayout 인터페이스를 구축 할 수 있습니까?

내가 (추가 JPanel를 도입하지 않고) GridBagLayout 기능을 사용하여 동쪽에서이 두 버튼을 가하고 달성하고자하는,하지만 난 그렇게하지 못했습니다.

| - [tab] -------------------------- |
| --------------------------------- | |
| --------------------------------- | |
| --------------------------------- | |
| --------------------------------- | |
| ------------- [button] [button] |

두 단추 모두에 대한 레이아웃 제한을 WEST (EAST 아님)로 설정할 때. 둘 다 올바르게 서쪽으로갑니다!

c.anchor = GridBagConstraints.WEST;

하지만 동쪽

c.anchor = GridBagConstraints.EAST;

에 두 버튼의 레이아웃 제약 조건을 설정할 때 그들 중

하나는 동쪽으로 이동하고, 다른 하나는 서쪽으로 간다!

이 문제를 해결하기 위해 두 버튼을 모두 잡고이 JPanel을 인터페이스에 추가하고 c.anchor = GridBagConstraints.EAST;으로 레이아웃 제약 조건을 설정했습니다. 잘 작동한다.

JPanel을 컨테이너로 사용하지 않고 동일한 GridBagLayout 인터페이스를 구축 할 수 있습니까?

다음 두 클래스는 SSCCE 형식입니다.

import javax.swing.*; 
import java.awt.*; 

public class GridBagTest extends JPanel { 

    JFrame frame; 
    JTabbedPane tabbedPane; 
    JPanel panel_tab; 

    JButton btn_previous; 
    JButton btn_next; 

    private void create_and_layout() { 

     this.setLayout(new GridBagLayout()); 

     tabbedPane = new JTabbedPane() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(300, 150); 
      } 
     }; 

     panel_tab = new JPanel(); 
     tabbedPane.addTab("Tab 1", panel_tab); 

     btn_previous = new JButton("previous"); 
     btn_next = new JButton(" next "); 

     GridBagConstraints c; 

     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     c.gridwidth = 2; 
     c.weighty = 1; 
     c.fill = GridBagConstraints.BOTH; 
     c.anchor = GridBagConstraints.NORTH; 
     this.add(tabbedPane, c); 

     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.EAST; 
     c.insets = new Insets(2, 0, 2, 2); 
     this.add(btn_previous, c); 

     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.EAST; 
     c.insets = new Insets(2, 0, 2, 2); 
     this.add(btn_next, c); 
    } 

    private void initGUI() { 
     create_and_layout(); 
     frame = new JFrame("GridBagTest"); 
     frame.getContentPane().add(this); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new GridBagTest()::initGUI); 
    } 
} 

솔루션 클래스 :

import javax.swing.*; 
import java.awt.*; 

public class GridBagTest_solved extends JPanel { 

    JFrame frame; 
    JTabbedPane tabbedPane; 
    JPanel panel_tab; 
    JPanel panel_buttons; 

    JButton btn_previous; 
    JButton btn_next; 

    private void create_and_layout() { 

     this.setLayout(new GridBagLayout()); 

     tabbedPane = new JTabbedPane() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(300, 150); 
      } 
     }; 

     panel_tab = new JPanel(); 
     tabbedPane.addTab("Tab 1", panel_tab); 

     panel_buttons = new JPanel(new GridBagLayout()); 

     btn_previous = new JButton("previous"); 
     btn_next = new JButton(" next "); 

     GridBagConstraints c; 
     // Adding buttons to panel_buttons 
     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(2, 0, 2, 2); 
     panel_buttons.add(btn_previous, c); 

     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 0; 
     c.insets = new Insets(2, 0, 2, 2); 
     panel_buttons.add(btn_next, c); 

     // Adding tabbedPane & panel_buttons to this (GridBagTest_solved) 
     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     c.weighty = 1; 
     c.fill = GridBagConstraints.BOTH; 
     c.anchor = GridBagConstraints.NORTH; 
     this.add(tabbedPane, c); 

     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.EAST; 
     this.add(panel_buttons, c); 
    } 

    private void initGUI() { 
     create_and_layout(); 
     frame = new JFrame("GridBagTest_solved"); 
     frame.getContentPane().add(this); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new GridBagTest_solved()::initGUI); 
    } 
} 
첫 번째 문제 GridBagTest, 다른 쇼 솔루션 추가 JPanel

문제 클래스를 사용합니다 GridBagTest_solved을 보여

답변

1

JTabbedPane과 JButton의 세 가지 구성 요소 만 있으면 GridBag보다 간단한 레이아웃을 사용해야합니다. 메인 패널에 BorderLayout을 사용하고, JTabbedPane을 CENTER에 배치하고, TRANING이 정렬 된 FlowLayout을 가진 JPanel을 남겨두기 만하면 JPanel에 두 개의 버튼을 추가하면됩니다.

+0

이 코드는 [SSCCE] (http://sscce.org/)를 따르기 위해 단순화되었습니다. 그리고 제가 문제를 해결할 때 단지'GridBagLayout' 기능 만 사용하는 솔루션이 있는지 알고 싶습니다. –