当前位置: 代码迷 >> Java相关 >> [求助]java记事本的字体问题,如何修改.
  详细解决方案

[求助]java记事本的字体问题,如何修改.

热度:245   发布时间:2007-07-09 16:40:54.0
[求助]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();
}
}


搜索更多相关的解决方案: java  字体  记事本  

----------------解决方案--------------------------------------------------------
谢谢了

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