java - On screen Keyboard JButton fit text size exactly -
i wrote on screen keyboard should display keyboard. problem want jbuttons fit size of labels , not set fix size of jbuttons. example numbers on keyboard: "1,2,3...." should small jbutton , keys "backspace" "tab" etc... should bigger (fit size of label)
this code wrote:
package q2; import java.awt.*; import javax.swing.*; public class mainpanel extends jpanel{ private jbutton[][] button; private jpanel[] panel; //array of panels 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 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 parent panel , array of 5 panels , buttons array parent = new jpanel(); parent.setlayout(new gridlayout(0,1)); panel = new jpanel[5]; button = new jbutton[20][20]; (int row = 0; row<key.length; row++){ panel[row] = new jpanel(); (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 line 1 of keyboard buttons - numbers protected jcomponent getpanelline1(){ jpanel panel1 = new jpanel(); (int i=0; i<10; i++){ } } //panel line 1 of keyboard buttons - q-p protected jcomponent getpanelline2(){ } //panel line 1 of keyboard buttons - a-l protected jcomponent getpanelline3(){ }![enter image description here][1] //panel line 1 of keyboard buttons - z-m protected jcomponent getpanelline4(){ } //panel line 1 of keyboard buttons - space , arrows protected jcomponent getpanelline5(){ }*/ } image: 
as discussed here, don't set preferred size!
// button[row][column].setpreferredsize(new dimension(key[row][column].length()+80,30)); instead set margins
button[row][column].setmargin(new insets(10, 20, 10, 20)); // insets ( top, left, bottom, right ) and/or set font size
font font = button[row][column].getfont(); string family = font.getfamily(); int style = font.getstyle(); button[row][column].setfont(new font(family, style, 24)); you don't want create new font every iteration, it's showing how defaults button
doing either of these things increase preferred size of panel accordingly, taking account size of text contained within it
Comments
Post a Comment