2017-09-09 8 views
0

私は要素を整理するために各グリッド内にFlowLayoutGridLayoutを使用して、さまざまなボタンとラベルの4行で簡単なメニューインターフェイスを作成しようとしています。しかし、ボタンとラベルのスペースは1行しか取らないはずですが、膨大なスペースが必要です。GridLayoutの各グリッドの高さを変更することはできますか?

これを最小限に抑えるように私のインターフェイスが見えるものです:

これが最大のようにそれが見えるものです:

私はラベルの最大サイズを設定するには探していますパネル/グリッドを使用して、わずかなスペースしか必要としません。

私はこのような可能な限り小さなウィンドウサイズで隠されて何もせずに、すべての要素が見えるようにしようとしています:

これは私のコードです:

public class Window extends JFrame{ 
public Window() { 
    super("TastyThai Menu Ordering"); 
} 

public static void main(String[] args) { 
    Window w = new Window(); 
    w.setSize(500, 500); 
    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JLabel title = new JLabel("TastyThai Menu Order", SwingConstants.CENTER); 
    title.setFont(title.getFont().deriveFont(32f)); 

    //generate page title 
    Container titlePanel = new JPanel(); // used as a container 
    titlePanel.setBackground(Color.WHITE); 
    FlowLayout flow = new FlowLayout(); // Create a layout manager 
    titlePanel.setLayout(flow);// assign flow layout to panel 
    titlePanel.add(title); // add label to panel 
    w.getContentPane().add(BorderLayout.NORTH,titlePanel); 

    //generate row containers 
    Container r1 = new JPanel(new FlowLayout()); 
    Container r2 = new JPanel(new FlowLayout()); 
    Container r3 = new JPanel(new FlowLayout()); 
    Container r4 = new JPanel(new FlowLayout()); 

    //generate mains radio buttons 
    Container mains = new JPanel(new GridLayout(7, 0)); 
    mains.setBackground(Color.RED); 
    JLabel mainsHeader = new JLabel("Mains"); 
    mains.add(mainsHeader); 
    String[] mainsChoices = {"Vegetarian", "Chicken", "Beef", "Pork", "Duck", "Seafood Mix"}; 
    JRadioButton[] mainsRadioButton = new JRadioButton[6]; 
    ButtonGroup mainsButtons = new ButtonGroup(); 
    for(int i = 0; i < mainsChoices.length; i++) { 
     mainsRadioButton[i] = new JRadioButton(mainsChoices[i]); 
     mains.add(mainsRadioButton[i]); 
     mainsButtons.add(mainsRadioButton[i]); 
    } 

    //generate noodles radio buttons 
    Container noodles = new JPanel(new GridLayout(7, 0)); 
    noodles.setBackground(Color.GREEN); 
    JLabel noodlesHeader = new JLabel("Noodles"); 
    noodlesHeader.setFont(noodlesHeader.getFont().deriveFont(24f)); 
    noodles.add(noodlesHeader); 
    String[] noodlesChoices = {"Pad Thai", "Pad Siew", "Ba Mee"}; 
    JRadioButton[] noodlesRadioButton = new JRadioButton[3]; 
    ButtonGroup noodlesButtons = new ButtonGroup(); 
    for(int i = 0; i < noodlesChoices.length; i++) { 
     noodlesRadioButton[i] = new JRadioButton(noodlesChoices[i]); 
     noodles.add(noodlesRadioButton[i]); 
     noodlesButtons.add(noodlesRadioButton[i]); 
    } 

    //generate sauces radio buttons 
    Container sauces = new JPanel(new GridLayout(7, 0)); 
    sauces.setBackground(Color.BLUE); 
    JLabel saucesHeader = new JLabel("Sauce"); 
    saucesHeader.setFont(saucesHeader.getFont().deriveFont(24f)); 
    sauces.add(saucesHeader); 
    String[] saucesChoices = {"Soy Sauce", "Tamarind Sauce"}; 
    JRadioButton[] saucesRadioButton = new JRadioButton[2]; 
    ButtonGroup saucesButtons = new ButtonGroup(); 
    for(int i = 0; i < saucesChoices.length; i++) { 
     saucesRadioButton[i] = new JRadioButton(saucesChoices[i]); 
     sauces.add(saucesRadioButton[i]); 
     saucesButtons.add(saucesRadioButton[i]); 
    } 

    //generate extras check boxes 
    Container extras = new JPanel(new GridLayout(7, 0)); 
    extras.setBackground(Color.YELLOW); 
    JLabel extrasHeader = new JLabel("Extra"); 
    extrasHeader.setFont(extrasHeader.getFont().deriveFont(24f)); 
    extras.add(extrasHeader); 
    String[] extrasChoices = {"Mushroom", "Egg", "Broccoli", "Beansrpout", "Tofu"}; 
    JCheckBox[] extrasBoxes = new JCheckBox[5]; 
    for(int i = 0; i < extrasChoices.length; i++) { 
     extrasBoxes[i] = new JCheckBox(extrasChoices[i]); 
     extras.add(extrasBoxes[i]); 
    } 

    JLabel selectionPrice = new JLabel("Selection Price: $ "); 
    JLabel selectionPriceVal = new JLabel("_______________"); 
    JButton addToOrder = new JButton("Add to Order"); 

    JLabel totalPrice = new JLabel("Total Price: $ "); 
    JLabel totalPriceVal = new JLabel("_______________"); 
    JButton clearOrder = new JButton("Clear Order"); 

    JRadioButton pickUp = new JRadioButton("Pick Up"); 
    JRadioButton delivery = new JRadioButton("Delivery"); 
    ButtonGroup pickupDelivery = new ButtonGroup(); 
    pickupDelivery.add(pickUp); 
    pickupDelivery.add(delivery); 
    JButton completeOrder = new JButton("Complete Order"); 

    Container menuSelection = new JPanel(new GridLayout(4,0)); 
    menuSelection.add(r1); 
    r1.add(mains); 
    r1.add(noodles); 
    r1.add(sauces); 
    r1.add(extras); 
    menuSelection.add(r2); 
    r2.add(selectionPrice); 
    r2.add(selectionPriceVal); 
    r2.add(addToOrder); 
    menuSelection.add(r3); 
    r3.add(totalPrice); 
    r3.add(totalPriceVal); 
    r3.add(clearOrder); 
    menuSelection.add(r4); 
    r4.add(pickUp); 
    r4.add(delivery); 
    r4.add(completeOrder); 

    w.getContentPane().add(BorderLayout.CENTER, menuSelection); 

    w.setVisible(true); 
} 
} 
+0

これは、コンポーネントの最大サイズとは関係ありません。 GridLayout javadocは次のように述べています:* GridLayoutクラスは、コンテナのコンポーネントを長方形のグリッド内にレイアウトするレイアウトマネージャです。コンテナは等しいサイズの矩形に分割され、1つのコンポーネントが各矩形に配置されます*。それが望ましくない場合は、そのレイアウトを使用せず、あなたが望むことを実行できるものを選択します(これは不明です)。 –

+0

@JBNizet私はこのようにできるだけ小さなウィンドウサイズで隠されているものがなくてもすべての要素を見えるようにしようとしています:https://imgur.com/4zBhQafでは1行ずつ選択価格で注文ボタンに追加してから新しいラインの総価格とクリアオーダーボタンと新しいラインピックアップ/配信ラジオボタンと完全なオーダーボタンで。私はフローレイアウトだけでこれを達成することができましたが、フローレイアウトで新しいラインを作る方法はわかりません。だから私はグリッドレイアウトでフローレイアウトをネストしますが、 – nanj

+0

GUIの4つのセクションを積み重ねるのに 'BoxLayout'を使うかもしれません。 –

答えて

2

GridLayoutのそれをサポートしていません。すべての矩形は同じサイズです。

動的なサイズ変更などをサポートするGridBagLayoutをご覧ください。

関連する問題