시소당
자바로 클라이언트 애플리케이션이나 애플릿을 작성할 때, 빠지지 않는 것이 Swing입니다.
자바 개발자들이 싫어하는 작업 중 하나가 GUI 작업인데, MS의 비주얼 스튜디오와 같은 비주얼 툴이 없기 때문이기도 합니다. 따라서 자바로 하는 GUI 작업은 노가다가 되기 쉽상이죠.
사설은 그만하고 본론을 얘기하자면......
Swing 컴포넌트에 HTML을 사용할 수 있다는 사실을 아는 사람이 그리 많지 않은 것 같습니다.
상당수의 Swing 컴포넌트 들에 HTML을 사용할 수 있는데, JLabel, JButton 등이 좋은 예입니다.
아래는 Sun의 뉴스레터에서 발췌한 것입니다.
==================================================================================
USING HTML IN SWING COMPONENTS
Many of the Swing components support the display of HTML. This tip shows you how to use HTML to add superscripts, to style, and to provide flexible line breaking in text added to Swing components such as JLabels and JButtons. There is surprisingly little that you need to do to achieve more flexible results in the labeling of your Swing components.
Let's start with four JButtons with labels "1st", "2nd", "3rd", and "4th". You can produce the JButtons in the following program:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
public class TextButtons extends JFrame{
TextButtons(){
super("Text Buttons");
setSize(400,200);
getContentPane().setLayout(new GridLayout(1,4));
getContentPane().add(new JButton("1st"));
getContentPane().add(new JButton("2nd"));
getContentPane().add(new JButton("3rd"));
getContentPane().add(new JButton("4th"));
}
public static void main(String[] args) {
new TextButtons().setVisible(true);
}
}
Here is the result of running TextButtons.
textbuttons
Although the buttons display in the correct order and with the correct labels, they might look better if the letters after the numbers (st, nd, rd, and th) were rendered as superscripts. You can do that by enclosing the letters in HTML <sup> tags. You need to begin each input string with an <html> tag, and end it with an </html> tag. You do not need to include the HTML head and body tags. Here is the code for introducing the superscripts (although <p> tags are used in this example, they're not required):
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
public class HTMLButtons extends JFrame{
HTMLButtons(){
setSize(400,200);
getContentPane().setLayout(new GridLayout(1,4));
getContentPane().add(new JButton(
"<html><p>1<sup>st</sup></p></html>"));
getContentPane().add(new JButton(
"<html><p>2<sup>nd</sup></p></html>"));
getContentPane().add(new JButton(
"<html><p>3<sup>rd</sup></p></html>"));
getContentPane().add(new JButton(
"<html><p>4<sup>th</sup></p></html>"));
}
public static void main(String[] args) {
new HTMLButtons().setVisible(true);
}
}
As you can see in this screenshot, you now have superscripts on each of the buttons.
HTMLButtons
Although HTML is most often used in Swing components to style text in a particular way, it can be used with care to handle long lines of text. If you have text that is too long for your JButton, the text that can fit on the button will appear followed by an ellipsis. One solution is to manually add breaks to the code using "\n". These newline characters are ignored in displaying the JButton text. They are respected in JTextAreas, but this is not a good solution even in that in that case because the breaks are now hardcoded. This might look good at the initial size you specify, but the breaks might not be appropriate for larger or smaller sizes.
You can use HTML to get multiple lines in a JButton. You can explicitly use a <br> tag to force a new line. However, for the reasons just mentioned, you're probably better off allowing the text to break automatically, based on the size of the button. This technique should be used with care because a JButton with a really long line of HTML text will have a very wide preferred size, which will affect calls to pack. The next example illustrates the four cases just discussed:
* A button with text that is too long for the display
* A button with text that is too long, but that attempts to use the \n character to force a new line
* A text area where the new line characters are respected
* A a button with HTML formatted text
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.GridLayout;
public class LongNames extends JFrame{
LongNames(){
setSize(400,200);
getContentPane().setLayout(new GridLayout(1,4));
getContentPane().add(new JButton(
"This string is longer than the display."));
getContentPane().add(new JButton(
"This string \n is longer \n than the" +
"\n display"));
getContentPane().add(new JTextArea(
"This string \n is longer \n than the" +
"\n display"));
getContentPane().add(new JButton(
"<html> This string is longer than the " +
"display. </html>"));
}
public static void main(String[] args) {
new LongNames().setVisible(true);
}
}
Here is the result of running this code. If you make the JFrame bigger you will see the line breaks in the final JButton change accordingly.
LongNames
The HTML support for Swing components is still fairly basic. The documentation is clear that HTML 3.2 is targeted. This support should be sufficient for most needs when displaying small amounts of HTML on JButtons and JLabels. You can experiment with some of the standard tags to customize your applications in various ways. In the following example, the superscripts are used inside of a top level heading. Each button also includes a horizontal rule and colored text. One button includes bolded text and another is underlined.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
public class MoreHTMLButtons extends JFrame{
MoreHTMLButtons(){
setSize(300,200);
getContentPane().setLayout(new GridLayout(1,3));
getContentPane().add(new JButton(
"<html><h1>1<sup>st</sup></h1>" +
"<hr> <p color=blue> Use Blue</p></html>"));
getContentPane().add(new JButton(
"<html><h1>2<sup>nd</sup></h1>" +
"<hr> <p color=red>Use <b>Red</b></p></html>"));
getContentPane().add(new JButton(
"<html><h1>3<sup>rd</sup></h1><hr>" +
"<p color=green> Use <u>Green</u></p></html>"));
}
public static void main(String[] args) {
new MoreHTMLButtons().setVisible(true);
}
}
The new customized buttons look like this.
morehtmlbuttons
It would be much easier if you could use a style sheet with your HTML for the various Swing components. For example, you could create a style sheet file named jbutton.css with the following contents:
p {
color: red;
}
In a standard HTML document you could provide a link to this style sheet with a command like this.
<link href="./jbutton.css" rel="stylesheet" type="text/css">
If you try to insert this code into the HTML used to instantiate your JButton, the URL will not know how to resolve the link. You will not get the expected behavior because your JButton will not be able to find the file jbutton.css. Instead, specify the location of the style sheet by calling getResource() and pass in the name of the file jbutton.css. Here is an example of using a style sheet to color the text on the middle button:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
public class StyleSheets extends JFrame {
private static final String HTML_HEAD = "<head>" +
"<link rel=STYLESHEET TYPE=\"text/css\" HREF=\"" +
StyleSheets.class.getResource("jbutton.css") +
"\"></head>";
StyleSheets() {
setSize(300, 200);
getContentPane().setLayout(new GridLayout(1, 3));
getContentPane().add(new JButton(
"<html><h1>1<sup>st</sup></h1>" +
"<hr> <p color=blue> Use Blue</p></html>"));
getContentPane().add(new JButton( "<html>" +
HTML_HEAD + // reference the style sheet
"<h1>2<sup>nd</sup></h1>" +
"<hr> <p>Use <b>Red</b></p></html>"));
getContentPane().add(new JButton(
"<html><h1>3<sup>rd</sup></h1><hr>" +
"<p color=green> Use <u>Green</u></p></html>"));
}
public static void main(String[] args) {
new StyleSheets().setVisible(true);
}
}
When you run this sample you will see that the first button and third buttons are colored appropriately using HTML. The second button calls a .css file, and uses it to set the color. The result is the same as it was when you ran MoreHTMLButtons.
Here is the result of running this code.
[출처] HTML을 사용하여, Swing 컴포넌트를 꾸미자.|작성자 재학
http://blog.naver.com/7loveletter7?Redirect=Log&logNo=7277379