在屏幕上键盘JButton完全符合文本大小

时间:2014-05-08 12:23:29

标签: java swing jbutton layout-manager on-screen-keyboard

我在应该显示键盘的屏幕键盘上写了这个。 我的问题是我希望JButtons适合他们的标签大小而不是为所有JButton设置修复大小。 例如键盘上的数字:“1,2,3 ....”应该得到小的jbutton和键“退格”“标签”等...应该更大(适合他们的标签大小)

这是我写的代码:

package Q2;
import java.awt.*;

import javax.swing.*;

public class MainPanel extends JPanel{

    private JButton[][] button;
    private JPanel[] panel;                                                     //Array of panels for each buttons line
    private JPanel parent;
    private static final String[][] key = {
        {"`","1","2","3","4","5","6","7","8","9","0","-","+","Backspace"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]"},
        {"Caps","A","S","D","F","G","H","J","K","L",";","'","\\","Enter"},
        {"Shif","Z","X","C","V","B","N","M",",",".","?","/"},
        {"                  ",",","<","v",">"}
    };

    //Constructor for main Panel
    public MainPanel(){
        super();
        setLayout(new BorderLayout());
        TextField textField= new TextField(20);
        Font font1 = new Font("david", Font.BOLD, 22);
        textField.setFont(font1);
        add(textField,BorderLayout.CENTER);

        //initialize the parent panel and array of 5 panels and the buttons array
        parent = new JPanel();
        parent.setLayout(new GridLayout(0,1));
        panel = new JPanel[5];
        button = new JButton[20][20];

        for (int row = 0; row<key.length; row++){
            panel[row] = new JPanel();
            for (int column = 0; column<key[row].length; column++){
                button[row][column] = new JButton(key[row][column]);
                button[row][column].setPreferredSize(new Dimension(key[row][column].length()+80,30));
                button[row][column].putClientProperty("row", row);
                button[row][column].putClientProperty("column", column);
                button[row][column].putClientProperty("key", key[row][column]);
                //button[row][column].addActionListener(new MyActionListener());
                panel[row].add(button[row][column]);
            }
            parent.add(panel[row]);
        }
        add(parent,BorderLayout.SOUTH);


    }
    /*
    //panel for line 1 of keyboard buttons - numbers
    protected JComponent getPanelLine1(){
        JPanel panel1 = new JPanel();
        for (int i=0; i<10; i++){

        }
    }

    //panel for line 1 of keyboard buttons - Q-P
    protected JComponent getPanelLine2(){

    }

    //panel for line 1 of keyboard buttons - A-L
    protected JComponent getPanelLine3(){

    }![enter image description here][1]

    //panel for line 1 of keyboard buttons - Z-M    
    protected JComponent getPanelLine4(){

    }

    //panel for line 1 of keyboard buttons - space and arrows
    protected JComponent getPanelLine5(){

    }*/
}

图像: my keyborad

1 个答案:

答案 0 :(得分:1)

正如here所述,请勿设置首选尺寸!

// button[row][column].setPreferredSize(new Dimension(key[row][column].length()+80,30));

而是设置边距

button[row][column].setMargin(new Insets(10, 20, 10, 20));
                       // Insets ( top, left, bottom, right )

和/或设置字体大小

Font font = button[row][column].getFont();
String family = font.getFamily();
int style = font.getStyle();
button[row][column].setFont(new Font(family, style, 24));

您可能不希望每次迭代都创建一个新字体,但它只是向您展示如何从按钮中获取默认值

执行上述任何一项操作都会相应地增加面板的首选大小,同时考虑到其中包含的文本的大小