There were a lot of buttons in one of the panels in swing, in order to display the state of the machine, the abnormal red alarm icon, the normal is green, and then because the text is by the machine name, the machine name is different, plus the front of the text icon, so that the whole panel on the button look messy, decided to adjust. So I wrote a method to format the string as 8 bytes (not enough to complement half the space), the code is public static final int strlength = 8;
public static string Formatbuttontext (String targetstr) ... {
int curlength = Targetstr.getbytes (). length;
if (targetstr!=null && curlength>strlength)
Targetstr = Substringbyte (TARGETSTR);
String newstring = "";
int cutlength = Strlength-targetstr.getbytes (). length;
for (int i=0;i<cutlength;i++)
NewString + = "/b";
return targetstr+newstring;
}
public static string Substringbyte (String targetstr) ... {
while (Targetstr.getbytes (). Length>strlength)
Targetstr = targetstr.substring (0,targetstr.length ()-1);
return targetstr;
}
Since my format length here is OK, it is directly defined as the final variable, and if used elsewhere, you can use parameters to pass the desired length ... Hey, but the result, JButton in metal style under the space does not work, directly ignored, in the Windows style has a role, depressed in ...
Metal effect is as follows
Windows style effects are as follows
If you don't see the image effect, you can http://blog.mop.com/cleverfoxloving/2007/03/14/3529726.html another blog to see