功能界面展示:
记事本完整代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.io.*;
@SuppressWarnings("serial")
class mynotepad extends JFrame {
File file = null;
notepad++ JTextArea content = new JTextArea();
JFileChooser openfile = new JFileChooser();
JColorChooser opencolor = new JColorChooser();
JDialog about = new JDialog(this);
JMenuBar menu = new JMenuBar();
JFrame p=new JFrame();
boolean WordWrap = false;
boolean status=false;
mynotepad() {
initTextContent();
initMenu();
}
void initTextContent() {
JScrollPane scroll = new JScrollPane(content);
getContentPane().add(scroll);
ContentPane().add(scroll, BorderLayout.CENTER);
}
JMenu[] menus = new JMenu[] { new JMenu("文件(F)"), new JMenu("编辑(E)"),
new JMenu("格式(O)"), new JMenu("查(V)"), new JMenu("帮助(H)") };
JMenuItem optionofmenu[][] = new JMenuItem[][] {
{ new JMenuItem("新建"), new JMenuItem("打开"), new JMenuItem("保存
"),
new JMenuItem("退出") },
{new JMenuItem("复制"), new JMenuItem("剪切"), new JMenuItem("粘
贴"),
new JMenuItem("全选"), new JMenuItem("查"),new
JMenuItem("替换") },
{ new JCheckBoxMenuItem("自动换行(W)"), new JMenuItem("字体(F)") },
{ new JMenuItem("状态栏(S)") },
{ new JMenuItem("帮助") } };
JLabel js=new JLabel();
void initMenu() {
for (int i = 0; i < menus.length; i++) {
menu.add(menus[i]);
for (int j = 0; j < optionofmenu[i].length; j++) {
menus[i].add(optionofmenu[i][j]);
optionofmenu[i][j].addActionListener(action);
}
}
menu.add(js);
menus[0].setMnemonic(KeyEvent.VK_F);
menus[1].setMnemonic(KeyEvent.VK_E);
menus[2].setMnemonic(KeyEvent.VK_O);
menus[3].setMnemonic(KeyEvent.VK_V);
menus[4].setMnemonic(KeyEvent.VK_H);
optionofmenu[2][0].setMnemonic(KeyEvent.VK_W);
optionofmenu[2][1].setMnemonic(KeyEvent.VK_F);
optionofmenu[3][0].setMnemonic(KeyEvent.VK_S);
optionofmenu[1][4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JDialog dialog = new JDialog(about, "查字符串...",
true);
dialog.setBounds(560, 250, 310, 130);
JLabel find = new JLabel("请输入字符串 :");
final JTextField findtext = new JTextField(1);
JButton jbu = new JButton("查");
dialog.setLayout(null);
find.setBounds(10, 30, 90, 20);
findtext.setBounds(100, 30, 90, 20);
jbu.setBounds(200, 30, 80, 20);
dialog.add(find);
dialog.add(findtext);
dialog.add(jbu);
jbu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = Text();
String str = Text();
int end = text.length();
int len = str.length();
int start = SelectionEnd();
if (start == end) {
start = 0;
}
for (; start <= end - len; start++) {
if (text.substring(start, start +
len).equals(str)) {
content.setSelectionStart
(start);
content.setSelectionEnd
(start + len);
return;
}
}
// 若不到待查字符串,则将光标置于末尾
content.setSelectionStart(end);
content.setSelectionEnd(end);
}
});
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dialog.dispose();
}
});
dialog.setResizable(false);
dialog.setVisible(true);
}
});
optionofmenu[1][5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JDialog dialog = new JDialog(about, "字符串替换...",
true);
dialog.setBounds(560, 250, 310, 180);
final JLabel tihuan = new JLabel("请输入要替换的字符串
:");
final JLabel mubiao = new JLabel("请输入替换后的字符串
:");
final JTextField jtf1 = new JTextField(10);
final JTextField jtf2 = new JTextField(10);
JButton jb = new JButton("替换");
dialog.setLayout(null);
tihuan.setBounds(10, 30, 150, 20);
mubiao.setBounds(10, 70, 150, 20);
jtf1.setBounds(160, 30, 110, 20);
jtf2.setBounds(160, 70, 110, 20);
jb.setBounds(100, 110, 80, 20);
dialog.add(tihuan);
dialog.add(mubiao);
dialog.add(jtf1);
dialog.add(jtf2);
dialog.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Text().contains
(Text()))
content.Text
().replaceFirst(
Text(),
Text()));
else {
JOptionPane.showConfirmDialog(null,
"没到 " +
Text());
}
}
});
dialog.setResizable(false);
dialog.setVisible(true);
}
});
JToolBar jb=new JToolBar();
jb.setBackground(new Color(236,233,216));
JLabel jl1=new JLabel(String.format("字符总长:0 总行数:0 "));
jl1.setFont(new Font("宋体",0,12));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论