⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swing对jtextpane中字体颜色的设置 - swing - blogjava.mht

📁 叫您如何实现部分字体的改变
💻 MHT
📖 第 1 页 / 共 5 页
字号:
#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>&nbsp;private JPanel jp1;</P>
<P style=3D"COLOR: #008000">&nbsp;private JButton color;</P>
<P style=3D"COLOR: #008000">&nbsp;private JTextPane jep;</P>
<P style=3D"COLOR: #008000">&nbsp;private JScrollPane jsp;</P>
<P style=3D"COLOR: #008000">&nbsp;private JButton font;</P>
<P style=3D"COLOR: #008000">&nbsp;/**<BR>&nbsp; * Auto-generated main =
method to=20
display this JFrame<BR>&nbsp; */<BR>&nbsp;public static void =
main(String[] args)=20
{<BR>&nbsp;&nbsp;NewJFrame inst =3D new=20
NewJFrame();<BR>&nbsp;&nbsp;inst.setVisible(true);<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;public NewJFrame()=20
{<BR>&nbsp;&nbsp;super();<BR>&nbsp;&nbsp;initGUI();<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;private void initGUI() =
{<BR>&nbsp;&nbsp;try=20
{<BR>&nbsp;&nbsp;&nbsp;BorderLayout thisLayout =3D new=20
BorderLayout();<BR>&nbsp;&nbsp;&nbsp;getContentPane().setLayout(thisLayou=
t);<BR>&nbsp;&nbsp;&nbsp;setDefaultCloseOperation(WindowConstants.DISPOSE=
_ON_CLOSE);<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;jp1=20
=3D new JPanel();<BR>&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().add(jp1,=20
BorderLayout.NORTH);<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;font=20
=3D new=20
JButton();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font.addActionListener(this);=
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp1.add(font);<BR>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;font.setText("font");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbs=
p;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=20
=3D new=20
JButton();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp1.add(color);<BR>&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;color.addActionListener(this);<BR>&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;color.setText("color");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;=
&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;jsp=20
=3D new =
JScrollPane();<BR>&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().add(jsp,=20
BorderLayout.CENTER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;jep=20
=3D new=20
JTextPane();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsp.setViewportView(jep);<B=
R>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jep.setDocument(new=20
DefaultStyledDocument());<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nb=
sp;}<BR>&nbsp;&nbsp;&nbsp;pack();<BR>&nbsp;&nbsp;&nbsp;setSize(400,=20
300);<BR>&nbsp;&nbsp;} catch (Exception e)=20
{<BR>&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR>&nbsp;&nbsp;}<BR>&nbsp;}</=
P>
<P style=3D"COLOR: #008000">&nbsp;public static void =
setFontSize(JEditorPane=20
editor, int size) {<BR>&nbsp;&nbsp;if (editor !=3D null) =
{<BR>&nbsp;&nbsp;&nbsp;if=20
((size &gt; 0) &amp;&amp; (size &lt; 512))=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;MutableAttributeSet attr =3D new=20
SimpleAttributeSet();<BR>&nbsp;&nbsp;&nbsp;&nbsp;StyleConstants.setFontSi=
ze(attr,=20
size);<BR>&nbsp;&nbsp;&nbsp;&nbsp;setCharacterAttributes(editor, attr,=20
false);<BR>&nbsp;&nbsp;&nbsp;} else=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;UIManager.getLookAndFeel().provideErrorFeedb=
ack(editor);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;public static void =
setForeground(JEditorPane=20
editor, Color fg) {<BR>&nbsp;&nbsp;if (editor !=3D null) =
{<BR>&nbsp;&nbsp;&nbsp;if=20
(fg !=3D null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;MutableAttributeSet attr =3D =
new=20
SimpleAttributeSet();<BR>&nbsp;&nbsp;&nbsp;&nbsp;StyleConstants.setForegr=
ound(attr,=20
fg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;setCharacterAttributes(editor, attr,=20
false);<BR>&nbsp;&nbsp;&nbsp;} else=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;UIManager.getLookAndFeel().provideErrorFeedb=
ack(editor);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;public static final void=20
setCharacterAttributes(JEditorPane =
editor,<BR>&nbsp;&nbsp;&nbsp;AttributeSet=20
attr, boolean replace) {<BR>&nbsp;&nbsp;int p0 =3D=20
editor.getSelectionStart();<BR>&nbsp;&nbsp;int p1 =3D=20
editor.getSelectionEnd();<BR>&nbsp;&nbsp;if (p0 !=3D p1)=20
{<BR>&nbsp;&nbsp;&nbsp;StyledDocument doc =3D=20
getStyledDocument(editor);<BR>&nbsp;&nbsp;&nbsp;doc.setCharacterAttribute=
s(p0,=20
p1 - p0, attr, replace);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;StyledEditorKit =
k =3D=20
getStyledEditorKit(editor);<BR>&nbsp;&nbsp;MutableAttributeSet =
inputAttributes =3D=20
k.getInputAttributes();<BR>&nbsp;&nbsp;if (replace)=20
{<BR>&nbsp;&nbsp;&nbsp;inputAttributes.removeAttributes(inputAttributes);=
<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;inputAttributes.addAttributes(attr);<BR>=
&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;protected static final StyledDocument=20
getStyledDocument(JEditorPane e) {<BR>&nbsp;&nbsp;Document d =3D=20
e.getDocument();<BR>&nbsp;&nbsp;if (d instanceof StyledDocument)=20
{<BR>&nbsp;&nbsp;&nbsp;return (StyledDocument)=20
d;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;throw new =
IllegalArgumentException("document=20
must be StyledDocument");<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;protected static final StyledEditorKit =

getStyledEditorKit(JEditorPane e) {<BR>&nbsp;&nbsp;EditorKit k =3D=20
e.getEditorKit();<BR>&nbsp;&nbsp;if (k instanceof StyledEditorKit)=20
{<BR>&nbsp;&nbsp;&nbsp;return (StyledEditorKit)=20
k;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;throw new =
IllegalArgumentException("EditorKit=20
must be StyledEditorKit");<BR>&nbsp;}</P>
<P style=3D"COLOR: #008000">&nbsp;public void =
actionPerformed(ActionEvent e)=20
{<BR>&nbsp;&nbsp;Object obj =3D e.getSource();<BR>&nbsp;&nbsp;if (obj =
=3D=3D font)=20
{<BR>&nbsp;&nbsp;&nbsp;JEditorPane editor =3D=20
jep;<BR>&nbsp;&nbsp;&nbsp;setFontSize(editor,=20
20);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;if (obj =3D=3D color)=20
{<BR>&nbsp;&nbsp;&nbsp;JEditorPane editor =3D=20
jep;<BR>&nbsp;&nbsp;&nbsp;setForeground(editor,=20
Color.red);<BR>&nbsp;&nbsp;}<BR>&nbsp;}</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>&nbsp;&nbsp;&nbsp;&nbsp;StyleConstants.setFontFa=
mily(attr,=20
family);<BR>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;StyleConstants.setFontSi=
ze(attr,=20
size);<BR>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR>&nbsp;&nbsp;&nbsp;boolean bold =3D=20
(StyleConstants.isBold(attr)) ? false :=20
true;<BR>&nbsp;&nbsp;&nbsp;SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR>&nbsp;&nbsp;&nbsp;StyleConstants.setBold(sas,=20
bold);<BR>&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR>&nbsp;&nbsp;&nbsp;boolean italic =3D=20
(StyleConstants.isItalic(attr)) ? false :=20
true;<BR>&nbsp;&nbsp;&nbsp;SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR>&nbsp;&nbsp;&nbsp;StyleConstants.setItalic(sas, =

italic);<BR>&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;MutableAttributeSet =
attr =3D=20
kit.getInputAttributes();<BR>&nbsp;&nbsp;&nbsp;boolean underline =3D=20
(StyleConstants.isUnderline(attr)) ? =
false<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:=20
true;<BR>&nbsp;&nbsp;&nbsp;SimpleAttributeSet sas =3D new=20
SimpleAttributeSet();<BR>&nbsp;&nbsp;&nbsp;StyleConstants.setUnderline(sa=
s,=20
underline);<BR>&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;StyleConstants.setAlignment(at=
tr,=20
a);<BR>&nbsp;&nbsp;&nbsp;setParagraphAttributes(editor, attr, =
false);<BR>public=20
static final void setParagraphAttributes(JEditorPane=20
editor,<BR>&nbsp;&nbsp;&nbsp;AttributeSet attr, boolean replace)=20
{<BR>&nbsp;&nbsp;int p0 =3D =
editor.getSelectionStart();<BR>&nbsp;&nbsp;int p1 =3D=20
editor.getSelectionEnd();<BR>&nbsp;&nbsp;StyledDocument doc =3D=20
getStyledDocument(editor);<BR>&nbsp;&nbsp;doc.setParagraphAttributes(p0, =
p1 -=20
p0, attr, =
replace);<BR>&nbsp;}<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 + -