当前位置: 代码迷 >> Java相关 >> 记事本中的内容选定后怎样设置字体颜色,
  详细解决方案

记事本中的内容选定后怎样设置字体颜色,

热度:135   发布时间:2006-09-09 22:02:46.0
哎...JTextArea是不可能单独为选定的字体设置属性的


----------------解决方案--------------------------------------------------------
那请问可以为全部的字设置字体和颜色吗????????
请高手指点!
----------------解决方案--------------------------------------------------------
这个容易哈,不过偶今天要上课~~能不能晚点回答?
----------------解决方案--------------------------------------------------------
那就先谢谢了
不过想问一下实现各种字体的方法能放在同一个类中吗?
就象
class fontL implements ActionListener{ //设置字体
public void actionPerformed(ActionEvent e){
for(int i=0;i<fontItems.length;i++){
String selection = t1.getSelectedText();
if(e.getSource()==plain)
t1.setFont(fontItems[i]); 这几个实现将选定的字变为相应的字体可是不正确呀 else if(e.getSource()==bold)
selection.setFont(fontItems[i]);
else if(e.getSource()==italic)
selection.setFont(fontItems[i]);
else if(e.getSource()==boldItalic)
selection.setFont(fontItems[i]);
}
repaint();
}
}
}

----------------解决方案--------------------------------------------------------

如果你想为你的文字增加别的特色的话,JTextArea是不行的,你至少也得用一个JEditorPane或者JTextPane才可以


----------------解决方案--------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class Menuout3 extends JFrame{
private ButtonGroup colorGroup,fontGroup;
private JTextArea t1;
private Color colorValue[]={Color.black,Color.blue,Color.red,Color.green};
//private Font fontValue[]={Font.PLAIN,Font.BOLD,Font.ITALIC};
//实例化剪切板
Clipboard clipbd = getToolkit().getSystemClipboard();
JMenuItem colorItems[],fontItems[];
public Menuout3(){
super("记事本");
Container c=getContentPane();
c.setLayout(new BorderLayout());
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
JMenu filemenu=new JMenu("文件");
JMenuItem filenew=new JMenuItem("新建");
JMenuItem fileopen=new JMenuItem("打开");
JMenuItem filesave=new JMenuItem("保存");
JMenuItem filelsave=new JMenuItem("另存为");
JMenuItem fileshezhi=new JMenuItem("设置");
JMenuItem exit=new JMenuItem("退出");
filemenu.add(filenew);
filemenu.add(fileopen);
filemenu.addSeparator();
filemenu.add(filesave);
filemenu.add(filelsave);
filemenu.addSeparator();
filemenu.add(fileshezhi);
filemenu.add(exit);
bar.add(filemenu);
JMenu bianjimenu=new JMenu("编辑");
JMenuItem fileselall=new JMenuItem("全选");
JMenuItem filecopy=new JMenuItem("复制");
JMenuItem filepaste=new JMenuItem("粘贴");
JMenuItem filecut=new JMenuItem("剪切");
JMenuItem filedel=new JMenuItem("删除");
bianjimenu.add(fileselall);
bianjimenu.add(filecopy);
bianjimenu.add(filepaste);
bianjimenu.add(filecut);
bianjimenu.add(filedel);
bar.add(bianjimenu);
JMenu formatmenu=new JMenu("格式");
String colors[]={"black","blue","red","green"};
JMenu colorMenu=new JMenu("字体颜色");
colorItems=new JMenuItem[colors.length];
colorGroup=new ButtonGroup();
for(int i=0;i<colors.length;i++){
colorItems[i]=new JMenuItem(colors[i]);
colorMenu.add(colorItems[i]);
colorGroup.add(colorItems[i]);
}
String font[]={"plain","bold","italic","boldItalic"};
JMenu fontMenu=new JMenu("字体");
fontItems=new JMenuItem[font.length];
fontGroup=new ButtonGroup();
for(int i=0;i<font.length;i++){
fontItems[i]=new JMenuItem(font[i]);
fontMenu.add(fontItems[i]);
fontGroup.add(fontItems[i]);
}
JMenuItem autohuan=new JMenuItem("自动换行");
formatmenu.add(fontMenu);
formatmenu.add(colorMenu);
formatmenu.add(autohuan);
bar.add(formatmenu);
fileopen.addActionListener(new openL());
filelsave.addActionListener(new lsaveL());
filesave.addActionListener(new saveL());
filenew.addActionListener(new newL());
exit.addActionListener(new exitL());
fileselall.addActionListener(new selallL());
filecopy.addActionListener(new copyL());
filepaste.addActionListener(new pasteL());
filecut.addActionListener(new cutL());
filedel.addActionListener(new delL());
colorItems[0].addActionListener(new colorL());
colorItems[1].addActionListener(new colorL());
colorItems[2].addActionListener(new colorL());
colorItems[3].addActionListener(new colorL());
fontItems[0].addActionListener(new fontL());
fontItems[1].addActionListener(new fontL());
fontItems[2].addActionListener(new fontL());
autohuan.addActionListener(new autoL());
String s="这是一个字符串。";
t1=new JTextArea(s,15,53);
c.add(new JScrollPane(t1,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));
//设置JScrollPane对象的滚动条为水平的始终显现,垂直的是当垂直方向上内容大于显示区域时显示
setSize(600,400);
show();
}
public static void main(String args[]){
Menuout3 a=new Menuout3();
a.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class lsaveL implements ActionListener //另存为
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(Menuout3.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String savefile = fc.getSelectedFile().getPath();
if(savefile == null)
{
return;
}
else
{
String docToSave = t1.getText();
if(docToSave != null)
{
FileOutputStream fstrm = null;
BufferedOutputStream ostrm = null;
try
{
fstrm = new FileOutputStream(savefile);
ostrm = new BufferedOutputStream(fstrm);
byte[] bytes = null;
try
{
bytes = docToSave.getBytes();
}
catch(Exception e1)
{
e1.printStackTrace();
}
ostrm.write(bytes);
}
catch(IOException io)
{
System.err.println("IOException: " +
io.getMessage());
}
finally
{
try
{
ostrm.flush();
fstrm.close();
ostrm.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " +
ioe.getMessage());
}
}
}
}
}
else
{
return;
}
}
}
class saveL implements ActionListener //保存
{
public void actionPerformed(ActionEvent e)
{
}
}

class newL implements ActionListener //新建
{
public void actionPerformed(ActionEvent e)
{
t1.setDocument(new PlainDocument());
}
}
class openL implements ActionListener //打开
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(Menuout3.this, "打开");
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String file = fc.getSelectedFile().getPath();
if(file == null)
{
return;
}
try
{
Reader in = new FileReader(file);
char[] buff = new char[4096];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
{
t1.setDocument(new PlainDocument());
t1.append(new String(buff, 0, nch));
}
}
catch (IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
else
{
return;
}
}
}
class copyL implements ActionListener //复制
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
}
}
class cutL implements ActionListener //剪切
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
t1.replaceRange("", t1.getSelectionStart(),
t1.getSelectionEnd());
}
}
class pasteL implements ActionListener //粘贴
{
public void actionPerformed(ActionEvent e)
{
Transferable clipData = clipbd.getContents(Menuout3.this);
try
{
String clipString =
(String)clipData.getTransferData(
DataFlavor.stringFlavor);
t1.replaceRange(clipString,
t1.getSelectionStart(), t1.getSelectionEnd());
}
catch(Exception ex)
{
}
}
}
class delL implements ActionListener //删除
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
t1.replaceRange("", t1.getSelectionStart(),
t1.getSelectionEnd());
}
}
class selallL implements ActionListener //全部选择
{
public void actionPerformed(ActionEvent e)
{
t1.selectAll();
}
}
class autoL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//自动换行
}
}

class exitL implements ActionListener{ //退出
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class fontL implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==fontItems[0])
t1.setFont(new Font("黑体",Font.PLAIN,22));
else if(e.getSource()==fontItems[1])
t1.setFont(new Font("黑体",Font.BOLD,22));
else if(e.getSource()==fontItems[2])
t1.setFont(new Font("黑体",Font.ITALIC,22));
else
t1.setFont(new Font("黑体",Font.BOLD+Font.ITALIC,22));
}
} //设置字体与上面的等价
class colorL implements ActionListener{ //设置字的颜色
public void actionPerformed(ActionEvent e){
for(int i=0;i<colorItems.length;i++){
if(e.getSource()==colorItems[i])
t1.setForeground(colorValue[i]);
}
repaint();
}
}
}
现在就自动换行这个功能没实现,
希望大家多提意见!


----------------解决方案--------------------------------------------------------
setLineWrap(boolean wrap)
----------------解决方案--------------------------------------------------------
汗,我都忘记了..
----------------解决方案--------------------------------------------------------
     我是初学者,做的不是很好,望大家多提意见.改进中
----------------解决方案--------------------------------------------------------
保存功能没有做吧,saveL模块是空的~~
----------------解决方案--------------------------------------------------------
  相关解决方案