/****************************************************/(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
//行为事件
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jMenuFileCut)
{
FileCut(e);
}
else if (e.getSource() == jMenuFileCopy)
{
FileCopy(e);
}
else if (e.getSource() == jMenuFilePaste)
{
FilePaste(e);
}
else if (e.getSource() == jMenuFileDel)
{
FileDel(e);
}
else if (e.getSource()==jMenuMouseCut)
{
FileCut(e);
}
else if (e.getSource()==jMenuMouseCopy)
{
FileCopy(e);
}
else if (e.getSource()==jMenuMousePaste)
{
FilePaste(e);
}
else if (e.getSource()==jMenuMouseDel)
{
FileDel(e);
}
else if (e.getSource() == jMenuFileFindNext)
{
FileFindNext(e);
}
else if(e.getSource() ==jMenuFileSelect){
AllSelect(e);
}
else if (e.getSource() ==jMenuFileTime){
FileTime(e);
}
else if(e.getSource()==jMenuCheckBox){
if(check){
FileLine(e);
}
//check=!check;
}
else if (e.getSource() == jMenuFileFont)
{
new Bfont(jTextArea1);
}
else if (e.getSource() == jcolor)
{
fcolor(e);
}
else if (e.getSource() == jMenuHelpAbout)
{
HelpAboutDialog(e);
}
}
/****************************************************/
//主函数
public static void main(String args[]){
new Notepad();
}
//****************************************************
class File_new_actionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e){
File_new_actionPerformed(e);
}
}
class File_open_actionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e){
File_open_actionPerformed(e);
}
}
class File_save_actionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e){
File_save_actionPerformed(e);
}
}
class File_saveto_actionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e){
File_saveto_actionPerformed(e);
}
}
class File_close_actionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e){
File_close_actionPerformed(e);
}
}
//****************************************************************
//查找类
class Frame1 extends JDialog implements ActionListener {
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JCheckBox jCheckBox1 = new JCheckBox("区分大小写",true);
JRadioButton[] teams = new JRadioButton[2];
ButtonGroup jj=new ButtonGroup();
String findstr;
public Frame1(JTextArea jTextArea1) {
try {
findstr=jTextArea1.getText();
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void jbInit() throws Exception {
this.getContentPane().setLayout(null);
this.setBounds(120,120,380, 150);
this.setTitle("查找");
this.setModal(true);
jButton1.setText("下一个");
jButton1.setBounds(new Rectangle(273, 7, 91, 20));
Button1Listener next=new Button1Listener();
jButton1.addActionListener(next);
jButton3.setText("替换");
jButton3.setBounds(new Rectangle(273, 36, 91, 20));
Button3Listener replace =new Button3Listener();
jButton3.addActionListener(replace);
jButton2.setText("取消");
jButton2.setBounds(new Rectangle(273, 65, 91, 20));
Button2Listener cancel=new Button2Listener();
jButton2.addActionListener(cancel);
jLabel1.setText("查找内容 :");
jLabel1.setBounds(new Rectangle(7, 10, 72, 19));
jTextField1.setBounds(new Rectangle(78, 12, 182, 19));
jLabel2.setText("替换为 :");
jLabel2.setBounds(new Rectangle(7, 36, 72, 19));
jTextField2.setBounds(new Rectangle(78, 36, 182, 19));
jCheckBox1.setBounds(new Rectangle(7, 85, 89, 25));
jCheckBox1.addActionListener(this);
teams[0]=new JRadioButton("向上",false);
teams[0].setBounds(new Rectangle(98, 85, 68, 22));
teams[0].addActionListener(this);
teams[1]=new JRadioButton("向下",true);
teams[1].setBounds(new Rectangle(178, 85, 62, 25));
teams[1].addActionListener(this);
jj.add(teams[0]);
jj.add(teams[1]);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton3, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jTextField2, null);
this.getContentPane().add(jCheckBox1, null);
this.getContentPane().add(teams[0], null);
this.getContentPane().add(teams[1], null);
show();
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == jCheckBox1){
if(check){
findstr=findstr.toLowerCase();
}
else
findstr=jTextArea1.getText();
check=!check;
}
}
public void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSED) {
System.exit(0);
}
}
public void find(){
String text1=jTextField1.getText();
if(check){
text1=text1.toLowerCase();
}
int index,
i=jTextArea1.getSelectionStart();
t=jTextArea1.getSelectionEnd();
if(teams[0].isSelected()){
String ftp=findstr.substring(0,i);
index = ftp.lastIndexOf(text1);
if(index!=-1){
i=index-1;
jTextArea1.select(index,index+text1.length());
}
else
JOptionPane.showMessageDialog(this,"找不到"+text1,"提示",JOptionPane.INFORMATION_MESSAGE);
}
else {
index = findstr.indexOf(text1,t);
if(index!=-1){
t=index+text1.length();
jTextArea1.select(index,index+text1.length());
}
else
JOptionPane.showMessageDialog(this,"找不到"+text1,"提示",JOptionPane.INFORMATION_MESSAGE);
}
this.repaint();
}
//替换
public void replace(){
String text2=jTextField2.getText();
int star=jTextArea1.getSelectionStart();
int end=jTextArea1.getSelectionEnd();
if(star!=end){
jTextArea1.replaceRange(text2,star,end);
}
else
JOptionPane.showMessageDialog(this,"请确定要查找的内容","提示",JOptionPane.INFORMATION_MESSAGE);
}
class Button1Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
find();
}
}
class Button2Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
hide();
}
}
class Button3Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
replace();
}
}
}
//字体类
public class Bfont extends JDialog {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
String[] data1 = {"Arial", "Arial Black", "Arial Narrow", "Book Antiqua","Bookman Old Style","Century Gothic","Comic Sans MS","Courier","Fixedsys","Garamond"};
JList jList1 = new JList(data1);
JLabel jLabel2 = new JLabel();
String[] data2 = {"常规", "斜体", "粗体", "粗斜体"};
JList jList2 = new JList(data2);
JLabel jLabel3 = new JLabel();
String[] data3 = {"10", "11", "12", "14","16","18","20"};
JList jList3 = new JList(data3);
JButton jButton1 = new JButton();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JTextField jTextField1 = new JTextField("败");
JTextField jTextField2 = new JTextField("Arial");
JTextField jTextField3 = new JTextField("常规");
JTextField jTextField4 = new JTextField("10");
JButton jButton2 = new JButton();
int index;
String ziti;
int daxiao;
int zixing;
public Bfont(JTextArea jTextArea1) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.getContentPane().setBackground(Color.lightGray);
this.setSize(new Dimension(440,315));
this.setTitle("字体");
this.setModal(true);
jLabel1.setText("字体");
jLabel1.setBounds(new Rectangle(11, 10, 65, 23));
jTextField2.setBounds(new Rectangle(8, 40, 138, 20));
jList1.setToolTipText("提示");
jList1.setSelectedIndex(0);
jList1.setBounds(new Rectangle(8, 61, 138, 115));
jLabel2.setText("字型");
jLabel2.setBounds(new Rectangle(158, 10, 65, 23));
jTextField3.setBounds(new Rectangle(152, 40, 109, 20));
jList2.setSelectedIndex(0);
jList2.setBounds(new Rectangle(152, 61, 109, 115));
jLabel3.setText("大小");
jLabel3.setBounds(new Rectangle(273, 10, 65, 23));
jTextField4.setBounds(new Rectangle(267, 40, 66, 20));
jList3.setSelectedIndex(0);
jList3.setBounds(new Rectangle(267, 61, 66, 115));
jButton1.setBounds(new Rectangle(344, 38, 81, 26));
jButton1.setText("确定");
jComboBox1.setBounds(new Rectangle(151, 215, 181, 22));
jLabel4.setText("字符集");
jLabel4.setBounds(new Rectangle(152, 188, 65, 23));
jTextField1.setBackground(Color.lightGray);
jTextField1.setEnabled(false);
jTextField1.setBounds(new Rectangle(10, 194, 127, 79));
jButton2.setBounds(new Rectangle(345, 83, 79, 26));
jButton2.setText("取消");
contentPane.add(jLabel1, null);
contentPane.add(jLabel4, null);
contentPane.add(jComboBox1, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel2, null);
contentPane.add(jList1, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jList3, null);
contentPane.add(jList2, null);
contentPane.add(jTextField4, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField2, null);
contentPane.add(jTextField1, null);
jComboBox1.addItem("西方");
jComboBox1.addItem("华文");
Button1Listener fok=new Button1Listener();
jButton1.addActionListener(fok);
Button2Listener fon=new Button2Listener();
jButton2.addActionListener(fon);
MouseListener mouseListener1 = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
index = jList1.locationToIndex(e.getPoint());
jList1.setSelectedIndex(index);
jTextField2.setText(jList1.getSelectedValue().toString());
ziti=jTextField2.getText();
}
}
};
MouseListener mouseListener2 = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
index = jList2.locationToIndex(e.getPoint());
jList2.setSelectedIndex(index);
String zip=jList2.getSelectedValue().toString();
jTextField3.setText(zip);
if(zip.compareTo("常规")==0){
zixing=Font.PLAIN;
}
else if(zip.compareTo("斜体")==0){
zixing=Font.ITALIC;
}
else if(zip.compareTo("粗体")==0){
zixing=Font.BOLD;
}
// jTextField2.setFont(new Font(ziti, zixing, daxiao));
}
}
};
MouseListener mouseListener3 = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
index = jList3.locationToIndex(e.getPoint());
jList3.setSelectedIndex(index);
jTextField4.setText(jList3.getSelectedValue().toString());
daxiao=Integer.parseInt(jTextField4.getText());
// jTextField2.setFont(new Font(ziti, zixing, daxiao));
}
}
};
jList1.addMouseListener(mouseListener1);
jList2.addMouseListener(mouseListener2);
jList3.addMouseListener(mouseListener3);
show();
}
void sfont(){
// String ziti=jTextField2.getText();
// int daxiao=Integer.parseInt(jTextField4.getText());
jTextArea1.setDragEnabled(true);
jTextArea1.setFont(new Font(ziti, zixing, daxiao));
}
class Button1Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
sfont();
//有问题 si
// hide();
}
}
class Button2Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
hide();
}
}
public void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSED) {
System.exit(0);
}
}
}
}