您的位置:首页 > 移动开发 > 微信开发

java记事本小程序

2012-08-08 19:24 267 查看
/* 下面这两个类实现了java开份小记事本程序,里面有打开,保存,关闭,新建,右键中的全选,复制,粘贴裁剪等功能*/

package notebook;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FileDialog;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import javax.swing.JColorChooser;

import javax.swing.JFileChooser;

import javax.swing.border.TitledBorder;

import javax.swing.filechooser.FileNameExtensionFilter;

/**

 *

 * @author  __USER__

 */

public class netbookmain extends javax.swing.JFrame {

 FileDialog fd = null;//声明一个文件对话框

 //创建SWING组件的文件选择对话框

 JFileChooser fileDialog = new JFileChooser();

 File file = null;

 /** Creates new form netbookmain */

 /**

  *

  */

 public netbookmain() {

  Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

  this.setBounds((size.width - 700) / 2, (size.height - 600) / 2, 500,

    600);

  initComponents();

  //txt1.setBorder(new TitledBorder("文本内容"));//在面板里加上边线

  openFile.addActionListener(new MyActionListener());

  saveFile.addActionListener(new MyActionListener());

  newFile.addActionListener(new MyActionListener());

  exit.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {

    System.exit(0);//关闭程序

   }

  });

  colorFile.addActionListener(new MyActionListener());

  fontFile.addActionListener(new MyActionListener());

  bankcolor.addActionListener(new MyActionListener());

  txt1.addMouseListener(new MouseAdapter() {

   public void mousePressed(MouseEvent e) {

    // TODO Auto-generated method stub

    if (e.getButton() == 3) {

     jPopupMenu1.show(txt1, e.getX(), e.getY());//右键菜单显示

    }

   }

  });

  //右键菜单中的监听

  //全选文本区域里面的内容

  SelectAll.addActionListener(new ActionListener() {

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    txt1.selectAll();

   }

  });

  copy.addActionListener(new ActionListener() {

   

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    txt1.copy();

   }

  });

  cut.addActionListener(new ActionListener() {

   

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    txt1.cut();

   }

  });

  pater.addActionListener(new ActionListener() {

   

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    txt1.paste();

   }

  });

  openFile.setActionCommand("open");

  saveFile.setActionCommand("save");

  newFile.setActionCommand("newF");

  colorFile.setActionCommand("color");

  fontFile.setActionCommand("font");

  bankcolor.setActionCommand("bankc");

 }

 private void MyPopupMenuListener() {

 }

 /**

  * @author Administrator

  *菜单事件

  */

 private class MyActionListener implements ActionListener {

  @Override

  public void actionPerformed(ActionEvent e) {

   // TODO Auto-generated method stub

   String comm = e.getActionCommand();

   if (comm.equals("open")) {

    open();

   } else if (comm.equals("save")) {

    save();

   } else if (comm.equals("newF")) {

    txt1.setText("");

    file = null;

   } else if (comm.equals("font")) {

    setfont();

   } else if (comm.equals("color")) {

    setcolor();

   } else if (comm.equals("bankc")) {

    setbancolor();

   }

  }

 }

 /**

  * 设置字体

  */

 private void setfont() {

  Font font = FontChooserDialog.showDialog(this, true);

  txt1.setFont(font);

 }

 /**

  * 设置字体颜色

  */

 private void setcolor() {

  Color color = JColorChooser.showDialog(this, "设置字体颜色", txt1

    .getForeground());

  txt1.setForeground(color);//设置字体颜色

 }

 /**

  * 设置背景颜色

  */

 private void setbancolor() {

  Color color = JColorChooser.showDialog(this, "设置背景颜色", txt1

    .getBackground());

  txt1.setBackground(color);//设置背景颜色

 }

 /**

  * 保存文件方法

  */

 private void save() {

  BufferedWriter bw = null;

  if (file == null) {

   //   fd = new FileDialog(this, "我的保存", FileDialog.SAVE);

   //   fd.setVisible(true);

   //创建txt文件的过滤器

   FileNameExtensionFilter ext = new FileNameExtensionFilter("文本文件",

     ".txt");

   //将过滤器添加到文件选择对话框

   fileDialog.addChoosableFileFilter(ext);

   int resu = fileDialog.showSaveDialog(this);

   if (resu == JFileChooser.CANCEL_OPTION)

    return;

   file = fileDialog.getSelectedFile();//得到选泽的文件

   System.out.println(file.getName());

   //   String path = fd.getDirectory();

   //   String fileName = fd.getFile();

   //   file = new File(path, fileName);

  }

  try {

   bw = new BufferedWriter(new FileWriter(file));

   String txt = txt1.getText();

   bw.write(txt.replaceAll("([^\r])\n", "$1\r\n"));

   bw.flush();

  } catch (Exception e) {

   e.printStackTrace();

  } finally {

   try {

    if (bw != null)

     bw.close();

   } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

   }

  }

 }

 /**

  * 打开文件方法

  */

 private void open() {

  //  fd = new FileDialog(this, "打开文件", FileDialog.LOAD);//打开模式

  //  fd.setVisible(true);//将文件对话框设置为可见

  //  String path = fd.getDirectory();

  //  String fileName = fd.getFile();

  BufferedReader br = null;

  //  if(path==null||fileName==null)return;

  //  file = new File(path, fileName);

  //创建txt文件的过滤器

  FileNameExtensionFilter ext = new FileNameExtensionFilter("文本文件",

    ".txt");

  //将过滤器添加到文件选择对话框

  fileDialog.addChoosableFileFilter(ext);

  int resu = fileDialog.showSaveDialog(this);

  if (resu == JFileChooser.CANCEL_OPTION)

   return;

  file = fileDialog.getSelectedFile();

  //System.out.println(path + ":" + fileName);

  try {

   br = new BufferedReader(new FileReader(file));

   String line = null;

   txt1.setText("");

   while ((line = br.readLine()) != null) {

    txt1.append(line + "\r\n");

   }

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  try {

   if (br != null)

    br.close();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 /** This method is called from within the constructor to

  * initialize the form.

  * WARNING: Do NOT modify this code. The content of this method is

  * always regenerated by the Form Editor.

  */

 //GEN-BEGIN:initComponents

 // <editor-fold defaultstate="collapsed" desc="Generated Code">

 private void initComponents() {

  bankcolor = new javax.swing.JMenuItem();

  jPopupMenu1 = new javax.swing.JPopupMenu();

  SelectAll = new javax.swing.JMenuItem();

  copy = new javax.swing.JMenuItem();

  cut = new javax.swing.JMenuItem();

  jSeparator3 = new javax.swing.JSeparator();

  pater = new javax.swing.JMenuItem();

  jScrollPane1 = new javax.swing.JScrollPane();

  txt1 = new javax.swing.JTextArea();

  jMenuBar1 = new javax.swing.JMenuBar();

  newFile = new javax.swing.JMenu();

  jMenuItem1 = new javax.swing.JMenuItem();

  jSeparator2 = new javax.swing.JSeparator();

  openFile = new javax.swing.JMenuItem();

  saveFile = new javax.swing.JMenuItem();

  jSeparator1 = new javax.swing.JSeparator();

  exit = new javax.swing.JMenuItem();

  jMenu2 = new javax.swing.JMenu();

  fontFile = new javax.swing.JMenuItem();

  colorFile = new javax.swing.JMenuItem();

  bankcolor = new javax.swing.JMenuItem();

  jMenuBar1 = new javax.swing.JMenuBar();

  newFile = new javax.swing.JMenu();

  jMenuItem1 = new javax.swing.JMenuItem();

  jSeparator2 = new javax.swing.JSeparator();

  openFile = new javax.swing.JMenuItem();

  saveFile = new javax.swing.JMenuItem();

  jSeparator1 = new javax.swing.JSeparator();

  exit = new javax.swing.JMenuItem();

  jMenu2 = new javax.swing.JMenu();

  fontFile = new javax.swing.JMenuItem();

  colorFile = new javax.swing.JMenuItem();

  bankcolor = new javax.swing.JMenuItem();

  bankcolor.setText("\u80cc\u666f\u989c\u8272");

  jMenu2.add(bankcolor);

  SelectAll.setText("\u5168\u9009");

  jPopupMenu1.add(SelectAll);

  copy.setText("\u590d\u5236");

  jPopupMenu1.add(copy);

  cut.setText("\u88c1\u526a");

  jPopupMenu1.add(cut);

  jPopupMenu1.add(jSeparator3);

  pater.setText("\u7c98\u8d34");

  jPopupMenu1.add(pater);

  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

  getContentPane().setLayout(new java.awt.CardLayout());

  txt1.setColumns(50);

  txt1.setRows(30);

  txt1.setBorder(javax.swing.BorderFactory

    .createTitledBorder("\u6211\u7684\u6587\u6863"));

  jScrollPane1.setViewportView(txt1);

  getContentPane().add(jScrollPane1, "card2");

  newFile.setText("\u6587\u4ef6");

  jMenuItem1.setText("\u65b0\u5efa");

  newFile.add(jMenuItem1);

  newFile.add(jSeparator2);

  openFile.setText("\u6253\u5f00");

  newFile.add(openFile);

  saveFile.setText("\u4fdd\u5b58");

  newFile.add(saveFile);

  newFile.add(jSeparator1);

  exit.setText("\u9000\u51fa");

  newFile.add(exit);

  jMenuBar1.add(newFile);

  jMenu2.setLabel("\u683c\u5f0f");

  fontFile.setText("\u5b57\u4f53");

  jMenu2.add(fontFile);

  colorFile.setText("\u5b57\u4f53\u989c\u8272");

  colorFile.setActionCommand("\u80cc\u666f\u989c\u8272");

  jMenu2.add(colorFile);

  bankcolor.setText("\u80cc\u666f\u989c\u8272");

  jMenu2.add(bankcolor);

  jMenuBar1.add(jMenu2);

  setJMenuBar(jMenuBar1);

  newFile.setText("\u6587\u4ef6");

  jMenuItem1.setText("\u65b0\u5efa");

  newFile.add(jMenuItem1);

  newFile.add(jSeparator2);

  openFile.setText("\u6253\u5f00");

  newFile.add(openFile);

  saveFile.setText("\u4fdd\u5b58");

  newFile.add(saveFile);

  newFile.add(jSeparator1);

  exit.setText("\u9000\u51fa");

  newFile.add(exit);

  jMenuBar1.add(newFile);

  jMenu2.setLabel("\u683c\u5f0f");

  fontFile.setText("\u5b57\u4f53");

  jMenu2.add(fontFile);

  colorFile.setText("\u5b57\u4f53\u989c\u8272");

  colorFile.setActionCommand("\u80cc\u666f\u989c\u8272");

  jMenu2.add(colorFile);

  bankcolor.setText("\u80cc\u666f\u989c\u8272");

  jMenu2.add(bankcolor);

  jMenuBar1.add(jMenu2);

  setJMenuBar(jMenuBar1);

  pack();

 }// </editor-fold>

 //GEN-END:initComponents

 /**

  * @param args the command line arguments

  */

 public static void main(String args[]) {

  java.awt.EventQueue.invokeLater(new Runnable() {

   public void run() {

    new netbookmain().setVisible(true);

   }

  });

 }

 //GEN-BEGIN:variables

 // Variables declaration - do not modify

 private javax.swing.JMenuItem SelectAll;

 private javax.swing.JMenuItem bankcolor;

 private javax.swing.JMenuItem colorFile;

 private javax.swing.JMenuItem copy;

 private javax.swing.JMenuItem cut;

 private javax.swing.JMenuItem exit;

 private javax.swing.JMenuItem fontFile;

 private javax.swing.JMenu jMenu2;

 private javax.swing.JMenuBar jMenuBar1;

 private javax.swing.JMenuItem jMenuItem1;

 private javax.swing.JPopupMenu jPopupMenu1;

 private javax.swing.JScrollPane jScrollPane1;

 private javax.swing.JSeparator jSeparator1;

 private javax.swing.JSeparator jSeparator2;

 private javax.swing.JSeparator jSeparator3;

 private javax.swing.JMenu newFile;

 private javax.swing.JMenuItem openFile;

 private javax.swing.JMenuItem pater;

 private javax.swing.JMenuItem saveFile;

 private javax.swing.JTextArea txt1;

 // End of variables declaration//GEN-END:variables

}

 

 

 

package notebook;

import java.awt.GraphicsEnvironment;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.ListModel;

import java.awt.Font;

import javax.swing.JPanel;

import java.awt.Rectangle;

import javax.swing.JScrollPane;

import javax.swing.BorderFactory;

import javax.swing.border.TitledBorder;

import java.awt.Color;

import javax.swing.JList;

import javax.swing.JLabel;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JTextField;

import javax.swing.border.BevelBorder;

import javax.swing.border.SoftBevelBorder;

import javax.swing.JButton;

import javax.swing.SwingConstants;

public class FontChooserDialog extends JDialog {

 /**

  *

  */

 private static final long serialVersionUID = 1L;

 /**

  * 界面设计需要

  * */

 private JPanel jPanel = null;

 private JScrollPane jScrollPane = null;

 private JPanel jPanel1 = null;

 private JLabel jLabel = null;

 private JLabel jLabel1 = null;

 private JLabel jLabel2 = null;

 private JTextField fontNameText = null;

 private JTextField fontItalicText = null;

 private JTextField fontSizeText = null;

 private JList fontNameList = null;

 private JList fontItalicList = null;

 private JList fontSizeList = null;

 private JPanel jPanel2 = null;

 private JButton okButton = null;

 private JButton regitButton = null;

 private JButton cancleButton = null;

 private JScrollPane jScrollPane1 = null;

 private JScrollPane jScrollPane2 = null;

 private JScrollPane jScrollPane3 = null;

 private static JLabel fontStyle = null;

 /**

  * 字体默认变量

  * */

 private Font defaultFont = new Font("\u5b8b\u4f53", Font.PLAIN, 12);

 /**

  * 返回字体变量

  * */

 private static Font returnFont = null;

 /**

  * Boolean 变量,判断是否正常返回,是否用户选择了字体

  * */

 private static boolean judge = false;

 /**

  * 以防止事件重复调用或不必要的更改,定义两个boolean变量分别

  * 为:fontNameList和fontSizeList判断

  * 等于true则循环调用,false则不

  * */

 private boolean nameJuge = true;

 private boolean sizeJuge = true;

 

 

 public FontChooserDialog(){

  this(null);

 }

 public FontChooserDialog(JFrame jframe){

  this(jframe,true);

 }

 public FontChooserDialog(JFrame jframe,boolean boo){

  this(jframe,boo,null);

 }

 public FontChooserDialog(JFrame jframe,boolean boo,Font font){

  super(jframe,boo);

  initialize();

  initializeFont(font);

  this.setLocationRelativeTo(jframe);

 }

 

 /**

  * This method initializes this

  *

  */

 private void initialize() {

        this.setContentPane(getJPanel());

        this.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

        this.setBounds(new Rectangle(0, 0, 430, 335));

        this.setTitle("字体选择对话框");

        this.addWindowListener(new WindowAdapter(){

         public void windowClosing(WindowEvent e) {

          judge = false;

          closeWindow();

         }

        });

   

 }

 public static Font showDialog(JFrame jframe,boolean boo){

  return showDialog(jframe,boo,null);

 }

 public static Font showDialog(JFrame jframe,boolean boo,Font font){

  JDialog jd = new FontChooserDialog(jframe,boo,font);

  jd.setVisible(true);

  if(judge){returnFont = fontStyle.getFont();}

  jd.dispose();

  return returnFont;

 }

 /**

  * This method initializes jPanel 

  *  

  * @return javax.swing.JPanel 

  */

 private JPanel getJPanel() {

  if (jPanel == null) {

   jPanel = new JPanel();

   jPanel.setLayout(null);

   jPanel.setFont(new Font("Dialog", Font.PLAIN, 12));

   jPanel.add(getJPanel1(), null);

   jPanel.add(getJPanel2(), null);

   jPanel.add(getOkButton(), null);

   jPanel.add(getRegitButton(), null);

   jPanel.add(getCancleButton(), null);

  }

  return jPanel;

 }

 /**

  * This method initializes jScrollPane 

  *  

  * @return javax.swing.JScrollPane 

  */

 private JScrollPane getJScrollPane() {

  if (jScrollPane == null) {

   fontStyle = new JLabel();

   fontStyle.setText("你好!天生我才必有用!Hello World!");

   fontStyle.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontStyle.setHorizontalAlignment(SwingConstants.CENTER);

   fontStyle.setHorizontalTextPosition(SwingConstants.CENTER);

   jScrollPane = new JScrollPane();

   jScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

   jScrollPane.setViewportView(fontStyle);

   jScrollPane.setBounds(new Rectangle(5, 20, 400, 60));

  }

  return jScrollPane;

 }

 /**

  * This method initializes jPanel1 

  *  

  * @return javax.swing.JPanel 

  */

 private JPanel getJPanel1() {

  if (jPanel1 == null) {

   jLabel2 = new JLabel();

   jLabel2.setBounds(new Rectangle(285, 5, 120, 15));

   jLabel2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   jLabel2.setText("大小:");

   jLabel1 = new JLabel();

   jLabel1.setBounds(new Rectangle(160, 5, 120, 15));

   jLabel1.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   jLabel1.setText("字型:");

   jLabel = new JLabel();

   jLabel.setBounds(new Rectangle(5, 5, 150, 15));

   jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);

   jLabel.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   jLabel.setText("字体:");

   jPanel1 = new JPanel();

   jPanel1.setLayout(null);

   jPanel1.setBounds(new Rectangle(5, 5, 410, 175));

   jPanel1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));

   jPanel1.add(jLabel, null);

   jPanel1.add(jLabel1, null);

   jPanel1.add(jLabel2, null);

   jPanel1.add(getFontNameText(), null);

   jPanel1.add(getFontItalicText(), null);

   jPanel1.add(getFontSizeText(), null);

   jPanel1.add(getJScrollPane1(), null);

   jPanel1.add(getJScrollPane2(), null);

   jPanel1.add(getJScrollPane3(), null);

  }

  return jPanel1;

 }

 /**

  * This method initializes fontNameText 

  *  

  * @return javax.swing.JTextField 

  */

 private JTextField getFontNameText() {

  if (fontNameText == null) {

   fontNameText = new JTextField();

   fontNameText.setBounds(new Rectangle(5, 25, 150, 20));

   fontNameText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontNameText.addKeyListener(new KeyAdapter(){

    public void keyTyped(KeyEvent e) {

     String oldText = fontNameText.getText();

     String newText = "";

     if("".equals(fontNameText.getSelectedText()) && null == fontNameText.getSelectedText()){

      newText = fontNameText.getText()+e.getKeyChar();

     }else{

      newText = oldText.substring(0,fontNameText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontNameText.getSelectionEnd());

     }

     System.out.println("fontName:"+newText);

     nameJuge = false;

     fontNameList.setSelectedValue(getLateIndex(fontNameList,newText),true);

     nameJuge = true;

    }

   });

  }

  return fontNameText;

 }

 /**

  * This method initializes fontItalicText 

  *  

  * @return javax.swing.JTextField 

  */

 private JTextField getFontItalicText() {

  if (fontItalicText == null) {

   fontItalicText = new JTextField();

   fontItalicText.setBounds(new Rectangle(160, 25, 120, 20));

   fontItalicText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontItalicText.setEnabled(false);

  }

  return fontItalicText;

 }

 /**

  * This method initializes fontSizeText 

  *  

  * @return javax.swing.JTextField 

  */

 private JTextField getFontSizeText() {

  if (fontSizeText == null) {

   fontSizeText = new JTextField();

   fontSizeText.setBounds(new Rectangle(285, 25, 120, 20));

   fontSizeText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontSizeText.setColumns(4);

   fontSizeText.addKeyListener(new KeyAdapter(){

    public void keyTyped(KeyEvent e) {

     //System.out.println("Key Code:"+(Character.getNumericValue(e.getKeyChar())==-1));

     String oldText = fontSizeText.getText();

     String newText = "";

     if("".equals(fontSizeText.getSelectedText()) && null == fontSizeText.getSelectedText()){

      newText = fontSizeText.getText()+e.getKeyChar();

     }else{

      newText = oldText.substring(0,fontSizeText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontSizeText.getSelectionEnd());

     }

     //System.out.println("fontSize:"+newText);

     sizeJuge = false;

     fontSizeList.setSelectedValue(getLateIndex(fontSizeList,newText),true);

     if(newText.matches("(\\d)+")){

      fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(newText)));

     }

     sizeJuge = true;

    }

   });

   

  }

  return fontSizeText;

 }

 /**

  * This method initializes fontNameList 

  *  

  * @return javax.swing.JList 

  */

 private JList getFontNameList() {

  if (fontNameList == null) {

   fontNameList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment()

     .getAvailableFontFamilyNames());

   fontNameList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontNameList

     .addListSelectionListener(new javax.swing.event.ListSelectionListener() {

      public void valueChanged(javax.swing.event.ListSelectionEvent e) {

       if(nameJuge){fontNameText.setText(fontNameList.getSelectedValue().toString());}

       fontStyle.setFont(new Font(fontNameList.getSelectedValue().toString(),fontStyle.getFont().getStyle(),fontStyle.getFont().getSize()));

      }

     });

  }

  return fontNameList;

 }

 /**

  * This method initializes fontItalicList 

  *  

  * @return javax.swing.JList 

  */

 private JList getFontItalicList() {

  if (fontItalicList == null) {

   fontItalicList = new JList(new String[]{"Plain", "Bold", "Italic","Bold Italic"});

   fontItalicList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontItalicList

     .addListSelectionListener(new javax.swing.event.ListSelectionListener() {

      public void valueChanged(javax.swing.event.ListSelectionEvent e) {

       fontItalicText.setText(fontItalicList.getSelectedValue().toString());

       fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontItalicList.getSelectedIndex(),fontStyle.getFont().getSize()));

      }

     });

  }

  return fontItalicList;

 }

 /**

  * This method initializes fontSizeList 

  *  

  * @return javax.swing.JList 

  */

 private JList getFontSizeList() {

  if (fontSizeList == null) {

   fontSizeList = new JList(new String[]{"3", "4", "5", "6", "7", "8", "9", "10",

     "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "22",

     "24", "27", "30", "34", "39", "45", "51", "60"});

   fontSizeList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   fontSizeList

     .addListSelectionListener(new javax.swing.event.ListSelectionListener() {

      public void valueChanged(javax.swing.event.ListSelectionEvent e) {

       if(sizeJuge){fontSizeText.setText(fontSizeList.getSelectedValue().toString());}

       fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(fontSizeList.getSelectedValue().toString())));

      }

     });

  }

  return fontSizeList;

 }

 /**

  * This method initializes jPanel2 

  *  

  * @return javax.swing.JPanel 

  */

 private JPanel getJPanel2() {

  if (jPanel2 == null) {

   jPanel2 = new JPanel();

   jPanel2.setLayout(null);

   jPanel2.setBounds(new Rectangle(3, 180, 414, 90));

   jPanel2.setBorder(BorderFactory.createTitledBorder(null, "\u6548\u679c\u9884\u89c8", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("\u5b8b\u4f53", Font.PLAIN, 12), new Color(51, 51, 51)));

   jPanel2.add(getJScrollPane(), null);

  }

  return jPanel2;

 }

 /**

  * This method initializes okButton 

  *  

  * @return javax.swing.JButton 

  */

 private JButton getOkButton() {

  if (okButton == null) {

   okButton = new JButton();

   okButton.setBounds(new Rectangle(215, 275, 60, 20));

   okButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   okButton.setText("确定");

   okButton.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

     judge = true;

     closeWindow();

    }

   });

  }

  return okButton;

 }

 /**

  * This method initializes regitButton 

  *  

  * @return javax.swing.JButton 

  */

 private JButton getRegitButton() {

  if (regitButton == null) {

   regitButton = new JButton();

   regitButton.setBounds(new Rectangle(285, 275, 60, 20));

   regitButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   regitButton.setText("重置");

   regitButton.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

     initializeFont(null);

    }

   });

  }

  return regitButton;

 }

 /**

  * This method initializes cancleButton 

  *  

  * @return javax.swing.JButton 

  */

 private JButton getCancleButton() {

  if (cancleButton == null) {

   cancleButton = new JButton();

   cancleButton.setBounds(new Rectangle(355, 275, 60, 20));

   cancleButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

   cancleButton.setText("取消");

   cancleButton.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

     judge = false;

     closeWindow();

    }

   });

  }

  return cancleButton;

 }

 /**

  * This method initializes jScrollPane1 

  *  

  * @return javax.swing.JScrollPane 

  */

 private JScrollPane getJScrollPane1() {

  if (jScrollPane1 == null) {

   jScrollPane1 = new JScrollPane();

   jScrollPane1.setBounds(new Rectangle(5, 50, 150, 120));

   jScrollPane1.setViewportView(getFontNameList());

  }

  return jScrollPane1;

 }

 /**

  * This method initializes jScrollPane2 

  *  

  * @return javax.swing.JScrollPane 

  */

 private JScrollPane getJScrollPane2() {

  if (jScrollPane2 == null) {

   jScrollPane2 = new JScrollPane();

   jScrollPane2.setBounds(new Rectangle(160, 50, 120, 120));

   jScrollPane2.setViewportView(getFontItalicList());

  }

  return jScrollPane2;

 }

 /**

  * This method initializes jScrollPane3 

  *  

  * @return javax.swing.JScrollPane 

  */

 private JScrollPane getJScrollPane3() {

  if (jScrollPane3 == null) {

   jScrollPane3 = new JScrollPane();

   jScrollPane3.setBounds(new Rectangle(285, 50, 120, 120));

   jScrollPane3.setViewportView(getFontSizeList());

  }

  return jScrollPane3;

 }

 

 /**

  * 默认的字体初始化方法

  * */

 private void initializeFont(Font font){

  if(font!=null){

   defaultFont = font;

   fontStyle.setFont(defaultFont);

  }

  fontStyle.setFont(defaultFont);

  fontNameList.setSelectedValue(defaultFont.getFontName(), true);

  fontSizeList.setSelectedValue(new Integer(defaultFont.getSize()).toString(), true);

  fontItalicList.setSelectedIndex(defaultFont.getStyle());

 }

 /**

  * 判断里给定的值最近的索引

  * */

 private Object getLateIndex(JList jlist,String str){

  ListModel list = jlist.getModel();

  if(str.matches("(\\d)+")){

   for(int i = list.getSize()-1;i>=0;i--){

    if(Integer.parseInt(list.getElementAt(i).toString())<=Integer.parseInt(str)){

     return list.getElementAt(i);

    }

   }

  }else {

   for(int i = list.getSize()-2;i>=0;i--){

    if(str.compareToIgnoreCase(list.getElementAt(i).toString())==0){

     return list.getElementAt(i);

    }else if(str.compareToIgnoreCase(list.getElementAt(i).toString())>0){

     return list.getElementAt(i+1);

    }

   }

  }

  return list.getElementAt(0);

 }

 /**

  * 窗体关闭方法!

  * */

 private void closeWindow(){

  this.setVisible(false);

 }

}  //  @jve:decl-index=0:visual-constraint="91,34"

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息