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

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

热度:564   发布时间:2006-09-08 16:39:41.0
记事本中的内容选定后怎样设置字体颜色,
我做了一个记事本程序
想实现将记事本中的内容选定后设置字体颜色,
该调用哪个方法????
class redL implements ActionListener //设置为红色
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
selection.?????????????
}
}
怎么设置
清高手指点
谢谢了!
搜索更多相关的解决方案: 字体  记事本  颜色  设置  

----------------解决方案--------------------------------------------------------
怎么没人回答?

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

----------------解决方案--------------------------------------------------------
请问,你的windows记事本的内容选定后能做到你上述的功能么?

----------------解决方案--------------------------------------------------------
不能呀
可是我想添加这个功能
实现将字选定后设置字体的 颜色.
----------------解决方案--------------------------------------------------------
这个只好问千里了,偶已经忘记了
----------------解决方案--------------------------------------------------------
t1是什么?
----------------解决方案--------------------------------------------------------
他没写全吧
----------------解决方案--------------------------------------------------------
t1 明显是JTextArea

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

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 Menuout2 extends JFrame{
private ButtonGroup colorGroup,fontGroup;
private JTextArea t1;
private JRadioButton plain,bold,italic,boldItalic;
//实例化剪切板
Clipboard clipbd = getToolkit().getSystemClipboard();
JMenuItem colorItems[],fontItems[];
public Menuout2(){
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[colors.length];
fontGroup=new ButtonGroup();
for(int i=0;i<colors.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 blackL());
colorItems[1].addActionListener(new blueL());
colorItems[2].addActionListener(new redL());
colorItems[3].addActionListener(new greenL());
fontItems[0].addActionListener(new fontL());
fontItems[1].addActionListener(new fontL());
fontItems[2].addActionListener(new fontL());
fontItems[3].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));
setSize(600,400);
show();
}
public static void main(String args[]){
Menuout2 a=new Menuout2();
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(Menuout2.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(Menuout2.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();
//if(selection=="")
//set
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(Menuout2.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 blackL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
//selection.set
???????????????

}
}
class blueL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
???????????????

}
}
class redL implements ActionListener //字设置为红色
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
??????????????? 这几个实现将选定的字变为相应的颜色
}
}
class greenL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
???????????????

}
}
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){
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();
}
}
}
我不知道怎么样设置选定字的字体和颜色.
请高手指点~~~~~~~~``
先谢谢了


----------------解决方案--------------------------------------------------------
  相关解决方案