| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5868 人关注过本帖
标题:记事本中的内容选定后怎样设置字体颜色,
只看楼主 加入收藏
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
收藏
 问题点数:0 回复次数:20 
记事本中的内容选定后怎样设置字体颜色,
我做了一个记事本程序
想实现将记事本中的内容选定后设置字体颜色,
该调用哪个方法????
class redL implements ActionListener //设置为红色
{
public void actionPerformed(ActionEvent e)
{
String selection = t1.getSelectedText();
selection.?????????????
}
}
怎么设置
清高手指点
谢谢了!
搜索更多相关主题的帖子: 字体 记事本 颜色 设置 
2006-09-08 16:39
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
收藏
得分:0 
怎么没人回答?

2006-09-08 17:41
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
收藏
得分:0 

2006-09-09 15:11
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
请问,你的windows记事本的内容选定后能做到你上述的功能么?

淘宝杜琨
2006-09-09 15:50
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
收藏
得分:0 
不能呀
可是我想添加这个功能
实现将字选定后设置字体的 颜色.

2006-09-09 16:01
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
这个只好问千里了,偶已经忘记了

淘宝杜琨
2006-09-09 19:28
baidu
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:3811
专家分:0
注 册:2005-11-4
收藏
得分:0 
t1是什么?

偶放弃所有文章版权,偶在BCCN论坛任何贴子,可转贴,可散发,可抄袭,可复制,可被冒名顶替,可被任何人引用到任何文章中且不写出引文出处,偶分文不取。
2006-09-09 20:01
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
他没写全吧

win32汇编
病毒 加密
目前兴趣所在
2006-09-09 20:32
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
t1 明显是JTextArea

淘宝杜琨
2006-09-09 21:57
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
收藏
得分:0 

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


2006-09-09 21:59
快速回复:记事本中的内容选定后怎样设置字体颜色,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016179 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved