按钮大小不符合尺寸

时间:2015-01-10 06:32:29

标签: java swing jframe jbutton dimension

我是java编程gui的新手。我制作了4个面板。如果我单独实现它们就像我希望它们一样,但是当我合并它们时,尺寸会发生变化。这是代码。第一行中的按钮应该像" C"应该有更多的空间然后"%"和添加按钮相同。

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;

public class calcu1row
{

    public static void main(String [] args)
    {
        JFrame frame = new JFrame("Calculator");


        JPanel panel0 = new JPanel(new GridBagLayout());


        JTextField text = new JTextField(" ", 10);
        text.setFont(new Font("Monotype Corsiva" , 15 , 30));
        panel0.add(text);



        JPanel panel1 = new JPanel(new GridBagLayout());
        panel1.setBackground(Color.GRAY);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5,5,5,5);
        //gbc.gridwidth = 4;
        gbc.weightx = 1.0; 
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JButton button = new JButton("C");
        JButton buttonDivide = new JButton(" % ");
        JButton buttonAdd = new JButton(" + ");

        gbc.gridx = 0;
        gbc.gridy = 0;
        //gbc.gridwidth = 2;
        //gbc.weightx = 2.0;


        panel1.add(button,gbc);

        gbc.gridx = 1;
        //gbc.gridy = 0;
       //gbc.weightx = 0.5;
        panel1.add(buttonDivide,gbc);

        gbc.gridx = 2;
        //gbc.weightx = 1.0;
        panel1.add(buttonAdd,gbc);

        JPanel panel2 = new JPanel(new GridBagLayout());

        panel2.setBackground(Color.GRAY);

        GridBagConstraints gbc2 = new GridBagConstraints();

        gbc2.weightx= 1.0;
        //gbc2.weighty = 1.0;
        gbc2.fill = GridBagConstraints.HORIZONTAL;



        gbc2.insets = new Insets(5,5,5,5);
        gbc2.gridwidth = 1;

        int k = 0;
        for(int i = 0 ; i < 4 ; i++)
        {
            for(int j = 0 ; j < 4 ; j++)
            { 


                //gbc2.gridwidth = 2;
                panel2.add(new JButton(""+j ), gbc2);
                //System.out.println(j);
                k++;
            }
            gbc2.gridy = k++;

    }


        JPanel panel3 = new JPanel(new GridBagLayout());


        GridBagConstraints gbc1 = new GridBagConstraints();

        gbc1.insets= new Insets(5,5,5,5);


        JButton button0 = new JButton("0");


        panel3.add(button0,gbc1);


        JButton buttondot = new JButton(".");

        panel3.add(buttondot,gbc1);

        JButton buttonequal = new JButton("=");

        panel3.add(buttonequal,gbc1);
        panel3.setBackground(Color.GRAY);


        //JButton b1 = new JButton("io");
        //panel3.add(b1,gbc);


        JPanel main = new JPanel(new GridBagLayout());
        GridBagConstraints gbc3 = new GridBagConstraints();
        //gbc3.weightx = 1.0;
        //gbc3.weighty = 1.0;
        //gbc3.gridwidth = 4;
        //gbc3.fill = GridBagConstraints.HORIZONTAL;
        gbc3.gridy=0;
        gbc3.gridx = 0;
        main.add(panel0,gbc3);

        gbc3.gridy = 1;
        gbc3.gridx = 0;
        main.add(panel1,gbc3);

        gbc3.gridy = 2;
        gbc3.gridx = 0;
        main.add(panel2,gbc3);


        gbc3.gridy = 6;
        gbc3.gridx = 0;
        main.add(panel3,gbc3);
        main.setBackground(Color.GRAY);

        frame.add(main);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(250,350);



    }
}

0 个答案:

没有答案