Import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>title: Using HTML language </p>
in swing
* <p>description: This demonstrates the use of HTML language to construct display information on swing panels </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:HtmlDemo.java</p>
* @version 1.0
*/
public class Htmldemo extends JPanel
implements ActionListener {
JLabel Thelabel;
JTextArea HtmlTextArea;
/**
*<br> Method Description: Constructor, which describes the members of a form
*<br> input Parameters:
*<br> return type:
*/
public Htmldemo () {
setlayout (The new BoxLayout (this, boxlayout.line_axis));
String initialtext = "<html>\n" +
"Color and font test: \ n" +
"<ul>\n" +
"<li><font color=red>red</font>\n" +
"<li><font color=blue>blue</font>\n" +
"<li><font color=green>green</font>\n" +
"<li><font size=-2>small</font>\n" +
"<li><font size=+2>large</font>\n" +
"<li><i>italic</i>\n" +
"<li><b>bold</b>\n" +
"</ul>\n";
//Define a text box
HtmlTextArea = new JTextArea (10, 20);
Htmltextarea.settext (Initialtext);
JScrollPane ScrollPane = new JScrollPane (HtmlTextArea);
//Definition button
JButton Changethelabel = new JButton ("Change display");
changethelabel.setmnemonic (Keyevent.vk_c);
Changethelabel.setalignmentx (component.center_alignment);
Changethelabel.addactionlistener (this);
//Definition label
Thelabel = new JLabel (initialtext) {
public Dimension getpreferredsize () {
return new Dimension (200, 200);
}
public Dimension getminimumsize () {
return new Dimension (200, 200);
}
public Dimension getmaximumsize () {
return new Dimension (200, 200);
}
};
//sets the alignment of the label
thelabel.setverticalalignment (Swingconstants.center);
thelabel.sethorizontalalignment (Swingconstants.center);
//Constructs a border-left edit panel
JPanel Leftpanel = new JPanel ();
Leftpanel.setlayout (New BoxLayout (Leftpanel, Boxlayout.page_axis));
Leftpanel.setborder (Borderfactory.createcompoundborder (
Borderfactory.createtitledborder (
edit HTML, click the button to display the results. "),
Borderfactory.createemptyborder (10,10,10,10)));
Leftpanel.add (ScrollPane);
Leftpanel.add (Box.createrigidarea (New Dimension (0,10)));
Leftpanel.add (Changethelabel);
//Construct a panel with a border to the right display
JPanel Rightpanel = new JPanel ();
rightpanel.setlayout (New BoxLayout (Rightpanel, Boxlayout.page_axis));
Rightpanel.setborder (Borderfactory.createcompoundborder (
Borderfactory.createtitledborder ("Show HTML results here using tags"),
Borderfactory.createemptyborder (10,10,10,10)));
Rightpanel.add (Thelabel);
SetBorder (Borderfactory.createemptyborder (10,10,10,10));
Add (Leftpanel);
Add (Box.createrigidarea (new Dimension (10,0)));
Add (Rightpanel);
}
/**
*<br> Method Description: Event listener, when the user clicks on the button to trigger the
*<br> input Parameters:
*<br> return type:
*/
public void actionperformed (ActionEvent e) {
Thelabel.settext (Htmltextarea.gettext ());
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
jframe.setdefaultlookandfeeldecorated (TRUE);
//Create form
JFrame frame = new JFrame ("Htmldemo");
frame.setdefaultcloseoperation (jframe.exit_on_close);
//Create panel
JComponent Newcontentpane = new Htmldemo ();
Newcontentpane.setopaque (TRUE);
Frame.setcontentpane (Newcontentpane);
//Display form
Frame.pack ();
frame.setvisible (TRUE);
}
}