2016-09-08 7 views
0

私はGridBagLayoutを使用してインターフェイスを設計しています。インターフェイスは、JTabbedPaneを北に設定し、サイズを変更すると両方向に塗りつぶします。JTabbedPaneのすぐ下に2つのJButtonがあります。追加のJPanelをコンテナとして使用しなくても、同じGridBagLayoutインターフェイスを構築できますか?

私は(余分JPanelを導入することなく)のみGridBagLayout機能を使用してでこれらの2つのボタンが入れている達成したいが、私はそうしませんでした。

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

両方のボタンのレイアウト制約をWEST(EASTではなく)に設定したとき。両方とも正しく西に行く!

c.anchor = GridBagConstraints.WEST;

しかし、私は東

c.anchor = GridBagConstraints.EAST;

に両方のボタンのレイアウト制約を設定するときにそれらの

一つは東に行くと、他の西に行きます!

この問題を解決するために、JPanelを追加し、両方のボタンを保持し、このJPanelをインターフェイスに追加し、そのレイアウト制約をc.anchor = GridBagConstraints.EAST;に設定しました。それはうまくいく。

JPanelをコンテナに使用せずに同じGridBagLayoutインターフェイスを構築することはできますか?

SSCCEフォーマットには、次の2つのクラスがあります。

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と2つのJButtonの3つのコンポーネントしか持たないため、GridBagよりも簡単なレイアウトを使用することを検討する必要があります。メインパネルにBorderLayoutを使用し、JTabbedPaneをCENTERに、JPanelをSOUTHに配置して、TRAILINGの整列を持つFlowLayoutを持つようにして、そのJPanelに2つのボタンを追加するだけです。

+0

コードは[SSCCE](http://sscce.org/)に準拠するように単純化されています。私が問題を解決したときに、 'GridBagLayout'機能のみを使用するソリューションがあるかどうかを知りたいとします。 –

関連する問題