📄 jishiben.java
字号:
FindDialog dFind = new FindDialog(text, JiShiBen.this);
dFind.setVisible(true);
}
private void numPerformed() {
num=text.getText().length();
JOptionPane.showMessageDialog(this, "当前字符个数为:"+num);
}
private void metalPerformed(){
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel") ;
}
catch(Exception e)
{e.printStackTrace();
}
}
private void motifPerformed(){
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel") ;
}
catch(Exception e)
{e.printStackTrace();
}
}
private void windowsPerformed(){
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel") ;
}
catch(Exception e)
{e.printStackTrace();
}
}
/**
*<p>Title: </p>
*<p>Description: 处理菜单出发的事件</p>
*<p>Copyright: Copyright (c) 2005</p>
*<p>Company: </p>
*@author 红色彼岸
*@version 0.1
*/
//内部类,监听文挡undo
class UndoHander implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent eundo)
{
undo.addEdit(eundo.getEdit());
}
}
//内部类,监听工具栏字体复选框
class CheckBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(bold.isSelected())
text.setFont(new Font(font,Font.BOLD,m));
if(italic.isSelected())
text.setFont(new Font(font,Font.ITALIC,m));
}
}
//内部类,监听菜单项目
class MenuItemEventHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
if(item == mi_File_New||item==pi_New)
newPerformed();
else if(item == mi_File_Open)
openPerformed();
else if(item == mi_File_Save )
savePerformed();
else if(item == mi_File_LSave)
saveAsPerformed();
// else if(item == pageSetItem)
//pageSetPerformed();
// else if(item == printItem)
// printPerformed();
else if(item == mi_File_Exit)
exitPerformed();
else if(item == undoItem)
undoPerformed();
else if(item == redoItem)
redoPerformed();
else if(item == mi_Edit_Cut||item==pi_Cut)
cutPerformed();
else if(item == mi_Edit_Copy||item==pi_Copy)
copyPerformed();
else if(item == pasteItem||item==pi_Paste)
pastePerformed();
else if(item == pi_Del)
deletePerformed();
else if(item==foreColor)
foreColorPerformed();
else if(item==backColor)
backColorPerformed();
else if(item == selectAll||item==pi_selectAll)
selectAllPerformed();
else if(item == dateTime)
timePerformed();
else if(item == replaceItem)
replacePerformed();
// else if(item == goToLineItem)
//goToLinePerformed();
else if(item == findItem)
findPerformed();
else if(item == findNextItem)
findNextPerformed();
else if(item == numItem)
numPerformed();
else if(item==metal)
metalPerformed();
else if(item==motif)
motifPerformed();
else if(item==windows)
windowsPerformed();
}
}
//类SimpleEditor作为动作事件的监听器,方法中要区分事件源
public void actionPerformed(ActionEvent e){
Object m=e.getSource();
if((JButton)m==cutBtn)
{
cutPerformed();
}
else if((JButton)m ==saveBtn)
{
savePerformed();
}
else if((JButton)m ==newBtn)
{
newPerformed();
}
else if(m==copyBtn)
{
copyPerformed();
}
else if(m==pasteBtn)
{
pastePerformed();
}
else if(m==openBtn)
openPerformed();
else if(m==undoBtn)
undoPerformed();
else if(m==redoBtn)
redoPerformed();
}
public static void main(String args[])
{
JiShiBen f = new JiShiBen();
f.setSize(300,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
class FindDialog extends JDialog implements ActionListener, WindowListener,
KeyListener {
final JTextArea textArea;
JLabel lfind, lreplace;
JTextField tfind, treplace;
JButton bfind, breplace, bnext, bcancel;
JPanel fieldPane, buttonPane;
String strFind, strReplace;
int txtPlace, txtLen, strNext;
FindDialog(final JTextArea textArea, JFrame findDialog) {
super(findDialog, "查找/替换", false);
this.textArea = textArea;
Container findPane = getContentPane();
lfind = new JLabel("查找内容");
lreplace = new JLabel("替换");
tfind = new JTextField();
treplace = new JTextField();
bfind = new JButton("查找", new ImageIcon("ico/Find16.gif"));
breplace = new JButton("替换", new ImageIcon("ico/Replace16.gif"));
bcancel = new JButton("取消", new ImageIcon("ico/exit.gif"));
bnext = new JButton("下一个", new ImageIcon("ico/FindAgain16.gif"));
fieldPane = new JPanel();
buttonPane = new JPanel();
fieldPane.setLayout(new GridLayout(2, 2));
findPane.add(fieldPane, BorderLayout.CENTER);
findPane.add(buttonPane, BorderLayout.SOUTH);
fieldPane.add(lfind);
fieldPane.add(tfind);
fieldPane.add(lreplace);
fieldPane.add(treplace);
buttonPane.add(bfind);
buttonPane.add(breplace);
buttonPane.add(bnext);
buttonPane.add(bcancel);
bfind.addActionListener(this);
breplace.addActionListener(this);
bcancel.addActionListener(this);
this.addWindowListener(this);
tfind.addKeyListener(this);
treplace.addKeyListener(this);
bnext.addActionListener(this);
setSize(370, 120);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bfind) {
find();
}
if (e.getSource() == breplace) {
replace();
}
if (e.getSource() == bcancel) {
setVisible(false);
}
if (e.getSource() == bnext) {
findNext();
}
}
//查找处理
private void find() {
strFind = tfind.getText();
strNext = 0; //开始位置
txtPlace = textArea.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
textArea.select(txtPlace, txtLen);
strNext = textArea.getSelectionEnd(); //选中内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找",
JOptionPane.INFORMATION_MESSAGE);
}
bnext.setEnabled(true);
}
//替换处理
private void replace() {
strReplace = treplace.getText();
textArea.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
}
//查找下一个处理
private void findNext() {
bfind.setEnabled(false);
txtPlace = textArea.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
textArea.select(txtPlace, txtLen);
strNext = textArea.getSelectionEnd(); //获取查找下一个内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找下一个",
JOptionPane.INFORMATION_MESSAGE);
strNext = 0; //没有找到初始化位置,以变重新查找
tfind.setText("");
bnext.setEnabled(false);
breplace.setEnabled(false);
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -