Java formatted string, insufficient to fill space __java

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.