更改Java按钮GridBayLayout的大小 [英] Changing size of Java button GridBayLayout

查看:109
本文介绍了更改Java按钮GridBayLayout的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个大问题。我想制作=按钮2 *高度和0按钮2 *宽度(其他按钮应该只是普通尺寸),这就是我尝试了很多组合,但我得到了奇怪的尺寸。

Hello I have a big problem. I would like to make "=" button 2*height and the "0" button 2*width(OTHER buttons should be just normal size), that's all I tried many combinantions, but instead i get weird sizes.

我得到那个

我得到的是什么http://desmond.imageshack.us/Himg684/scaled.php?server=684&filename=33109545.jpg& res =登陆

我想得到类似于我在网上找到的(仅按钮布局)

I would like to get sth similar to that I found in web(only button layout)

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();
}


推荐答案

尝试使用此代码示例并询问可能出现的任何问题:

Try your hands on this code example and ask any questions that may arise :

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();
            }
        });
    }
}

这是输出:

这篇关于更改Java按钮GridBayLayout的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆