| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1072 人关注过本帖
标题:[求助]java记事本的字体问题,如何修改.
只看楼主 加入收藏
ye5terday
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-4
收藏
 问题点数:0 回复次数:1 
[求助]java记事本的字体问题,如何修改.

import javax.swing.*;

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;


public class Notepad extends JFrame{

public Notepad(){
initContainer();
initComponent();
initListener();
}
JList fontlist;
Choice fontpara;
JTextField fontsizetext;
JScrollPane listscroll,textscroll;
JPanel fontbuttonpanel,fontmainpanel,fontsizepanel,searchpanel,searchbuttonpanel,searchtextpanel,changepanel,changetextpanel,changebuttonpanel;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontname[]=ge.getAvailableFontFamilyNames();
String texttype="宋体";
int size=18;
int para=0;
Font font=new Font(texttype,Font.PLAIN,size);
private JPanel contentPane;
private JMenuBar menuBar=new JMenuBar();
private JMenu menuFile=new JMenu();
private JMenuItem mItemFileNew=new JMenuItem();
private JMenuItem mItemFileOpen=new JMenuItem();
private JMenuItem mItemFileSave=new JMenuItem();
private JMenuItem mItemFileSaveAs=new JMenuItem();
private JMenuItem mItemFileQuit=new JMenuItem();
private JMenu menuEdit=new JMenu();
private JMenuItem mItemEditCut=new JMenuItem();
private JMenuItem mItemEditCopy=new JMenuItem();
private JMenuItem mItemEditPaste=new JMenuItem();
private JMenuItem mItemEditDelete=new JMenuItem();
private JMenuItem mItemEditSelectAll=new JMenuItem();
private JMenu menuForm=new JMenu();
private JMenuItem mItemFormatColor=new JMenuItem();
private JMenuItem mItemFont=new JMenuItem();
private JMenu menuHelp=new JMenu();
private JMenuItem mItemHelpAbout=new JMenuItem();
private JPopupMenu PopupMenu=new JPopupMenu();
private JMenuItem mItemPopupCut=new JMenuItem();
private JMenuItem mItemPopupCopy=new JMenuItem();
private JMenuItem mItemPopupPaste=new JMenuItem();
private JMenuItem mItemPopupDelete=new JMenuItem();
private JTextArea textArea=new JTextArea();
private JScrollPane scroller=new JScrollPane();
private Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
private void initContainer(){
contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());
contentPane.add(textArea,BorderLayout.CENTER);
contentPane.add("Center",scroller);

this.setBounds(100,100,500,500);
this.setFont(new Font("宋体",Font.PLAIN,8));
this.setTitle("无标题-记事本");
this.setJMenuBar(menuBar);
}
private void initComponent(){
buildMenuBar();
buildPopupmenu();
buildTextArea();
}
private void buildMenuBar(){
initMenuBar();
initMenuFile();
initMenuEdit();
initMenuColor();
initMenuHelp();
}
private void initMenuBar(){
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuForm);
menuBar.add(menuHelp);
}
private void initMenuFile(){
menuFile.setText("文件");
menuFile.add(mItemFileNew);
menuFile.add(mItemFileOpen);
menuFile.add(mItemFileSave);
menuFile.add(mItemFileSaveAs);
menuFile.addSeparator();
menuFile.add(mItemFileQuit);

mItemFileNew.setText("新建(N)");
mItemFileNew.setMnemonic(KeyEvent.VK_N);
mItemFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));

mItemFileOpen.setText("打开(O)...");
mItemFileOpen.setMnemonic(KeyEvent.VK_O);
mItemFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));

mItemFileSave.setText("保存(S)...");
mItemFileSave.setMnemonic(KeyEvent.VK_S);
mItemFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));

mItemFileSaveAs.setText("另存为(A)...");
mItemFileSaveAs.setMnemonic(KeyEvent.VK_A);

mItemFileQuit.setText("退出");

}
private void initMenuEdit(){
menuEdit.setText("编辑");
menuEdit.add(mItemEditCut);
menuEdit.add(mItemEditCopy);
menuEdit.add(mItemEditPaste);
menuEdit.add(mItemEditDelete);
menuEdit.add(mItemEditSelectAll);

mItemEditCut.setText("剪切(T)");
mItemEditCut.setMnemonic(KeyEvent.VK_T);
mItemEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));

mItemEditCopy.setText("复制(C)");
mItemEditCopy.setMnemonic(KeyEvent.VK_C);
mItemEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK));

mItemEditPaste.setText("粘贴(P)");
mItemEditPaste.setMnemonic(KeyEvent.VK_P);
mItemEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK));

mItemEditDelete.setText("删除(L)");
mItemEditDelete.setMnemonic(KeyEvent.VK_L);
mItemEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,Event.CTRL_MASK));

mItemEditSelectAll.setText("全选(A)");
mItemEditSelectAll.setMnemonic(KeyEvent.VK_A);
mItemEditSelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK));

}
private void initMenuColor(){
menuForm.setText("格式");
menuForm.add(mItemFormatColor);
menuForm.add(mItemFont);

mItemFormatColor.setText("设置颜色(C)...");
mItemFormatColor.setMnemonic(KeyEvent.VK_C);

mItemFont.setText("字体(F)");
mItemFont.setMnemonic(KeyEvent.VK_F);

}
private void initMenuHelp(){
menuHelp.setText("帮助");
menuHelp.add(mItemHelpAbout);

mItemHelpAbout.setText("关于记事本(A)");
}
private void buildPopupmenu(){
PopupMenu.add(mItemPopupCut);
PopupMenu.add(mItemPopupCopy);
PopupMenu.add(mItemPopupPaste);
PopupMenu.add(mItemPopupDelete);

mItemPopupCut.setText("剪切");
mItemPopupCopy.setText("复制");
mItemPopupPaste.setText("粘贴");
mItemPopupDelete.setText("删除");
}
private void buildTextArea(){

textArea.setRows(20);
textArea.setColumns(20);
textArea.setDoubleBuffered(false);
textArea.setToolTipText("这是一个简单的记事本");
textArea.setVerifyInputWhenFocusTarget(true);
textArea.setText("");
textArea.add(PopupMenu);
scroller.getViewport().add(textArea);

}
private void initListener(){
ActListener actlistener=new ActListener();

mItemFileNew.addActionListener(actlistener);
mItemFileOpen.addActionListener(actlistener);
mItemFileSave.addActionListener(actlistener);
mItemFileSaveAs.addActionListener(actlistener);
mItemFileQuit.addActionListener(actlistener);

mItemEditCut.addActionListener(actlistener);
mItemEditCopy.addActionListener(actlistener);
mItemEditPaste.addActionListener(actlistener);
mItemEditDelete.addActionListener(actlistener);
mItemEditSelectAll.addActionListener(actlistener);

mItemFormatColor.addActionListener(actlistener);
mItemFont.addActionListener(actlistener);

mItemHelpAbout.addActionListener(actlistener);

mItemPopupCut.addActionListener(actlistener);
mItemPopupCopy.addActionListener(actlistener);
mItemPopupPaste.addActionListener(actlistener);
mItemPopupDelete.addActionListener(actlistener);

textArea.addMouseListener(new MouseListener());

this.addWindowListener(new WindowListener());

}
class ActListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if (e.getSource()==mItemFileNew){
fileNew();
}else if (e.getSource()==mItemFileOpen){
fileOpen();
}else if (e.getSource()==mItemFileSave){
fileSave();
}else if (e.getSource()==mItemFileSaveAs){
fileSaveAs();
}else if (e.getSource()==mItemFileQuit){
fileQuit();
}else if (e.getSource()==mItemEditCut){
fileCut();
}else if (e.getSource()==mItemEditCopy){
fileCopy();
}else if (e.getSource()==mItemEditPaste){
filePaste();
}else if (e.getSource()==mItemEditDelete){
fileDel();
}else if (e.getSource()==mItemPopupCut){
fileCut();
}else if (e.getSource()==mItemPopupCopy){
fileCopy();
}else if (e.getSource()==mItemPopupPaste){
filePaste();
}else if (e.getSource()==mItemPopupDelete){
fileDel();
}else if (e.getSource()==mItemEditSelectAll){
selectAll();
}else if (e.getSource()==mItemFormatColor){
chooseColor();
}else if (e.getSource()==mItemFont){
font();
}else if (e.getSource()==mItemHelpAbout){
about();
}
}
}
String fileName=null;

boolean isSaved=false;

private void fileNew(){
if (isSaved){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");
}else{
int result=JOptionPane.showConfirmDialog(this,"想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);

if (JOptionPane.OK_OPTION==result){
fileSaveAs();
}else if(JOptionPane.NO_OPTION==result){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");
}else {
}
}

}
private void fileOpen(){
String openFileName="";
JFileChooser jFileChooser=new JFileChooser();
if (isSaved){
try{
if (JFileChooser.APPROVE_OPTION==jFileChooser
.showOpenDialog(this)){
openFileName=jFileChooser.getSelectedFile().getPath();
File file=new File(openFileName);
int flength=(int) file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[] data =new char[flength];
while (fReader.ready()){
num+=fReader.read(data,num,flength - num);
}
fReader.close();
textArea.setText(new String(data,0,num));
fileName = openFileName;
this.setTitle(fileName
.substring(fileName.lastIndexOf("\\")+1));
this.repaint();
isSaved=false;
}
}catch (Exception e){
JOptionPane.showMessageDialog(this,"打开文件时出错!","错误",
JOptionPane.ERROR_MESSAGE);
}
}else{
int result =JOptionPane.showConfirmDialog(this,"想保存文件吗?","记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if (JOptionPane.OK_OPTION==result){
fileSave();
fileOpen();
}else if (JOptionPane.NO_OPTION==result){
isSaved=true;
fileOpen();
}else{
}
}
}
private void fileSave(){
if (fileName==null){
fileSaveAs();
}else {
if (!isSaved){
if (fileName.length()!=0){
try {
File saveFile=new File(fileName);
FileWriter fw=new FileWriter(saveFile);
fw.write(textArea.getText());
fw.close();
isSaved=true;
this.setTitle(fileName.substring(fileName
.lastIndexOf("\\")+1));
this.repaint();

}catch (Exception e){
JOptionPane.showMessageDialog(this,"保存文件时出错!",
"错误",JOptionPane.ERROR_MESSAGE);
}
}else {
fileSaveAs();
}
}
}
}
private void fileSaveAs(){
JFileChooser jFileChooser=new JFileChooser();
if (JFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this))
{
fileName=jFileChooser.getSelectedFile().getPath();
fileSave();
}
}
private void fileQuit(){
if (!isSaved){
int result=JOptionPane.showConfirmDialog(this,"想保存文件吗?","记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
fileSave();
}else if(JOptionPane.NO_OPTION==result){
System.exit(0);
}else{
}
}else{
System.exit(0);
}
}
private void fileCut(){
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
this.textArea.replaceRange("",this.textArea
.getSelectionStart(),
this.textArea.getSelectionEnd());
isSaved=false;
}
}catch(Exception ex){
JOptionPane.showMessageDialog(this,"剪切时出错!",
"错误",JOptionPane.ERROR_MESSAGE);
}
}
private void fileCopy(){
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
}
}catch (Exception ex){
JOptionPane.showMessageDialog(this,"复制时出错!","错误",
JOptionPane.ERROR_MESSAGE);
}
}
private void filePaste(){
try {
Transferable tr = cb.getContents(this);
if(tr!=null){
String s=(String)
tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null){
textArea.replaceRange(s,textArea.getSelectionStart(),
textArea.getSelectionEnd());

}
isSaved=false;
}
}catch (Exception err){
JOptionPane.showMessageDialog(this,"粘贴时出错!","错误",
JOptionPane.ERROR_MESSAGE);
}
}
private void fileDel(){
textArea.replaceRange("",textArea.getSelectionStart(),textArea
.getSelectionEnd());
isSaved=false;
}
private void selectAll(){
textArea.setSelectionStart(0);
textArea.setSelectionEnd(this.textArea.getText().length());
}
private void chooseColor(){
Color bcolor=textArea.getForeground();
JColorChooser jColor=new JColorChooser();
jColor.setColor(bcolor);
textArea.setForeground(JColorChooser.showDialog(textArea,"选择颜色",bcolor));
}

public void font(){
try{
size=Integer.parseInt(fontsizetext.getText());
if(size<=0 || size>99){
size=8;
throw new Exception("字体大小范围为1~99,字体将还原为默认值");
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"字体大小错误","警告",JOptionPane.WARNING_MESSAGE);
}
if(fontpara.getSelectedItem()=="正常"){
font=new Font(texttype,Font.PLAIN,size);
}
else if(fontpara.getSelectedItem()=="粗体"){
font=new Font(texttype,Font.BOLD,size);
}
else if(fontpara.getSelectedItem()=="斜体"){
font=new Font(texttype,Font.ITALIC,size);
}
else if(fontpara.getSelectedItem()=="粗斜体"){
font=new Font(texttype,Font.BOLD+Font.ITALIC,size);
}
else{}
}
private void about(){
JOptionPane.showMessageDialog(this,"这是一个简单的记事本!","记事本",
JOptionPane.INFORMATION_MESSAGE);
}
class MouseListener extends MouseAdapter{
@Override
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
PopupMenu.show((JTextArea) e.getSource(),e.getX(),e.getY());
}
}
}
class WindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent arg0){
fileQuit();
}
}
public void showNotepad(){
this.setVisible(true);
this.validate();
}

public static void main(String args[]){
new Notepad().showNotepad();
}
}

zd8dKDBP.rar (7 KB) [求助]java记事本的字体问题,如何修改.


搜索更多相关主题的帖子: java 字体 记事本 
2007-07-09 16:40
ye5terday
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-4
收藏
得分:0 
谢谢了
2007-07-10 15:55
快速回复:[求助]java记事本的字体问题,如何修改.
数据加载中...
 
   



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

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