十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
以下代码是一个完整的实现,你只要复制过去就可以了~~
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的修武网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
public class Start
{
public static void main(String args[])
{
new TextPad();
}
}
class TextPad extends JFrame implements ActionListener
{
JTextArea jta=new JTextArea("小廖记事本:)",18,52);
JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行",true);
String ss1=jta.getText();
UndoableEditListener ue=new UndoHander();
UndoManager undo = new UndoManager();
int StartFindPos=0,a=0,b=0;
GridBagConstraints gbc=new GridBagConstraints();
//Dimension dd=new Dimension();
// jta.getDocument().addUndoableEditListener(ue);
public TextPad()
{
//MyMenuListener ml=new MyMenuListener();
//JTextArea jta=new JTextArea("This is my textpad",18,52);
//System.out.println(dd.getHeight());
//System.out.println(dd.getWidth());
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
//System.out.println("OK");
this.setTitle("一个功能比较齐全的JAVA记事本");
this.setLocation(180,100);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JPanel jp=new JPanel();
JScrollPane jsp=new JScrollPane(jta);
jp.add(jsp);
//Rectangle rt=new Rectangle(0,0,this.getWidth(),this.getHeight());
//jsp.setBounds(rt);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
JMenu mf=new JMenu("文件(F)");
JMenuItem mtf1=new JMenuItem("新建");
mtf1.addActionListener(this);
JMenuItem mtf2=new JMenuItem("打开");
//mtf2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
mtf2.addActionListener(this);
JMenuItem mtf3=new JMenuItem("保存");
//mtf3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
mtf3.addActionListener(this);
JMenuItem mtf4=new JMenuItem("另存为");
mtf4.addActionListener(this);
JMenuItem mtf5=new JMenuItem("退出");
mtf5.addActionListener(this);
JMenu me=new JMenu("编辑(E)");
JMenuItem mte1=new JMenuItem("撤消");
mte1.addActionListener(this);
jta.getDocument().addUndoableEditListener(ue);
if(undo.canUndo())
{
mte1.setEnabled(false);
}
JMenuItem mte2=new JMenuItem("剪切");
mte2.addActionListener(this);
JMenuItem mte3=new JMenuItem("复制");
mte3.addActionListener(this);
JMenuItem mte4=new JMenuItem("粘贴");
mte4.addActionListener(this);
//JMenuItem mte5=new JMenuItem("删除");
//mte5.addActionListener(this);
JMenuItem mte6=new JMenuItem("查找");
mte6.addActionListener(this);
//JMenuItem mte7=new JMenuItem("查找下一个");
JMenuItem mte8=new JMenuItem("替换");
mte8.addActionListener(this);
//JMenuItem mte9=new JMenuItem("转到");
JMenuItem mte10=new JMenuItem("全选");
mte10.addActionListener(this);
JMenuItem mte11=new JMenuItem("日期/时间");
mte11.addActionListener(this);
JMenu mo=new JMenu("格式(O)");
//JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行(W)");
mto1.addActionListener(this);
JMenuItem mto2=new JMenuItem("字体");
mto2.addActionListener(this);
JMenu mv=new JMenu("查看(V)");
JMenuItem mtv1=new JMenuItem("状态栏");
mtv1.setEnabled(false);
JMenu mh=new JMenu("帮助(H)");
JMenuItem mth1=new JMenuItem("关于记事本");
mth1.addActionListener(this);
JMenuBar mb=new JMenuBar();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
mb.add(mf);
mb.add(me);
mb.add(mo);
mb.add(mv);
mb.add(mh);
this.setJMenuBar(mb);
mf.add(mtf1);
mf.add(mtf2);
mf.add(mtf3);
mf.add(mtf4);
mf.addSeparator();
mf.add(mtf5);
me.add(mte1);
me.addSeparator();
me.add(mte2);
me.add(mte3);
me.add(mte4);
//me.add(mte5);
me.addSeparator();
me.add(mte6);
//me.add(mte7);
me.add(mte8);
//me.add(mte9);
me.addSeparator();
me.add(mte10);
me.add(mte11);
mo.add(mto1);
mo.add(mto2);
mv.add(mtv1);
mh.add(mth1);
this.getContentPane().add(jsp);
this.setSize(600,400);
this.setResizable(true);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
try
{
Frame f=new Frame();
FileDialog fd=new FileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String fpath=fd.getDirectory();
String fname=fd.getFile();
BufferedReader br=new BufferedReader(new FileReader(fpath+fname));
jta.setText("");
String s=br.readLine();
while(s!=null)
{
jta.append(s+"\n");
s=br.readLine();
}
br.close();
}
catch(Exception ex)
{
}
}
if(e.getActionCommand().equals("保存"))
{
String fns=null;
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("新建"))
{
jta.setText("");
}
if(e.getActionCommand().equals("另存为"))
{
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"文件另存为",FileDialog.SAVE);
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("退出"))
{
String ss2=jta.getText();
if(!ss1.equals(ss2))
{
System.out.println("File is changed.");
}
System.exit(0);
}
if(e.getActionCommand().equals("撤消"))
{
try
{
undo.undo();
//System.out.println(undo.canUndo());
}
catch(Exception eundo)
{
}
}
if(e.getActionCommand().equals("剪切"))
{
jta.cut();
}
if(e.getActionCommand().equals("复制"))
{
jta.copy();
}
if(e.getActionCommand().equals("粘贴"))
{
jta.paste();
}
if(e.getActionCommand().equals("删除"))
{
}
if(e.getActionCommand().equals("全选"))
{
jta.selectAll();
}
if(e.getActionCommand().equals("查找"))
{
try
{
final JDialog jd=new JDialog(this,"查找",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
//jd.setDefaultLookAndFeelDecorated(true);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("查找内容:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("查找");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf.getText();
if(a=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("取消");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
//jd.setResizable(false);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand().equals("替换"))
{
final JDialog jd=new JDialog(this,"替换",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=1;
gbc.weighty=1;
gbc.gridwidth=1;
gbc.gridheight=1;
JLabel jlFind=new JLabel("查找:");
JLabel jp=new JLabel("替换内容:");
final JTextField jtf=new JTextField(15);
final JTextField jtf1=new JTextField(15);
jd.getContentPane().setLayout(gbl);
jd.setSize(330,150);
jd.setResizable(false);
final JButton jbReplace=new JButton("替换");
final JButton jbReplaceAll=new JButton("替换所有");
final JButton jbCancel=new JButton("取消");
final JButton jbFind=new JButton("查找");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jlFind,gbc);
gbc.gridx=1;
gbc.gridy=0;
jd.getContentPane().add(jtf1,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jbFind,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jp,gbc);
gbc.gridx=1;
gbc.gridy=1;
jd.getContentPane().add(jtf,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jbReplace,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jbReplaceAll,gbc);
gbc.gridx=2;
gbc.gridy=3;
jd.getContentPane().add(jbCancel,gbc);
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf1.getText();
if(a=0)
{
a=strA.indexOf(strB,StartFindPos);
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
}
}
}
);
jbReplace.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("替换"))
{
String strRepleace=jtf.getText();
jta.replaceSelection(strRepleace);
}
}
}
);
jbReplaceAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
while(a-1)
{
String strA=jta.getText();
String strB=jtf1.getText();
a=strA.indexOf(strB,StartFindPos);
if(a==-1)
{
break;
}
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
//System.out.println(StartFindPos);
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
String strRepleaceAll=jtf.getText();
jta.replaceSelection(strRepleaceAll);
StartFindPos=a+b;
}
JOptionPane.showMessageDialog(null, "全部替换完毕", "替换内容",1);
a=0;
StartFindPos=0;
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setLocation(240,200);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("日期/时间"))
{
final JDialog jd=new JDialog(this,"插入日期");
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField jtf=new JTextField(10);
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
//Calendar cl=Calendar.getInstance();
//DateFormat df=DateFormat.getInstance();
//String sdate1=df.format(cl.getTime());
//jtf.setText(sdate1);
jp1.add(jtf);
jp1.add(jbOK);
jp1.add(jbCancel);
jd.getContentPane().add(jp1,"North");
JPanel jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
final JCheckBox jcb1=new JCheckBox("格式一");
final JCheckBox jcb2=new JCheckBox("格式二");
final JCheckBox jcb3=new JCheckBox("格式三");
jp2.add(jcb1);
jp2.add(jcb2);
jp2.add(jcb3);
jd.getContentPane().add(jp2,"Center");
jd.setSize(220,120);
jd.setResizable(false);
jd.setLocation(240,200);
final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cl=Calendar.getInstance();
DateFormat df=DateFormat.getInstance();
final String sdate=df.format(cl.getTime());
jcb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式一"))
{
if(jcb1.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy年MM月dd日");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb2.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle1)
{
estyle1.printStackTrace();
}
}
else
{
jcb2.setEnabled(true);
jcb3.setEnabled(true);
}
try
{
System.out.println(jta.getLineStartOffset(3));
//System.out.println(jta.getLineOfOffset(346));
}
catch(Exception eee)
{
}
}
}
}
);
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式二"))
{
if(jcb2.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy/MM/dd");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb1.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle2)
{
estyle2.printStackTrace();
}
}
else
{
jcb1.setEnabled(true);
jcb3.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式三"))
{
if(jcb3.isSelected())
{
jtf.setText(sdate);
jcb1.setEnabled(false);
jcb2.setEnabled(false);
}
else
{
jcb1.setEnabled(true);
jcb2.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("确定"))
{
int pos=jta.getCaretPosition();
jta.insert(jtf.getText(),pos);
}
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("自动换行"))
{
if(mto1.getState())
{
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
}
else
{
jta.setLineWrap(false);
jta.setWrapStyleWord(false);
}
//System.out.println("OK");
}
if(e.getActionCommand().equals("字体"))
{
final JDialog jd=new JDialog(this,"字体设置");
jd.setLocation(240,200);
//final JFrame jfontview=new JFrame();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
JTextField jtf1=new JTextField(6);
final JTextArea jtaview=new JTextArea(4,8);
//jfontview.getContentPane().add(jtaview);
final JTextField jtf2=new JTextField(6);
final JTextField jtf3=new JTextField(3);
JComboBox jcb1=new JComboBox();
final JComboBox jcb2=new JComboBox();
jcb2.addItem("BOLD");
jcb2.addItem("ITALIC");
jcb2.addItem("PLAIN");
//jcb2.addItem("BOLDITALIC");
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf2.setText((String)jcb2.getSelectedItem());
}
}
);
final JComboBox jcb3=new JComboBox();
jcb3.addItem("14");
jcb3.addItem("18");
jcb3.addItem("22");
jcb3.addItem("26");
jcb3.addItem("30");
jcb3.addItem("34");
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf3.setText((String)jcb3.getSelectedItem());
//jtaview.setText("SS");
// int fontsizeview=Integer.parseInt((String)jcb3.getSelectedItem());
//Font fontview=new Font("字体预览",Font.BOLD,fontsizeview);
//jtaview.setFont(fontview);
//jtaview.setEditable(false);
}
}
);
gbc.gridheight=1;
gbc.gridwidth=1;
//gbc.gridx=1;
//gbc.gridy=0;
gbc.weightx=0.5;
gbc.weighty=0.5;
jd.getContentPane().setLayout(gbl);
//JLabel jl1=new JLabel("预览:");
JLabel jl2=new JLabel("字型:");
JLabel jl3=new JLabel("大小:");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jl2,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jl3,gbc);
//gbc.gridx=4;
//gbc.gridy=0;
//jd.getContentPane().add(jl3,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jtf2,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jtf3,gbc);
//gbc.gridx=4;
// gbc.gridy=1;
//jd.getContentPane().add(jtf3,gbc);
gbc.gridx=0;
gbc.gridy=2;
jd.getContentPane().add(jcb2,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jcb3,gbc);
//gbc.gridx=4;
//gbc.gridy=2;
//jd.getContentPane().add(jcb3,gbc);
gbc.gridx=4;
gbc.gridy=1;
jd.getContentPane().add(jbOK,gbc);
gbc.gridx=4;
gbc.gridy=2;
jd.getContentPane().add(jbCancel,gbc);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf2.getText().equals("PLAIN"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=0;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
// System.out.println(f.getFont("楷体_GB2312").getFontName());
}
if(jtf2.getText().equals("BOLD"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=1;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
if(jtf2.getText().equals("ITALIC"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=2;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
jd.dispose();
//System.out.println(fontstyle);
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jd.dispose();
}
}
);
jd.setSize(200,120);
jd.setResizable(false);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("关于记事本"))
{
JOptionPane jop=new JOptionPane(null,JOptionPane.INFORMATION_MESSAGE);
jop.showMessageDialog(null,"小廖记事本","关于记事本",JOptionPane.OK_OPTION);
//System.out.println("OK");
}
}
class UndoHander implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent eundo)
{
undo.addEdit(eundo.getEdit());
}
}
}
我给你如下:/*
* WriteBoard.java
*
* Created on 2006年12月19日, 下午7:26
*/
/**
*
* @author LecH.giF
*/
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.awt.FileDialog;
public class WriteBoard extends java.awt.Frame {
Clipboard clipboard =null;
FileDialog fc = new FileDialog(this);
/** Creates new form WriteBoard */
public WriteBoard() {
clipboard = getToolkit().getSystemClipboard();
initComponents();
}
/** 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.
*/
// editor-fold defaultstate="collapsed" desc=" Generated Code "
private void initComponents() {
textArea1 = new java.awt.TextArea();
menuBar1 = new java.awt.MenuBar();
menu1 = new java.awt.Menu();
menuItem1 = new java.awt.MenuItem();
menuItem2 = new java.awt.MenuItem();
menuItem3 = new java.awt.MenuItem();
menuItem4 = new java.awt.MenuItem();
menuItem5 = new java.awt.MenuItem();
menu2 = new java.awt.Menu();
menuItem6 = new java.awt.MenuItem();
menuItem7 = new java.awt.MenuItem();
menuItem8 = new java.awt.MenuItem();
setTitle("WriteBoard");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
add(textArea1, java.awt.BorderLayout.CENTER);
menu1.setLabel("Menu");
menuItem1.setLabel("\u65b0\u5efa");
menuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newText(evt);
}
});
menu1.add(menuItem1);
menuItem2.setLabel("\u6253\u5f00");
menuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
open(evt);
}
});
menu1.add(menuItem2);
menuItem3.setLabel("\u4fdd\u5b58");
menuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem3ActionPerformed(evt);
}
});
menu1.add(menuItem3);
menuItem4.setLabel("\u53e6\u5b58\u4e3a");
menuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem4ActionPerformed(evt);
}
});
menu1.add(menuItem4);
menuItem5.setLabel("\u9000\u51fa");
menuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exit(evt);
}
});
menu1.add(menuItem5);
menuBar1.add(menu1);
menu2.setLabel("\u7f16\u8f91");
menuItem6.setLabel("\u526a\u5207");
menuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem6ActionPerformed(evt);
}
});
menu2.add(menuItem6);
menuItem7.setLabel("\u590d\u5236");
menuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem7ActionPerformed(evt);
}
});
menu2.add(menuItem7);
menuItem8.setLabel("\u7c98\u8d34");
menuItem8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem8ActionPerformed(evt);
}
});
menu2.add(menuItem8);
menuBar1.add(menu2);
setMenuBar(menuBar1);
pack();
}// /editor-fold
private void menuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
PrintWriter pw = new PrintWriter(file);
pw.print(textArea1.getText());
pw.flush();
pw.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void menuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
PrintWriter pw = new PrintWriter(file);
pw.print(textArea1.getText());
pw.flush();
pw.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void menuItem8ActionPerformed(java.awt.event.ActionEvent evt) {
Transferable contents = clipboard.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor))
try{
String str;
str=(String)contents.getTransferData(flavor);
textArea1.append(str);
}catch(Exception e){}
}
private void menuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
String temp = this.textArea1.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
}
private void menuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
String temp = this.textArea1.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
int start = textArea1.getSelectionStart();
int end = textArea1.getSelectionEnd();
textArea1.replaceRange("",start,end);
}
private void open(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s;
try {
while((s= br.readLine())!=null){
textArea1.append(s+"\n");
}
fr.close();
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void newText(java.awt.event.ActionEvent evt) {
this.textArea1.setText("");
}
private void exit(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WriteBoard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Menu menu1;
private java.awt.Menu menu2;
private java.awt.MenuBar menuBar1;
private java.awt.MenuItem menuItem1;
private java.awt.MenuItem menuItem2;
private java.awt.MenuItem menuItem3;
private java.awt.MenuItem menuItem4;
private java.awt.MenuItem menuItem5;
private java.awt.MenuItem menuItem6;
private java.awt.MenuItem menuItem7;
private java.awt.MenuItem menuItem8;
private java.awt.TextArea textArea1;
// End of variables declaration
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class test implements ActionListener
{
JFrame frame;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;
JTextArea ta;
JPanel p1,p2,p3,p4;
JMenuBar mb;
JMenu m1,m2,m3;
JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;
JRadioButton rb1,rb2;
ButtonGroup bg;
Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;
String s1="",s2="",s3="",s4="";
int a=0;
char c1;
int i=0;
public static void main(String[] args)
{
test that=new test();
that.go();
}
public void go()
{
frame=new JFrame("计算器");
Container cp= frame.getContentPane();
cp.setLayout(new FlowLayout());
b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");
b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");
b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");
b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");
b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");
mb=new JMenuBar();
m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");
mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");
ta=new JTextArea(1,30);
p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();
rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");
bg=new ButtonGroup();
b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);
b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);
b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);
b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);
b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);
b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);
b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);
b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);
b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);
b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);
b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);
b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);
b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);
b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);
b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);
b31.setForeground(Color.red);b31.setBackground(Color.white);
bg.add(rb1);bg.add(rb2);
p1.setBackground(Color.yellow);
cp.setBackground(Color.CYAN);
m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);
m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);
mb.add(m1);mb.add(m2);mb.add(m3);
frame.setJMenuBar(mb);
p2.setLayout(new GridLayout(4,7));
p3.setLayout(new GridLayout(1,3));
ta.setEditable(false);
p1.add(ta);
p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);
p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);
p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);
p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);
p3.add(b29);p3.add(b30);p3.add(b31);
Border etched=BorderFactory.createEtchedBorder();
Border border=BorderFactory.createTitledBorder(etched,"计算类型");
p4.add(rb1);p4.add(rb2);
p4.setBorder(border);
b2.setActionCommand("8");
b2.addActionListener(this);
cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);
frame.setSize(400,330);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.setActionCommand("7");
b1.addActionListener(this);
b2.setActionCommand("8");
b2.addActionListener(this);
b3.setActionCommand("9");
b3.addActionListener(this);
b4.setActionCommand("/");
b4.addActionListener(this);
b5.setActionCommand("1/x");
b5.addActionListener(this);
b6.setActionCommand("sin");
b6.addActionListener(this);
b7.setActionCommand("log");
b7.addActionListener(this);
b8.setActionCommand("4");
b8.addActionListener(this);
b9.setActionCommand("5");
b9.addActionListener(this);
b10.setActionCommand("6");
b10.addActionListener(this);
b11.setActionCommand("*");
b11.addActionListener(this);
b12.setActionCommand("x^y");
b12.addActionListener(this);
b13.setActionCommand("cos");
b13.addActionListener(this);
b14.setActionCommand("ln");
b14.addActionListener(this);
b15.setActionCommand("1");
b15.addActionListener(this);
b16.setActionCommand("2");
b16.addActionListener(this);
b17.setActionCommand("3");
b17.addActionListener(this);
b18.setActionCommand("-");
b18.addActionListener(this);
b19.setActionCommand("x!");
b19.addActionListener(this);
b20.setActionCommand("tan");
b20.addActionListener(this);
b21.setActionCommand("x^3");
b21.addActionListener(this);
b22.setActionCommand("0");
b22.addActionListener(this);
b23.setActionCommand("+/-");
b23.addActionListener(this);
b24.setActionCommand(".");
b24.addActionListener(this);
b25.setActionCommand("+");
b25.addActionListener(this);
b26.setActionCommand("√x");
b26.addActionListener(this);
b27.setActionCommand("cot");
b27.addActionListener(this);
b28.setActionCommand("x^2");
b28.addActionListener(this);
b29.setActionCommand("Backspace");
b29.addActionListener(this);
b30.setActionCommand("C");
b30.addActionListener(this);
b31.setActionCommand("=");
b31.addActionListener(this);
rb1.setActionCommand("kxx");
rb1.addActionListener(this);
rb2.setActionCommand("bzx");
rb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) //throws Exception
{
if (e.getActionCommand()=="bzx")
{
b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);
b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);
b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);
b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);
}
if (e.getActionCommand()=="kxx")
{
b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);
b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);
b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);
b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);
}
if (e.getActionCommand()=="1")
{
ta.append("1");
}
if (e.getActionCommand()=="2")
{
ta.append("2");
}
if (e.getActionCommand()=="3")
{
ta.append("3");
}
if (e.getActionCommand()=="4")
{
ta.append("4");
}
if (e.getActionCommand()=="5")
{
ta.append("5");
}
if (e.getActionCommand()=="6")
{
ta.append("6");
}
if (e.getActionCommand()=="7")
{
ta.append("7");
}
if (e.getActionCommand()=="8")
{
ta.append("8");
}
if (e.getActionCommand()=="9")
{
ta.append("9");
}
if (e.getActionCommand()=="0")
{
ta.append("0");
}
if (e.getActionCommand()=="+")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=1;
}
if (e.getActionCommand()=="-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=2;
}
if (e.getActionCommand()=="*")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=3;
}
if (e.getActionCommand()=="/")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=4;
}
if (e.getActionCommand()=="=")
{
s2=ta.getText();
d2=Double.parseDouble(s2);
if(i==1)
{
d3=d1+d2;
ta.setText( d3.toString());
}
if(i==2)
{
d3=d1-d2;
ta.setText( d3.toString());
}
if(i==3)
{
d3=d1*d2;
ta.setText( d3.toString());
}
if(i==4)
{
if(d2==0.0)
ta.setText("ERROR");
else
{
d3=d1/d2;
ta.setText( d3.toString());
}
}
if (i==5)
{
s2=ta.getText();
d2 = Double.parseDouble(s2);
for (int l=1;l=d2 ; l++)
{
d5=d5*d1;
}
ta.setText( d5.toString());
}
}
if (e.getActionCommand()=="C")
{
ta.setText("");
d4=1.0;
d5=1.0;
}
/*if (e.getActionCommand()=="Backspace")
{
s3=ta.getText();
a=s3.length();
//ta.cut(ta.select(a-1,a));
s4=ta.getText(1,3);
ta.setText(s4);
}
*/
if (e.getActionCommand()=="1/x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=1/d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()==".")
{
ta.append(".");
}
if (e.getActionCommand()=="+/-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=0-d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^2")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^3")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^y")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=5;
// d2=d1*d1*d1;
// ta.setText( d2.toString());
}
if (e.getActionCommand()=="√x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sqrt(d1);
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x!")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
if (d10)
{
ta.setText( "error");
}
else if (d1==0)
{
ta.setText( "0.0");
}
else {
for (int k=1;k=d1 ;k++ )
d4=d4*k;
ta.setText( d4.toString());
}
}
if (e.getActionCommand()=="sin")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sin(3.1415926*d1/180);
ta.setText( d2.toString());
}
}
}
给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新记事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("开始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替换为");
private JButton jba=new JButton("全部替换");
private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替换");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新记事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打开".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag){
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤销".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=jta.getText();
jta.cut();
}else if("复制".equals(comm)){
jta.copy();
}else if("粘贴".equals(comm)){
value=jta.getText();
jta.paste();
}else if("删除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全选".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替换".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("开始".equals(comm)||"下一个".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一个");
// value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("替换为".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替换".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp+new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","编辑","帮助"};
String[][] menuItemLab={{"新建","打开","保存","另存","退出"},
{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},
{"版本"}};
for(int i=0;imenuLab.length;i++){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自动换行
JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
if(p.getActionCommand()=="查找 "||p.getSource()==findc) // 创建查找对话框;
{find=new Dialog(this,"查找");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
findtxt=new JTextField(7);
p1.add(new JLabel("输入要查找字符:"));
p1.add(findtxt);
p2.add(findenter);
find.add("Center",p1);
find.add("South",p2);
find.setSize(200,100);
find.show();
}
if(p.getSource()==findenter) ///点击查找对话框的确定按钮后的事件处理
{
if(findtxt.getText().equals(""))
{find.dispose();}
else if(!findtxt.getText().equals(""))
{ find.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.select(startp,endp);
m26.setEnabled(true);
newendp=endp;////////获取这次查找的终点
notfindmethod();
}
}
//////////////////////////////////////////////////////////
if(p.getActionCommand()=="查找下一个 ")
{
nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall=txt1.getText();
txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp); ////找到相应文本,并选择
notfindmethod();
}
if(p.getActionCommand()=="替换 ")
{m271.setEnabled(true);
replace=new Dialog(this,"替换"); // 创建替换对话框;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
findtxt=new JTextField(7);
repltxt=new JTextField(7);
p1.add(new JLabel("输入原始字符:"));
p1.add(findtxt);
p2.add(new JLabel("输入替换字符:"));
p2.add(repltxt);
p3.add(replb);
replace.add("North",p1);
replace.add("Center",p2);
replace.add("South",p3);
replace.setSize(200,150);
replace.show();
}
if(p.getSource()==replb) ///点击替换对话框的确定按钮后的事件处理
{
replace.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.replaceRange(repltxt.getText(),startp,endp);
newendp=endp; ///获取这次替换的终点
}
if(p.getActionCommand()=="替换下一个")
{nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall=txt1.getText();
txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp);
notfindmethod();
txt1.replaceRange(repltxt.getText(),newstartp,newendp);//替换字符
}
if(p.getActionCommand()=="全选 ")
{txt1.selectAll(); }
要看全部代码,我空间里有,自己看
你有没有写监听器啊,继承ActionListener,按钮必须添加监听器后才会起作用。
重写actionPerformed(ActionEvent e) 方法,把你按钮执行的动作放在这个方法里才行。