2012-05-08 3 views
2

私が作業しているUIは、ユーザーがムービーを選択して再生できるパネルを表示します。再生、一時停止などのコントロールがあります。GridBagLayout - 1行の高さによって次の行の幅が変更されます

レイアウトは私が望むように見えます。パネルはGridBagLayoutを使用します。行2はステータスメッセージのテキストエリアを表示し、行3はボタンと進行状況バーを表示するパネルを表示します。

私が問題になっているのは、テキスト領域にテキスト行が多すぎると、行3のボタンが折り返してしまうことです。これは外枠の高さに関係なく行われます。

行2の高さが行3の幅に影響しています。この動作はわかりません。誰かが私に間違ってやっていることを教えてもらえ、それをどうやって解決できるのか誰かが私に教えてくれるのだろうかと思っています私はコードを添付しました。

少し異なるトピックで、コードを見ている場合は、一番下のコンポーネントと一番外側のパネルの間に余白を残す方法も提案できますか?

ご協力いただきありがとうございます。

よろしく、
ピーター

だけ Center and Bottomための1 JPanel各ので、あなたの JTextAreaあなた GridBagLayout意志サーバー目的とその後 BorderLayoutまで、あなたのためのトリックを行います追加


    private static JButton CreateImageButton(String fileName) { 
     JButton retVal = new JButton("xxx"); 
     return retVal; 
    } 

    public MoviePanel() { 
     this.setLayout(new GridBagLayout()); 
     this.setBackground(Color.WHITE); 

     JButton btnRefresh = CreateImageButton("refresh.png"); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx=0; 
     c.gridy=0; 
     c.fill = GridBagConstraints.NORTH; 
     c.insets.left = 10; c.insets.right = 10; c.insets.top = 10; 
     this.add(btnRefresh, c); 

     JComboBox cbMovieList = new JComboBox(); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 0; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.insets.right = 10; c.insets.top = 10; 
     c.weightx = 1.0; 
     this.add(cbMovieList, c); 

     JButton btnAuthorize = new JButton("Get Info"); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.WEST; 
     c.insets.top = 10; 
     this.add(btnAuthorize, c); 

     JTextArea txtInfo = new JTextArea(); 
     txtInfo.setFont(new Font("SansSerif", Font.BOLD, 12)); 
     txtInfo.setBackground(Color.cyan); 
     // txtInfo.setText("abc\ndef"); 
     txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz"); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 2; 
     c.anchor = GridBagConstraints.NORTHWEST; 
     c.weighty = 1.0; 
     c.insets.top = 10; 

     this.add(txtInfo, c); 

     JPanel controllerOuter = new JPanel(); 
     controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS)); 
     controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder()); 

     FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER); 
     controllerLayout.setHgap(0); 
     JPanel controller = new JPanel(controllerLayout); 

     controller.setBorder(new EmptyBorder(10, 10, 10, 10)); 

     Dimension dim = new Dimension(60, 40); 
     JButton btnPlay = CreateImageButton("play.png"); 
     btnPlay.setPreferredSize(dim); 

     controller.add(btnPlay); 
     JButton btnPause = CreateImageButton("pause.png"); 
     btnPause.setPreferredSize(dim); 
     controller.add(btnPause); 
     JButton btnStop = CreateImageButton("stop.png"); 
     btnStop.setPreferredSize(dim); 
     controller.add(btnStop); 
     JButton btnForward = CreateImageButton("forward.png"); 
     btnForward.setPreferredSize(dim); 
     controller.add(btnForward); 
     JComboBox cbAspectRatio = new JComboBox(); 
     cbAspectRatio.setPreferredSize(new Dimension(100, 40)); 
     cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0)); 
     controller.add(cbAspectRatio); 

     controllerOuter.add(controller); 

     JProgressBar pbProgress = new JProgressBar(0, 100); 
     pbProgress.setPreferredSize(new Dimension(350, 40)); 
     pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10)); 
     pbProgress.setValue(50); 
     pbProgress.setString("50/100"); 
     pbProgress.setStringPainted(true); 
     pbProgress.setForeground(Color.BLUE); 
     pbProgress.setBorderPainted(true); 
     controllerOuter.add(pbProgress); 


     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 3; 
     c.gridwidth = 2; 
     c.weightx = 1.0; 
     this.add(controllerOuter, c); 
    } 

Here is the image

+0

_wrap around_とはどういう意味ですか?あなたのコードをTAで別のテキストで数回走らせました。ボタンは常に同じ場所にあります。投稿を編集して、表示されている問題のスクリーンショットを追加することはできますか? –

+0

イメージを追加しました。あなたが見ることができるように、フレームの高さを減らした後で5番目のコントロールがラップしています。 – Peter

+0

実際には、ラップされたComboBoxでボタンのラッピングについて話したので、私は混乱しました。私はすぐに –

答えて

4

私はあなたのコード内のいくつかを参照してください。

  1. あなたはJButtonのpreferredSizeを強制します。可能であれば、これはソリューションよりも多くの問題が発生するため、削除します。 preferredSizeを強制したい場合は、最小サイズと最大サイズにも注意する必要があります。そうでない場合は、観察しているような奇妙な動作が発生します。
  2. BoxLayoutを使用してコントロールを表示します。これは完全に受け入れられますが、BoxLayoutは設定しなかったレイアウトを実行するために最小/最大サイズにも依存しています。
  3. レイアウトが壊れています。これも問題ありませんが、MoviePanelのGridBagLayoutだけを使用してみませんか?
  4. 通常、テキストが大きすぎると、TextAreasはJScrollPaneでラップされます。 TextAreaにse​​tLineWrap(true)を設定することもできます。そうすることで、右に大きく移動することはありません。 TextAreaの行/列を設定することにより、preferredreSizeを定義します(これが含まれるテキストに依存しないようにする)。
  5. NONE,VERTICALHORIZONTALまたはBOTH(いずれかにVERTICALを使用しました)は、GridBagConstraintsでfillプロパティにのみ指定できます。また、新しいインスタンスを再作成する必要はなく、同じGridBagConstraintを何度も再利用することができます。コンポーネントの制約を設定すると、LayoutManagerによって自動的に複製されます。あなたがcontollerOuterを追加すると、

    1. あなたがのpreferredSizeがを設定した場合も
    2. (これはあなたの問題を解決する最も簡単な方法です)c.fill = GridBagConstraints.HORIZONTAL;を指定:

    は今のソリューションのために、私はいくつかのを見つけましたまた、JButtonsはminimumSizeを同じ値に強制します。

  6. GridBagLayoutのみを使用して、すべてのコンポーネントをレイアウトします。 (これは私のお気に入りです)
  7. FlowLayoutをBoxLayoutでX_AXISに置き換えます。

プロパティをGridBagConstraintsのRember:

  • gridx、gridy:場所を指定
  • gridwidth、gridheightは:/余分な水平を取得する指定:重いCOLSPAN/ROWSPAN
  • weightxには、指定垂直方向のスペースとどのような割合であるか
  • アンカー:「セル」がコンポーネントより大きい場合、「セル」とコンポーネントの整列を指定します。
  • fill:コンポーネントがセルの幅/高さに伸びるかどうかを指定します。
+0

+1、私はこの答えが好きです、ちょうどわかりません、今日は怠惰だと思われます、大きさの設定に関連するものを言及すべきでした、私は思った、神はOPが何を考えているか知っています:-)、話題に触れることはありません。 –

+0

あなたの助けに感謝します。それに応じてコードを変更します。 GridBagConstraintの再利用については、古い値が間違って持ち越される可能性があるため、OracleのWebサイトからのヒントはありませんでした。よろしく。 – Peter

+0

@Peterはいそれは本当です。しかし、同じ「行」に複数のコンポーネントをレイアウトすると便利なことがあります。しかし、通常、ループで同じGridBagConstraintを使用し、モデルから値を取得するため、この問題は発生しません。あなたはあなたがより快適なものを選ぶべきです。 –

3

MAIN JPanelのいずれかになります。さらに、JScrollPaneを追加することで、他の領域で必要とされる労力が軽減されます。コードと出力を見て:あなたはより多くの行を追加すると、ここで

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

public class JTextPaneExample extends JPanel 
{ 
    private Icon info = UIManager.getIcon("OptionPane.informationIcon"); 
    private Icon error = UIManager.getIcon("OptionPane.errorIcon"); 

    private static JButton CreateImageButton(String fileName) { 
     JButton retVal = new JButton("xxx"); 
     return retVal; 
    } 

    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("JTextPane Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     this.setLayout(new BorderLayout()); 
     this.setBackground(Color.WHITE); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     centerPanel.setLayout(new GridBagLayout()); 
     centerPanel.setBackground(Color.WHITE); 
     JButton btnRefresh = CreateImageButton("refresh.png"); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx=0; 
     c.gridy=0; 
     c.fill = GridBagConstraints.NORTH; 
     c.insets.left = 10; c.insets.right = 10; c.insets.top = 10; 
     centerPanel.add(btnRefresh, c); 

     JComboBox cbMovieList = new JComboBox(); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 0; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.insets.right = 10; c.insets.top = 10; 
     c.weightx = 1.0; 
     centerPanel.add(cbMovieList, c); 

     JButton btnAuthorize = new JButton("Get Info"); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.WEST; 
     c.insets.top = 10; 
     centerPanel.add(btnAuthorize, c); 

     JTextArea txtInfo = new JTextArea(); 
     txtInfo.setFont(new Font("SansSerif", Font.BOLD, 12)); 
     txtInfo.setBackground(Color.cyan); 
     // txtInfo.setText("abc\ndef"); 
     txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz"); 
     JScrollPane scroller = new JScrollPane(); 
     scroller.setViewportView(txtInfo); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 2; 
     c.anchor = GridBagConstraints.NORTHWEST; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weighty = 1.0; 
     c.insets.top = 10; 

     centerPanel.add(scroller, c); 

     JPanel controllerOuter = new JPanel(); 
     controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS)); 
     controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder()); 

     FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER); 
     controllerLayout.setHgap(0); 
     JPanel controller = new JPanel(controllerLayout); 

     controller.setBorder(new EmptyBorder(10, 10, 10, 10)); 

     Dimension dim = new Dimension(60, 40); 
     JButton btnPlay = CreateImageButton("play.png"); 
     btnPlay.setPreferredSize(dim); 

     controller.add(btnPlay); 
     JButton btnPause = CreateImageButton("pause.png"); 
     btnPause.setPreferredSize(dim); 
     controller.add(btnPause); 
     JButton btnStop = CreateImageButton("stop.png"); 
     btnStop.setPreferredSize(dim); 
     controller.add(btnStop); 
     JButton btnForward = CreateImageButton("forward.png"); 
     btnForward.setPreferredSize(dim); 
     controller.add(btnForward); 
     JComboBox cbAspectRatio = new JComboBox(); 
     cbAspectRatio.setPreferredSize(new Dimension(100, 40)); 
     cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0)); 
     controller.add(cbAspectRatio); 

     controllerOuter.add(controller); 

     JProgressBar pbProgress = new JProgressBar(0, 100); 
     pbProgress.setPreferredSize(new Dimension(350, 40)); 
     pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10)); 
     pbProgress.setValue(50); 
     pbProgress.setString("50/100"); 
     pbProgress.setStringPainted(true); 
     pbProgress.setForeground(Color.BLUE); 
     pbProgress.setBorderPainted(true); 
     controllerOuter.add(pbProgress); 

     add(centerPanel, BorderLayout.CENTER); 
     add(controllerOuter, BorderLayout.PAGE_END); 

     frame.getContentPane().add(this); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new JTextPaneExample().createAndDisplayGUI(); 
      }   
     }); 
    } 
} 

が出力されます。

LAYOUT

+0

+1にもお答えします。 BorderLayoutの素晴らしいソリューションシンプルで簡単。 –

関連する問題