Space button size on my layout keyboard won't resize

…衆ロ難τιáo~ 提交于 2019-11-28 12:53:31
Paul Samsotha

What pack() does is pack the frame, using the preferred sizes of the components. What you are doing is pack()ing before add the components and setting their preferred sizes, so the pack() is insignificant to the components added after the call (in terms of preferred size. So pack() the frame after you add all the components and set their preferred sizes.

Another thing you need to consider is that GridLayout will not respect the preferred size of the components. It will actually make them all the same size.

See here to see which layout managers will respect preferred sizes. And restructure your code accordingly. I'd look into a GridBagLayout as MadProgrammer suggest, that way you can just use one layout manager. It's a little tricky if you have no experience with it. Another option is just to stick with the default FlowLayout of the JPanel and nest panels as you have done.

Also see Laying out Components Within a Container to learn about the layout mangers available and hot they work.

There are two problems, one, you're calling pack before you've actually finished building the content. pack uses the contents preferred size to calculate the size of the window, this would cause you issues as the (potential) size of the content would be different to what you wanted. The fact it works is down to the use of setPreferredSize you called earlier, which you should avoid doing.

The second issue is the choice of layout manager. GridLayout will give each component within the container an equal amount of space, horizontally and vertically based on the avaiable space of the parent container.

A better solution might be to use GridBagLayout, for example.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Example extends JFrame {

    //Individual keyboard rows  
    String firstRow[] = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "fill", "BackSpace"};
    String secondRow[] = {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"};
    String thirdRow[] = {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "fill", "fill", "Enter"};
    String fourthRow[] = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "blank", "^"};
    String fifthRow[] = {"blank", "blank", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "", "<", "v", ">"};

    //Jbuttons corresponding to each individual rows 
    JButton first[];
    JButton second[];
    JButton third[];
    JButton fourth[];
    JButton fifth[];

    //Driver main method to start the application 
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                new Example().setVisible(true);
            }
        });
    }

    // No argument constructor to create frame
    public Example() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initWidgets();
    }

    // Method to initialize frame component 
    private void initWidgets() {

        //set the layout and place component in place and pack it 
        setLayout(new BorderLayout());
        //Various panel for the layout 
        JPanel jpNorth = new JPanel();
        JPanel jpCenter = new JPanel();
        JPanel jpKeyboard = new JPanel(new GridBagLayout());
        JPanel jpNote = new JPanel();
        add(jpNorth, BorderLayout.NORTH);
        add(jpNote);
        add(jpCenter, BorderLayout.CENTER);
        add(jpKeyboard, BorderLayout.SOUTH);

        first = new JButton[firstRow.length];
        second = new JButton[secondRow.length];
        third = new JButton[thirdRow.length];
        fourth = new JButton[fourthRow.length];
        fifth = new JButton[fifthRow.length];

        addKeys(jpKeyboard, 0, firstRow, first);
        addKeys(jpKeyboard, 1, secondRow, second);
        addKeys(jpKeyboard, 2, thirdRow, third);
        addKeys(jpKeyboard, 3, fourthRow, fourth);
        addKeys(jpKeyboard, 4, fifthRow, fifth);

        pack();

    } //end of initWidgets   

    protected void addKeys(JPanel parent, int row, String[] keys, JButton[] buttons) {

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = row;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.BOTH;

        int gap = 0;
        for (int index = 0; index < keys.length; index++) {
            String key = keys[index];
            if ("blank".equalsIgnoreCase(key)) {
                gbc.gridx++;
            } else if ("fill".equalsIgnoreCase(key)) {
                gbc.gridwidth++;
                gap++;
            } else {
                System.out.println("Add " + key);
                JButton btn = new JButton(key);
                buttons[index] = btn;
                parent.add(btn, gbc);
                gbc.gridx += gap + 1;
                gbc.gridwidth = 1;
                gap = 0;
            }
        }

    }

}//end of class

If you find the answer useful, I would appreciate if you could mark @peeskillet's answer as correct, as he answered first and spotted a few problems I didn't...an upvote would be nice though ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!