2012-05-15 64 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