2012-04-04 14 views
1

私はJava Appletを作成しようとしていましたが、私はJava Webサイトを見ていて、編集した素晴らしい例が見つかりました。ボタンを広げる方法Java

この例では、ヘッダーのような3つのボタンと、フッターのような のようなものと、ボディのようなものがあります。私の問題は、中間の(ボディ)ボタンが完全には伸びないということです。

このコードを実行すると、私の意図がわかります。

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

public class main { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    public static void addComponentsToPane(Container pane) { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     JButton button; 
    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    if (shouldFill) { 
    //natural height, maximum width 
    c.fill = GridBagConstraints.HORIZONTAL; 
    } 


    button = new JButton("hi"); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.weightx = 0.5; 
    c.gridx = 1; 
    c.gridy = 0; 
    c.ipady = 40; 
    pane.add(button, c); 

    button = new JButton("Hello, i will not expand"); 
    c.fill = GridBagConstraints.BOTH; 
    c.weighty = 1.0; //request any extra vertical space 
    c.weightx=1.0; 
    c.anchor = GridBagConstraints.CENTER; //bottom of space 
    c.insets = new Insets(10,0,0,0); //top padding 
    c.gridx = 1;  //aligned with button 2 
    c.gridy = 1;  //third row 
    pane.add(button, c); 


    button = new JButton("fr"); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 0;  //reset to default 
    c.weighty = 1.0; //request any extra vertical space 
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
    c.insets = new Insets(10,0,0,0); //top padding 
    c.gridx = 1;  //aligned with button 2 
    c.gridwidth = 2; //2 columns wide 
    c.gridy = 2;  //third row 
    pane.add(button, c); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("main"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Set up the content pane. 
     addComponentsToPane(frame.getContentPane()); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

enter image description here

答えて

3

それは中央ボタンのせいではありませんように見えますが、あなたがそうc.fill = GridBagConstraints.BOTH;として下のボタンを設定した場合、拡大されていない下のボタンは、それは下の空のスペースを占有します。

中間を大きくしますか?下ボタンの重さを0.c.weighty = .5; //request any extra vertical space
に変更して、それがあなたが探しているものかどうかを確認することができます。

あなたは真ん中がトップとボトムその後、はるかに大きいようにしたい場合は、1,3,1など

+0

ああ、私は最初にあなたの答えをしようとしたが、それは同じ結果なので、私はc.weighty = 0を試してみました。ありがとうございました – user1294188

+0

あなたがそれを望んでいたのは確かに大きかったとは思えませんでしたが、私が手伝ってくれることを嬉しく思っていました! –

2

を試し、1,1,1-そうではなく、その後、より多くのグリッドスペースを作る必要があるかもしれません私はJavaのウェブサイトを見ていて、私が編集した素晴らしい例が見つかりました。

だから私はあなたがはGridBagLayoutのを使用するを持っていないそれを取りますか?

BorderLayoutを使用して、BorderLayout.CENTERの位置に展開可能なボタンを作成します。素敵な

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

関連する問題