📄 一个记事本的源代码.htm
字号:
windowClosing(WindowEvent e) { <BR>dispoe(); <BR>FindStartPos = 0; <BR>} <BR>});
<BR>dr.setSize(220,138); <BR>dr.setVisible(true); <BR><BR>} <BR>else
if(ae.getSource() == menuEditGoTo) { <BR>JDialog dg = new JDialog(this, "Line
Number", true); <BR>dg.getContentPane().setLayout(new FlowLayout()); <BR>final
JTextField dgtf = new JTextField(4); <BR>Button dgOk = new Button("Go To");
<BR>dgOk.addActionListener(new ActionListener() { <BR>public void
actionPerformed(ActionEvent brce) { <BR>int totalLine = ta.getLineCount();
<BR>int[] lineNumber = new int[totalLine + 1]; <BR>String s = ta.getText();
<BR>int pos = 0, t = 0; <BR>while(true) { <BR>pos = s.indexOf(´\12´, pos);
<BR>if(pos == -1) <BR>break; <BR>lineNumber[t++] = pos++; <BR>} <BR>int gt = 1;
<BR>try { <BR>gt = Integer.parseInt(dgtf.getText()); <BR>}
catch(NumberFormatException efe) { <BR>JOptionPane.showMessageDialog(null,
"Please enter an integer.", "Input",1); <BR>} <BR>if(gt < 2 || gt >=
totalLine) { <BR>if(gt < 2) <BR>ta.setCaretPosition(0); <BR>else
<BR>ta.setCaretPosition(s.length()); <BR>} <BR>else
<BR>ta.setCaretPosition(lineNumber[gt-2] + 1); <BR>dispose(); <BR>} <BR>});
<BR>Button dgCancel = new Button("Cancel"); <BR>dgCancel.addActionListener(new
ActionListener() { <BR>public void actionPerformed(ActionEvent brce) {
<BR>dispose(); <BR>} <BR>}); <BR>dg.getContentPane().add(dgtf);
<BR>dg.getContentPane().add(dgOk); <BR>dg.getContentPane().add(dgCancel);
<BR>dg.setResizable(false); <BR>dg.setLocation(120, 120);
<BR>dg.addWindowListener(new WindowAdapter() { <BR>public void
windowClosing(WindowEvent e) { <BR>dispose(); <BR>} <BR>});
<BR>dg.setSize(180,60); <BR>dg.setVisible(true); <BR>} <BR>else
if(ae.getSource() == menuEditSelectAll) { <BR>ta.selectAll(); <BR>} <BR>else
if(ae.getSource() == menuViewFont) { <BR>MenuFont mf = new MenuFont(this, true);
<BR>ta.setFont(mf.myLayout(ta.getFont())); <BR>} <BR>else if(ae.getSource() ==
menuViewColor) { <BR>MenuColor mc = new MenuColor(this, true); <BR>Color[] fbgc
= new Color[2]; <BR>fbgc = mc.myLayout(ta.getForeground(), ta.getBackground());
<BR>ta.setForeground(fbgc[0]); <BR>ta.setBackground(fbgc[1]);
<BR>ta.setCaretColor(fbgc[0]); <BR>} <BR>else if(ae.getSource() ==
menuViewClassic) { <BR>if(menuViewClassic.getState()) { <BR>ta.setForeground(new
Color(0, 255, 0)); <BR>ta.setBackground(new Color(45, 0, 45));
<BR>ta.setFont(new Font("Serif", Font.BOLD, 16)); <BR>ta.setCaretColor(new
Color(0, 255, 0)); <BR>} <BR>else { <BR>ta.setForeground(defaultForeground);
<BR>ta.setBackground(defaultBackground); <BR>ta.setFont(defaultFont);
<BR>ta.setCaretColor(defaultCaretColor); <BR>} <BR>} <BR>else if(ae.getSource()
== menuViewStatus) { <BR>if(menuViewStatus.getState()) { <BR>showStatus(); <BR>}
<BR>} <BR>else if(ae.getSource() == menuViewWordWrap) {
<BR>if(menuViewWordWrap.getState()) { <BR>ta.setLineWrap(true); <BR>} <BR>else {
<BR>ta.setLineWrap(false); <BR>} <BR>} <BR>else if(ae.getSource() ==
menuViewDoubleSpace) { <BR>int pos = 0, t = 0; <BR>String str = ta.getText();
<BR>while(true){ <BR>pos = str.indexOf(´\15´, pos); <BR>if(pos == -1) break;
<BR>str = str.substring(0, pos) + ´\15´ + ´\12´ + str.substring(pos); <BR>pos =
pos + 3; <BR>} <BR>ta.setText(str); <BR>} <BR>else if(ae.getSource() ==
menuHelpAbout) { <BR>dl = new JDialog(this,"About MiniEditor", true);
<BR>dl.getContentPane().setLayout(new GridLayout(3,3)); <BR>dl.setBackground(new
Color(212,208,200)); <BR>Button bOk = new Button("OK");
<BR>bOk.addActionListener(new ActionListener() { <BR>public void
actionPerformed(ActionEvent bOke) { <BR>dispose(); <BR>} <BR>}); <BR>Label ver =
new Label("Version .9"); <BR>Label null1 = new Label(); <BR>Label null2 = new
Label(); <BR>Label null3 = new Label(); <BR>Label null4 = new Label(); <BR>Label
null5 = new Label(); <BR>Label null6 = new Label(); <BR>Label null7 = new
Label(); <BR>dl.getContentPane().add(null1); <BR>dl.getContentPane().add(ver);
<BR>dl.getContentPane().add(null2); <BR>dl.getContentPane().add(null3);
<BR>dl.getContentPane().add(bOk); <BR>dl.getContentPane().add(null4);
<BR>dl.getContentPane().add(null5); <BR>dl.getContentPane().add(null6);
<BR>dl.getContentPane().add(null7); <BR>bOk.addActionListener(this);
<BR>dl.addWindowListener(new WindowAdapter() { <BR>public void
windowClosing(WindowEvent e) { <BR>dispose(); <BR>} <BR>});
<BR>dl.setLocation(120, 120); <BR>dl.setResizable(false);
<BR>dl.setSize(200,80); <BR>dl.setVisible(true); <BR><BR>} <BR>}//end of
ActionListener <BR><BR>//DocumentListener <BR>public void
removeUpdate(DocumentEvent e) { <BR>String s; <BR>s = fileStatus.getText();
<BR>if(!s.endsWith("*") & beginTextListener & !isNewFile) {
<BR>fileStatus.setText("*"); <BR>} <BR>menuEditUndo.setEnabled(true); <BR>}
<BR>public void insertUpdate(DocumentEvent e) { <BR>String s; <BR>s =
fileStatus.getText(); <BR>if(!s.endsWith("*") & beginTextListener &
!isNewFile) { <BR>fileStatus.setText("*"); <BR>}
<BR>menuEditUndo.setEnabled(true); <BR>} <BR>public void
changedUpdate(DocumentEvent e) { <BR>String s; <BR>s = fileStatus.getText();
<BR>if(!s.endsWith("*") & beginTextListener & !isNewFile) {
<BR>fileStatus.setText("*"); <BR>} <BR>menuEditUndo.setEnabled(true); <BR>}
<BR>//end of DocumentListener <BR><BR>// Caretlistener <BR>public void
caretUpdate(CaretEvent e) { <BR>if(menuViewStatus.getState()) <BR>showStatus();
<BR>}// end of Caretlistener <BR><BR>// KeyListener <BR>public void
keyPressed(KeyEvent e) { <BR>if(e.getKeyCode() == ´\10´) { <BR>BACKSPACE = true;
<BR>} <BR>//if(menuViewStatus.getState()) <BR>// showStatus(); <BR>} <BR>public
void keyReleased(KeyEvent e) { <BR>if(e.getKeyCode() == 155) { //ESCAPE = 155
<BR>if(INSERTMODE) <BR>INSERTMODE = false; <BR>else <BR>INSERTMODE = true; <BR>}
<BR>if(menuViewStatus.getState()) <BR>showStatus(); <BR>} <BR>public void
keyTyped(KeyEvent e) { <BR>beginTextListener = true; <BR>isNewFile = false;
<BR>if(!BACKSPACE) { <BR>if(!INSERTMODE) { <BR>int pos = ta.getCaretPosition();
<BR>char c = ta.getText().charAt(pos); <BR>if(c == ´\12´) { <BR>} <BR>else if(c
== ´\15´) { <BR>} <BR>else { <BR>ta.replaceRange("", pos, pos + 1); <BR>} <BR>}
<BR>} <BR>BACKSPACE = false; <BR>}// end of KeyListener <BR><BR>void
showStatus() { <BR>int rows, cols, from, current, to, fileSize; <BR>rows = cols
= from = current = 0; <BR>to = ta.getCaretPosition(); <BR>fileSize = 0;
<BR>String str = ta.getText(); <BR>cols = to - str.substring(0,
to).lastIndexOf(10); <BR>fileSize = str.length(); <BR>String mode;
<BR>if(INSERTMODE) { <BR>mode = "INSERT"; <BR>} <BR>else { <BR>mode = "OVERLAY";
<BR>} <BR>try { <BR>rows = ta.getLineOfOffset(to) + 1; <BR>}
catch(BadLocationException ble) { <BR>} <BR>statusRow.setText("row: " + rows);
<BR>statusCol.setText("col: " + cols); <BR>statusMode.setText("mode: " + mode);
<BR>statusSize.setText("size: " + fileSize); <BR>//fileStatus.setText("file
status: "); <BR>} <BR><BR>int wordLocation(String str, int pos, boolean
isToRight) { <BR>char c; <BR>if(isToRight) { <BR>c = str.charAt(pos);
<BR>while(true) { <BR>if(c < 48) break; <BR>else if(c > 57 & c <
65) break; <BR>else if(c > 90 & c < 97) break; <BR>else if(c > 122)
break; <BR>pos++; <BR>c = str.charAt(pos); <BR>} <BR>return pos--; <BR>}
<BR>else { <BR>pos--; <BR>c = str.charAt(pos); <BR>while(true) { <BR>if(c <
48) break; <BR>else if(c > 57 & c < 65) break; <BR>else if(c > 90
& c < 97) break; <BR>else if(c > 122) break; <BR>pos--; <BR>c =
str.charAt(pos); <BR>} <BR>return pos++; <BR>} <BR><BR>} <BR><BR>class
UndoHandler implements UndoableEditListener { <BR>public void
undoableEditHappened(UndoableEditEvent uee) { <BR>undo.addEdit(uee.getEdit());
<BR>} <BR>} <BR><BR>}//end of class MiniEditor <BR><BR><B></B></P>
<P><B>一个记事本的源代码</B> <BR>//保存为MenuColor.java <BR>import java.awt.*; <BR>import
java.awt.event.*; <BR><BR>import javax.swing.*; <BR><BR>public class MenuColor
extends JDialog <BR>implements ItemListener, <BR>ActionListener,
<BR>TextListener, <BR>AdjustmentListener { <BR><BR>CheckboxGroup gp;
<BR>Checkbox fore, back; <BR>Scrollbar scrollbarRed, scrollbarGreen,
scrollbarBlue; <BR>TetField textFieldRed, textFieldGreen, textFieldBlue;
<BR>Button colorButtonOk, colorButtonCancel; <BR>Checkbox colorCheckbox;
<BR>TextField colorTextField; <BR>boolean changed = true; <BR>boolean
synchronism = false; <BR>Color[] fbgc = new Color[2]; <BR>Color[] fbgcOld = new
Color[2]; <BR>boolean isFore = true; <BR><BR>MenuColor(Frame frame, boolean
modal) { <BR>super(frame, modal); <BR>} <BR><BR>public Color[] myLayout(Color
fgc, Color bgc) { <BR>fbgc[0] = fgc; <BR>fbgc[1] = bgc; <BR>fbgcOld[0] = fgc;
<BR>fbgcOld[1] = bgc; <BR>this.getContentPane().setLayout(new GridBagLayout());
<BR>GridBagConstraints gbc = new GridBagConstraints(); <BR>gbc.gridwidth = 1;
<BR>gbc.gridheight =1; <BR>gbc.weightx = 1; <BR>gbc.weighty = 1; <BR>gbc.fill =
gbc.HORIZONTAL; <BR>gbc.anchor = gbc.CENTER; <BR><BR>//CheckboxGroup for fore
and back ground <BR>gp = new CheckboxGroup(); <BR>fore = new
Checkbox("Foreground", true, gp); <BR>back = new Checkbox("Background", false,
gp); <BR>gbc.gridx = 1; <BR>gbc.gridy = 0; <BR>getContentPane().add(fore, gbc);
<BR>gbc.gridx = 3; <BR>gbc.gridy = 0; <BR>getContentPane().add(back, gbc);
<BR>fore.addItemListener(this); <BR>back.addItemListener(this); <BR><BR>// 3
groups of Label + ScrollBar + TextField <BR>Label labelRed = new Label("Red");
<BR>gbc.gridx = 0; <BR>gbc.gridy = 1; <BR>getContentPane().add(labelRed, gbc);
<BR>scrollbarRed = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
<BR>scrollbarRed.setValue(fgc.getRed()); <BR>gbc.gridx = 1; <BR>gbc.gridy = 1;
<BR>getContentPane().add(scrollbarRed, gbc);
<BR>scrollbarRed.addAdjustmentListener(this); <BR>textFieldRed = new
TextField(3); <BR>textFieldRed.setText(scrollbarRed.getValue() + "");
<BR>textFieldRed.addTextListener(this); <BR>//textFieldRed.setEditable(false);
<BR>gbc.gridx = 2; <BR>gbc.gridy = 1; <BR>getContentPane().add(textFieldRed,
gbc); <BR><BR>Label labelGreen = new Label("Green"); <BR>gbc.gridx = 0;
<BR>gbc.gridy = 2; <BR>getContentPane().add(labelGreen, gbc); <BR>scrollbarGreen
= new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
<BR>scrollbarGreen.setValue(fgc.getGreen()); <BR>gbc.gridx = 1; <BR>gbc.gridy =
2; <BR>getContentPane().add(scrollbarGreen, gbc);
<BR>scrollbarGreen.addAdjustmentListener(this); <BR>textFieldGreen = new
TextField(3); <BR>textFieldGreen.setText(scrollbarGreen.getValue() + "");
<BR>//textFieldGreen.addTextListener(this);
<BR>textFieldGreen.setEditable(false); <BR>gbc.gridx = 2; <BR>gbc.gridy = 2;
<BR>getContentPane().add(textFieldGreen, gbc); <BR><BR>Label labelBlue = new
Label("Blue"); <BR>gbc.gridx = 0; <BR>gbc.gridy = 3;
<BR>getContentPane().add(labelBlue, gbc); <BR>scrollbarBlue = new
Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
<BR>scrollbarBlue.setValue(fgc.getBlue()); <BR>gbc.gridx = 1; <BR>gbc.gridy = 3;
<BR>getContentPane().add(scrollbarBlue, gbc);
<BR>scrollbarBlue.addAdjustmentListener(this); <BR>textFieldBlue = new
TextField(3); <BR>textFieldBlue.setText(scrollbarBlue.getValue() + "");
<BR>//textFieldBlue.addTextListener(this); <BR>textFieldBlue.setEditable(false);
<BR>gbc.gridx = 2; <BR>gbc.gridy = 3; <BR>getContentPane().add(textFieldBlue,
gbc); <BR><BR>//2 Buttons: Ok and Cancel <BR>colorButtonOk = new Button("Ok");
<BR>gbc.gridx = 4; <BR>gbc.gridy = 1; <BR>gbc.fill = gbc.NONE;
<BR>getContentPane().add(colorButtonOk,gbc);
<BR>colorButtonOk.addActionListener(this); <BR><BR>colorButtonCancel = new
Button("Cancel"); <BR>gbc.gridx = 4; <BR>gbc.gridy = 2;
<BR>getContentPane().add(colorButtonCancel,gbc);
<BR>colorButtonCancel.addActionListener(this); <BR><BR>//a Checkbox for locking
RGB Scrollbar to synchronize <BR>colorCheckbox = new Checkbox("Lock RGB",
false); <BR>gbc.gridx = 3; <BR>gbc.gridy = 3;
<BR>getContentPane().add(colorCheckbox,gbc);
<BR>colorCheckbox.addItemListener(this); <BR><BR>//a TextField for demo the
color <BR>colorTextField = new TextField("Java awt"); <BR>//have to disable this
because of 70% decrease in color when setEditable(false) <BR>//and color can´t
be corrected by increasing 1/(70%) since >255
<BR>//colorTextField.setEditable(false); <BR>colorTextField.setSize(90, 60);
<BR>colorTextField.setForeground(fgc); <BR>colorTextField.setBackground(bgc);
<BR>colorTextField.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 36));
<BR>gbc.gridx = 3; <BR>gbc.gridy = 1; <BR>gbc.fill = gbc.BOTH;
<BR>getContentPane().add(colorTextField, gbc);
<BR><BR>this.addWindowListener(new WindowAdapter() { <BR>public void
windowClosing(WindowEvent e) { <BR>dispose(); <BR>} <BR>});
<BR>this.setLocation(120, 120); <BR>this.setResizable(false);
<BR>this.setSize(480,160); <BR>this.setVisible(true); <BR>if(changed) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -