📄 swing对jtextpane中字体颜色的设置 - swing - blogjava.mht
字号:
#ffff66">java</B>2=CD=BC=D0=CE=C9=E8=BC=C6=BE=ED2 swing=20
<LI style=3D"MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--zht =
</LI></UL></DIV>
<H3>=D4=C4=B6=C1=C5=C5=D0=D0=B0=F1</H3>
<DIV class=3DRecentComment>
<UL style=3D"WORD-BREAK: break-all">
<LI><A id=3DSingleColumn1__eae480_TopList_ctl01_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/05/26/120141.html">1.?=
<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>=BC=FC=C5=CC=CA=C2=BC=FE(1613)</A>=20
<LI><A id=3DSingleColumn1__eae480_TopList_ctl02_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/04/27/113869.html">2.?=
=D3=C3Swing=D0=B4=B5=C4=B7=C2jfreechart=B5=C4=C7=FA=CF=DF=C3=E6=B0=E5=BC=B0=
=D4=B4=C2=EB(1566)</A>=20
<LI><A id=3DSingleColumn1__eae480_TopList_ctl03_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/04/26/113732.html">3.?=
=B9=D8=D3=DASwing(1464)</A>=20
<LI><A id=3DSingleColumn1__eae480_TopList_ctl04_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/05/08/115898.html">4.?=
<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: #ffff66">Java</B> Web Start=20
=D1=A7=CF=B0(1164)</A>=20
<LI><A id=3DSingleColumn1__eae480_TopList_ctl05_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/05/29/120591.html">5.?=
<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>2d=CC=D8=D0=A7 =CE=C4=D7=D6=D0=FD=D7=AA(1083)</A>=20
</LI></UL></DIV>
<H3>=C6=C0=C2=DB=C5=C5=D0=D0=B0=F1</H3>
<DIV class=3DRecentComment>
<UL style=3D"WORD-BREAK: break-all">
<LI><A id=3DSingleColumn1__1a3d643_TopList_ctl01_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/04/26/113732.html">1.?=
=B9=D8=D3=DASwing(23)</A>=20
<LI><A id=3DSingleColumn1__1a3d643_TopList_ctl02_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/04/27/113869.html">2.?=
=D3=C3Swing=D0=B4=B5=C4=B7=C2jfreechart=B5=C4=C7=FA=CF=DF=C3=E6=B0=E5=BC=B0=
=D4=B4=C2=EB(13)</A>=20
<LI><A id=3DSingleColumn1__1a3d643_TopList_ctl03_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/09/15/145335.html">3.?=
=C8=ED=BC=FE=BF=AA=B7=A2#=C7=B0=CD=BE(12)</A>=20
<LI><A id=3DSingleColumn1__1a3d643_TopList_ctl04_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/10/18/153792.html">4.?=
Swing=C9=EE=C8=EB=D1=A7=CF=B0(4)</A>=20
<LI><A id=3DSingleColumn1__1a3d643_TopList_ctl05_Hyperlink1=20
=
href=3D"http://www.blogjava.net/Swing/archive/2007/11/13/160220.html">5.?=
Swing=20
GUI=C8=BA22069957 (3)</A> </LI></UL></DIV></DIV></DIV>
<DIV id=3Dcentercontent><!--done-->
<DIV class=3Dpost>
<DIV class=3DpostTitle><A class=3DpostTitle2 id=3Dviewpost1_TitleUrl=20
href=3D"http://www.blogjava.net/Swing/archive/2007/07/09/128965.html">Swi=
ng=B6=D4JTextPane=D6=D0<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#99ff99">=D7=D6=CC=E5=D1=D5=C9=AB</B>=B5=C4<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: #a0ffff">=C9=E8=D6=C3</B></A> =
</DIV><SPAN=20
style=3D"COLOR: #008000">import <B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>.awt.BorderLayout;<BR>import=20
<B style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>.awt.Color;<BR>import=20
<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>.awt.event.ActionEvent;<BR>import=20
<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#ffff66">java</B>.awt.event.ActionListener;</SPAN>=20
<P style=3D"COLOR: #008000">import javax.swing.JButton;<BR>import=20
javax.swing.JEditorPane;<BR>import javax.swing.JPanel;<BR>import=20
javax.swing.JScrollPane;<BR>import javax.swing.JTextPane;<BR>import=20
javax.swing.UIManager;<BR>import javax.swing.WindowConstants;<BR>import=20
javax.swing.text.AttributeSet;<BR>import=20
javax.swing.text.DefaultStyledDocument;<BR>import=20
javax.swing.text.Document;<BR>import =
javax.swing.text.EditorKit;<BR>import=20
javax.swing.text.MutableAttributeSet;<BR>import=20
javax.swing.text.SimpleAttributeSet;<BR>import=20
javax.swing.text.StyleConstants;<BR>import=20
javax.swing.text.StyledDocument;<BR>import =
javax.swing.text.StyledEditorKit;</P>
<P style=3D"COLOR: #008000">public class NewJFrame extends =
javax.swing.JFrame=20
implements ActionListener {<BR> private JPanel jp1;</P>
<P style=3D"COLOR: #008000"> private JButton color;</P>
<P style=3D"COLOR: #008000"> private JTextPane jep;</P>
<P style=3D"COLOR: #008000"> private JScrollPane jsp;</P>
<P style=3D"COLOR: #008000"> private JButton font;</P>
<P style=3D"COLOR: #008000"> /**<BR> * Auto-generated main =
method to=20
display this JFrame<BR> */<BR> public static void =
main(String[] args)=20
{<BR> NewJFrame inst =3D new=20
NewJFrame();<BR> inst.setVisible(true);<BR> }</P>
<P style=3D"COLOR: #008000"> public NewJFrame()=20
{<BR> super();<BR> initGUI();<BR> }</P>
<P style=3D"COLOR: #008000"> private void initGUI() =
{<BR> try=20
{<BR> BorderLayout thisLayout =3D new=20
BorderLayout();<BR> getContentPane().setLayout(thisLayou=
t);<BR> setDefaultCloseOperation(WindowConstants.DISPOSE=
_ON_CLOSE);<BR> {<BR> jp1=20
=3D new JPanel();<BR> getContentPane().add(jp1,=20
BorderLayout.NORTH);<BR> {<BR> &n=
bsp; font=20
=3D new=20
JButton();<BR> font.addActionListener(this);=
<BR> jp1.add(font);<BR> &nb=
sp; font.setText("font");<BR> }<BR> &nbs=
p; {<BR> color=20
=3D new=20
JButton();<BR> jp1.add(color);<BR> &nbs=
p; color.addActionListener(this);<BR> &=
nbsp; color.setText("color");<BR> }<BR> =
}<BR> {<BR> jsp=20
=3D new =
JScrollPane();<BR> getContentPane().add(jsp,=20
BorderLayout.CENTER);<BR> {<BR> &=
nbsp; jep=20
=3D new=20
JTextPane();<BR> jsp.setViewportView(jep);<B=
R> jep.setDocument(new=20
DefaultStyledDocument());<BR> }<BR> &nb=
sp;}<BR> pack();<BR> setSize(400,=20
300);<BR> } catch (Exception e)=20
{<BR> e.printStackTrace();<BR> }<BR> }</=
P>
<P style=3D"COLOR: #008000"> public static void =
setFontSize(JEditorPane=20
editor, int size) {<BR> if (editor !=3D null) =
{<BR> if=20
((size > 0) && (size < 512))=20
{<BR> MutableAttributeSet attr =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setFontSi=
ze(attr,=20
size);<BR> setCharacterAttributes(editor, attr,=20
false);<BR> } else=20
{<BR> UIManager.getLookAndFeel().provideErrorFeedb=
ack(editor);<BR> }<BR> }<BR> }</P>
<P style=3D"COLOR: #008000"> public static void =
setForeground(JEditorPane=20
editor, Color fg) {<BR> if (editor !=3D null) =
{<BR> if=20
(fg !=3D null) {<BR> MutableAttributeSet attr =3D =
new=20
SimpleAttributeSet();<BR> StyleConstants.setForegr=
ound(attr,=20
fg);<BR> setCharacterAttributes(editor, attr,=20
false);<BR> } else=20
{<BR> UIManager.getLookAndFeel().provideErrorFeedb=
ack(editor);<BR> }<BR> }<BR> }</P>
<P style=3D"COLOR: #008000"> public static final void=20
setCharacterAttributes(JEditorPane =
editor,<BR> AttributeSet=20
attr, boolean replace) {<BR> int p0 =3D=20
editor.getSelectionStart();<BR> int p1 =3D=20
editor.getSelectionEnd();<BR> if (p0 !=3D p1)=20
{<BR> StyledDocument doc =3D=20
getStyledDocument(editor);<BR> doc.setCharacterAttribute=
s(p0,=20
p1 - p0, attr, replace);<BR> }<BR> StyledEditorKit =
k =3D=20
getStyledEditorKit(editor);<BR> MutableAttributeSet =
inputAttributes =3D=20
k.getInputAttributes();<BR> if (replace)=20
{<BR> inputAttributes.removeAttributes(inputAttributes);=
<BR> }<BR> inputAttributes.addAttributes(attr);<BR>=
}</P>
<P style=3D"COLOR: #008000"> protected static final StyledDocument=20
getStyledDocument(JEditorPane e) {<BR> Document d =3D=20
e.getDocument();<BR> if (d instanceof StyledDocument)=20
{<BR> return (StyledDocument)=20
d;<BR> }<BR> throw new =
IllegalArgumentException("document=20
must be StyledDocument");<BR> }</P>
<P style=3D"COLOR: #008000"> protected static final StyledEditorKit =
getStyledEditorKit(JEditorPane e) {<BR> EditorKit k =3D=20
e.getEditorKit();<BR> if (k instanceof StyledEditorKit)=20
{<BR> return (StyledEditorKit)=20
k;<BR> }<BR> throw new =
IllegalArgumentException("EditorKit=20
must be StyledEditorKit");<BR> }</P>
<P style=3D"COLOR: #008000"> public void =
actionPerformed(ActionEvent e)=20
{<BR> Object obj =3D e.getSource();<BR> if (obj =
=3D=3D font)=20
{<BR> JEditorPane editor =3D=20
jep;<BR> setFontSize(editor,=20
20);<BR> }<BR> if (obj =3D=3D color)=20
{<BR> JEditorPane editor =3D=20
jep;<BR> setForeground(editor,=20
Color.red);<BR> }<BR> }</P>
<P style=3D"COLOR: =
#008000">}<BR>=C6=E4=CB=FB=B2=D9=D7=F7=C8=E7=CF=C2=A3=BA<BR>1=A1=A2=B6=D4=
=D7=D6=CC=E5=B5=C4=B2=D9=D7=F7 <BR>MutableAttributeSet attr=20
=3D new=20
SimpleAttributeSet();<BR> StyleConstants.setFontFa=
mily(attr,=20
family);<BR> setCharacterAttributes(editor, attr, =
false);=20
<BR>family=CE=AA=D7=D6=CC=E5<BR>2=A1=A2=B6=D4=D7=D6=CC=E5=B4=F3=D0=A1=B5=C4=
=B2=D9=D7=F7<BR>MutableAttributeSet attr =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setFontSi=
ze(attr,=20
size);<BR> setCharacterAttributes(editor, attr,=20
false);<BR>size=CE=AA=D7=D6=BA=C5<BR>3=A1=A2=CA=C7=B7=F1=CA=C7=B4=D6=CC=E5=
=B5=C4=B2=D9=D7=F7<BR>StyledEditorKit kit =3D=20
getStyledEditorKit(editor);<BR> MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR> boolean bold =3D=20
(StyleConstants.isBold(attr)) ? false :=20
true;<BR> SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setBold(sas,=20
bold);<BR> setCharacterAttributes(editor, sas,=20
false);<BR>4=A1=A2=CA=C7=B7=F1=CA=C7=D0=B1=CC=E5=B5=C4=B2=D9=D7=F7<BR>Sty=
ledEditorKit kit =3D=20
getStyledEditorKit(editor);<BR> MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR> boolean italic =3D=20
(StyleConstants.isItalic(attr)) ? false :=20
true;<BR> SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setItalic(sas, =
italic);<BR> setCharacterAttributes(editor, sas,=20
false);<BR>5=A1=A2=CA=C7=B7=F1=D3=D0=CF=C2=BB=AE=CF=DF=B5=C4=B2=D9=D7=F7<=
BR>StyledEditorKit kit =3D=20
getStyledEditorKit(editor);<BR> MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR> boolean underline =3D=20
(StyleConstants.isUnderline(attr)) ? =
false<BR> :=20
true;<BR> SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setUnderline(sa=
s,=20
underline);<BR> setCharacterAttributes(editor, sas,=20
false);<BR>6=A1=A2=D7=F3=D6=D0=D3=D2=B6=D4=C6=EB=B5=C4=B4=A6=C0=ED<BR>Mut=
ableAttributeSet attr =3D new=20
SimpleAttributeSet();<BR> StyleConstants.setAlignment(at=
tr,=20
a);<BR> setParagraphAttributes(editor, attr, =
false);<BR>public=20
static final void setParagraphAttributes(JEditorPane=20
editor,<BR> AttributeSet attr, boolean replace)=20
{<BR> int p0 =3D =
editor.getSelectionStart();<BR> int p1 =3D=20
editor.getSelectionEnd();<BR> StyledDocument doc =3D=20
getStyledDocument(editor);<BR> doc.setParagraphAttributes(p0, =
p1 -=20
p0, attr, =
replace);<BR> }<BR>a:0=A3=BA=D7=F3=A3=AC1=A3=BA=D6=D0=A3=AC2=A3=BA=D3=
=D2</P><SPAN=20
style=3D"COLOR: #008000">7=A1=A2=CE=C4=B1=BE<B=20
style=3D"COLOR: black; BACKGROUND-COLOR: =
#99ff99">=D7=D6=CC=E5=D1=D5=C9=AB</B>=B5=C4<B=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -