2012-05-15 59 views
1

你好,我有一個大問題。我想使「=」按鈕2 *高度和「0」按鈕2 *寬度(其他按鈕應該只是正常大小),這就是我嘗試了很多combinantions,但相反,我得到奇怪的大小。改變Java按鈕的大小GridBayLayout

O I拿到

what i get http://desmond.imageshack.us/Himg684/scaled.php?server=684&filename=33109545.jpg&res=landing

我想獲得某物類似於我在網絡上找到的(唯一的按鈕佈局)

enter image description here

public void someMethod() 
    Container cp = getContentPane(); 
    cp.setLayout(new BorderLayout()); 
    JPanel wyswietlacz = new JPanel(); 
    JTextField txt = new JTextField("123"); 
    txt.setPreferredSize(new Dimension(getWidth() - 10, 35)); 
    txt.setAlignmentX(JTextField.RIGHT_ALIGNMENT); 
    wyswietlacz.add(txt); 
    JPanel opcje = new JPanel(); 
    String[] etykiety = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5", 
      "6", "+", "1", "2", "3", "=", "0", "+/-" }; 

    JButton[] przyciski = new JButton[18]; 
    for (int i = 0; i < przyciski.length; i++) 
     przyciski[i] = new JButton(etykiety[i]); 

    GridBagLayout gridbag = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.fill = GridBagConstraints.BOTH; 
    opcje.setLayout(gridbag); 
    for (int i = 0; i < przyciski.length; i++) { 
     if (((i + 1) % 4) == 0) { 

      c.gridwidth = GridBagConstraints.REMAINDER; 
     } else { 
      c.gridwidth = GridBagConstraints.RELATIVE; 

     } 

     if (i == 15) { 
      c.gridheight = 2; 

      c.fill = GridBagConstraints.HORIZONTAL; 
     } 
     if (i == 16) 
      c.gridy = GridBagConstraints.SOUTH; 

     if (i == 16) { 
      c.gridwidth = 2; 
      c.fill = GridBagConstraints.HORIZONTAL; 
     } 

     makebutton(przyciski[i], gridbag, c, opcje); 
    } 

    add(wyswietlacz, BorderLayout.NORTH); 
    add(opcje, BorderLayout.CENTER); 

} 

protected void makebutton(JButton button, GridBagLayout gridbag, 
     GridBagConstraints c, JPanel jp) { 
    gridbag.setConstraints(button, c); 
    jp.add(button); 
} 

public static void main(String[] args) { 
    new Kalkulator(); 
} 
+0

只是請確保'的someMethod()'ISN塗料的方法! –

回答

7

嘗試在此代碼示例你的手,並要求任何可能出現的問題:

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

public class GridBagTest 
{ 
    private String[] buttonText = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5", 
      "6", "+", "1", "2", "3", "=", "0", "+/-" }; 
    private JButton[] button = new JButton[18]; 
    private int counter = 0; 

    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("GridBagLayout Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout()); 

     JPanel northPanel = new JPanel(); 
     northPanel.setLayout(new BorderLayout(2, 2)); 
     JTextField tfield = new JTextField(); 
     northPanel.add(tfield, BorderLayout.CENTER); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.PAGE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.insets = new Insets(2, 2, 2, 2); 
     for (int i = 0; i < button.length; i++) 
     { 
      System.out.println("Button Text : " + buttonText[i]); 
      button[i] = new JButton(buttonText[i]); 
     } 
     for (int i = 0; i < 3; i++) 
     { 
      for (int j = 0; j < 4; j++) 
      { 
        gbc.gridx = j; 
        gbc.gridy = i; 
        centerPanel.add(button[counter++], gbc); 
      } 
     } 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     centerPanel.add(button[counter++], gbc); 
     gbc.gridx = 1; 
     gbc.gridy = 3; 
     centerPanel.add(button[counter++], gbc); 
     gbc.gridx = 2; 
     gbc.gridy = 3; 
     centerPanel.add(button[counter++], gbc); 
     gbc.gridx = 3; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 2; 
     centerPanel.add(button[counter++], gbc); 
     int count = counter; 
     System.out.println(button[--count].getText()); 
     gbc.gridx = 0; 
     gbc.gridy = 4; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 2; 
     centerPanel.add(button[counter++], gbc); 
     gbc.gridwidth = 1; 
     gbc.gridx = 2; 
     gbc.gridy = 4; 
     centerPanel.add(button[counter++], gbc); 

     contentPane.add(northPanel, BorderLayout.PAGE_START); 
     contentPane.add(centerPanel, BorderLayout.CENTER); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

這裏是輸出:

CALCULATOR

+0

真棒!謝謝! – Yoda

+0

@RobertKilar:你最歡迎並保持微笑:-) –

+0

一個花哨的GridBagLayout例子也是如此。 1+ –

6

我可以」沒有看到任何問題,請閱讀

然後

enter image description here

從代碼

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

public class GridBagButtons { 

    private static final Insets insets = new Insets(0, 0, 0, 0); 

    public static void main(final String args[]) { 
     final JFrame frame = new JFrame("GridBagLayout"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridBagLayout()); 
     JButton button; 
     // Row One - Three Buttons 
     button = new JButton("One"); 
     addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     button = new JButton("Two"); 
     addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     button = new JButton("Three"); 
     addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     // Row Two - Two Buttons 
     button = new JButton("Four"); 
     addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     button = new JButton("Five"); 
     addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     // Row Three - Two Buttons 
     button = new JButton("Six"); 
     addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     button = new JButton("Seven"); 
     addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     frame.setSize(500, 200); 
     frame.setVisible(true); 
    } 

    private static void addComponent(Container container, Component component, int gridx, int gridy, 
      int gridwidth, int gridheight, int anchor, int fill) { 
     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, 
       anchor, fill, insets, 0, 0); 
     container.add(component, gbc); 
    } 

    private GridBagButtons() { 
    } 
} 
  • 我會用NestedLayout而不是GrigBagLayout,SpringLayout中或MigLayout
+0

謝謝我更正了代碼: http://wklej.org/id/753239/但我得到了那樣的 ![](http://desmond.imageshack.us/Himg207/scaled.php?server=207&filename = 25202449.jpg&res = landing) – Yoda

+0

也許自定義MigLayout是更好,最簡單的,抱歉,但我不知道他們的許可, – mKorbel

+0

任何想法與此GridBag :(從默認庫? – Yoda

5

困擾的問題是,你沒有設置的weightx和分量上的GridBagConstraints。將它們設置爲1.0將平均分配(水平/垂直)額外空間給每個組件。

+0

http://wklej.org/id/753244/ 謝謝, 現在它的工作,但只有當我使用正確的窗口大小,否則最後行將適合窗口。 – Yoda

+0

@RobertKilar你是什麼意思太寬? –

+0

http://desmond.imageshack.us/Himg191/scaled.php?server=191&filename=72720252。jpg&res = landing 左邊是JFrame的大小500,300,右邊是180,208 – Yoda