📄 java记事本_增加关闭响应.java
字号:
}
// 全选
else if (e.getSource() == popupMenu_SelectAll || e.getSource() == mEdit_SelectAll) {
Text.selectAll();
}
// 自动换行
else if (e.getSource() == formatMenu_LineWrap) {
if (formatMenu_LineWrap.getState()) {
Text.setLineWrap(true);
} else
Text.setLineWrap(false);
}
// 字体设置
else if (e.getSource() == formatMenu_Font || e.getSource() == fontButton) {
Text.requestFocus();
new MyFont();
}
// 设置字体颜色(前景色)
else if (e.getSource() == formatMenu_Color_FgColor || e.getSource() == fgcolorButton) {
Text.requestFocus();
Color color = JColorChooser.showDialog(this, "更改字体颜色", Color.black);
if (color != null) {
Text.setForeground(color);
} else
return;
}
// 设置编辑区背景颜色
else if (e.getSource() == formatMenu_Color_BgColor || e.getSource() == bgcolorButton) {
Text.requestFocus();
Color color = JColorChooser.showDialog(this, "更改背景颜色", Color.white);
if (color != null) {
Text.setBackground(color);
} else
return;
}
// 设置状态栏可见性
else if (e.getSource() == viewMenu_Status) {
if (viewMenu_Status.getState())
statusBar.setVisible(true);
else
statusBar.setVisible(false);
}
// 帮助主题
else if (e.getSource() == mHelp_HelpTopics || e.getSource() == helpButton) {
JOptionPane.showMessageDialog(this, "记事本支持拖入文本读取\n" + "由于对编码不熟悉保存文\n件时未进行编码转换\n", "帮助主题", JOptionPane.INFORMATION_MESSAGE);
}
// 关于
else if (e.getSource() == mHelp_About) {
JOptionPane.showMessageDialog(this, " VXBB的记事本\n" + " QQ:491697374\n" + " JAVA图形界面练习\n", "关于记事本", JOptionPane.INFORMATION_MESSAGE);
}
// 工具栏"粗体"按钮事件处理
else if (e.getSource() == boldButton) {
Text.requestFocus();
Font tempFont = Text.getFont();
if (Text.getFont().getStyle() == Font.PLAIN) {
tempFont = new Font(Text.getFont().getFontName(), Font.BOLD, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == Font.ITALIC) {
tempFont = new Font(Text.getFont().getFontName(), Font.BOLD + Font.ITALIC, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == Font.BOLD) {
tempFont = new Font(Text.getFont().getFontName(), Font.PLAIN, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == (Font.BOLD + Font.ITALIC)) {
tempFont = new Font(Text.getFont().getFontName(), Font.ITALIC, Text.getFont().getSize());
}
Text.setFont(tempFont);
}
// 工具栏"斜体"按钮事件处理
else if (e.getSource() == italicButton) {
Text.requestFocus();
Font tempFont = Text.getFont();
if (Text.getFont().getStyle() == Font.PLAIN) {
tempFont = new Font(Text.getFont().getFontName(), Font.ITALIC, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == Font.ITALIC) {
tempFont = new Font(Text.getFont().getFontName(), Font.PLAIN, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == Font.BOLD) {
tempFont = new Font(Text.getFont().getFontName(), Font.BOLD + Font.ITALIC, Text.getFont().getSize());
} else if (Text.getFont().getStyle() == (Font.BOLD + Font.ITALIC)) {
tempFont = new Font(Text.getFont().getFontName(), Font.BOLD, Text.getFont().getSize());
}
Text.setFont(tempFont);
}
}/* 方法actionPerformed()结束 */
class Clock extends Thread { // 模拟时钟
public void run() {
while (true) {
GregorianCalendar time = new GregorianCalendar();
int hour = time.get(Calendar.HOUR_OF_DAY);
int min = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
statusLabel2.setText(" 当前时间:" + hour + ":" + min + ":" + second);
try {
Thread.sleep(950);
} catch (InterruptedException exception) {
}
}
}
}
// 用于设置字体的类MyFont
class MyFont implements ActionListener {
final JDialog fontDialog;
final JTextField tfFont, tfSize, tfStyle;
final int fontStyleConst[] = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC };
final JList listStyle, listFont, listSize;
JLabel sample;
JPanel pane1, pane2, pane3, pane4;
// 构造函数MyFont
public MyFont() {
fontDialog = new JDialog(Notepad4.this, "字体设置", true);
Container con = fontDialog.getContentPane();
con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS));
pane1 = new JPanel();
pane2 = new JPanel();
pane3 = new JPanel();
pane4 = new JPanel();
Font currentFont = Text.getFont();
JLabel lblFont = new JLabel("字体(F):");
JLabel lblStyle = new JLabel("字形(Y):");
JLabel lblSize = new JLabel("大小(S):");
lblStyle.setHorizontalAlignment(SwingConstants.CENTER);
lblSize.setHorizontalAlignment(SwingConstants.CENTER);
lblFont.setPreferredSize(new Dimension(91, 20));
lblStyle.setPreferredSize(new Dimension(82, 20));
lblSize.setPreferredSize(new Dimension(100, 20));
tfFont = new JTextField(13);
tfFont.setText(currentFont.getFontName());
tfFont.selectAll();
tfFont.setPreferredSize(new Dimension(200, 20));
tfStyle = new JTextField(10);
if (currentFont.getStyle() == Font.PLAIN)
tfStyle.setText("常规");
else if (currentFont.getStyle() == Font.BOLD)
tfStyle.setText("粗体");
else if (currentFont.getStyle() == Font.ITALIC)
tfStyle.setText("斜体");
else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC))
tfStyle.setText("粗斜体");
tfFont.selectAll();
tfStyle.setPreferredSize(new Dimension(200, 20));
tfSize = new JTextField(7);
tfSize.setText(currentFont.getSize() + "");
tfSize.selectAll();
tfSize.setPreferredSize(new Dimension(200, 20));
final String fontStyle[] = { "常规", "粗体", "斜体", "粗斜体" };
listStyle = new JList(fontStyle);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final String fontName[] = ge.getAvailableFontFamilyNames();
int defaultFontIndex = 0;
for (int i = 0; i < fontName.length; i++) {
if (fontName[i].equals(currentFont.getFontName())) {
defaultFontIndex = i;
break;
}
}
listFont = new JList(fontName);
listFont.setSelectedIndex(defaultFontIndex);
listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFont.setVisibleRowCount(7);
listFont.setFixedCellWidth(99);
listFont.setFixedCellHeight(20);
listFont.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
tfFont.setText(fontName[listFont.getSelectedIndex()]);
tfFont.requestFocus();
tfFont.selectAll();
updateSample();
}
});
listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (currentFont.getStyle() == Font.PLAIN)
listStyle.setSelectedIndex(0);
else if (currentFont.getStyle() == Font.BOLD)
listStyle.setSelectedIndex(1);
else if (currentFont.getStyle() == Font.ITALIC)
listStyle.setSelectedIndex(2);
else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC))
listStyle.setSelectedIndex(3);
listStyle.setVisibleRowCount(7);
listStyle.setFixedCellWidth(85);
listStyle.setFixedCellHeight(20);
listStyle.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]);
tfStyle.requestFocus();
tfStyle.selectAll();
updateSample();
}
});
final String fontSize[] = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" };
listSize = new JList(fontSize);
int defaultFontSizeIndex = 0;
for (int i = 0; i < fontSize.length; i++) {
if (fontSize[i].equals(currentFont.getSize() + "")) {
defaultFontSizeIndex = i;
break;
}
}
listSize.setSelectedIndex(defaultFontSizeIndex);
listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSize.setVisibleRowCount(7);
listSize.setFixedCellWidth(50);
listSize.setFixedCellHeight(20);
listSize.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
tfSize.setText(fontSize[listSize.getSelectedIndex()]);
tfSize.requestFocus();
tfSize.selectAll();
updateSample();
}
});
fontOkButton = new JButton("确定");
fontOkButton.addActionListener(this);
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fontDialog.dispose();
}
});
sample = new JLabel(" 记事本 ");
sample.setHorizontalAlignment(SwingConstants.CENTER);
sample.setPreferredSize(new Dimension(150, 31));
JPanel samplePanel = new JPanel();
samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
samplePanel.add(sample);
pane1.add(lblFont);
pane1.add(lblStyle);
pane1.add(lblSize);
pane2.add(tfFont);
pane2.add(tfStyle);
pane2.add(tfSize);
pane3.add(new JScrollPane(listFont));
pane3.add(new JScrollPane(listStyle));
pane3.add(new JScrollPane(listSize));
pane4.add(samplePanel);
pane4.add(fontOkButton);
pane4.add(cancelButton);
con.add(pane1);
con.add(pane2);
con.add(pane3);
con.add(pane4);
updateSample();
fontDialog.pack();
fontDialog.setSize(400, 400);
fontDialog.setLocation(200, 200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
}// 构造函数结束
// 更新示例显示的字体和风格大小等
public void updateSample() {
Font sampleFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText()));
sample.setFont(sampleFont);
}// End method updateSample
// 设置文本编辑区的字体
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fontOkButton) {
Font tempFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText()));
Text.setFont(tempFont);
fontDialog.dispose();
}
}// End method actionPerformed
}/* End of class MyFont */
public void removeUpdate(DocumentEvent e) {
mEdit_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
mEdit_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
mEdit_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
// End of DocumentListener
// 实现了接口UndoableListener的类UndoHandler
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee) {
undo.addEdit(uee.getEdit());
}
}
public static void main(String s[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Text = new JTextArea();
Text.setDragEnabled(true); // 支持自动拖放
Text.setTransferHandler(new FileTransferHandler(Text));
new Notepad4();
}
}
class FileTransferHandler extends TransferHandler {
JTextArea Text;
public FileTransferHandler(JTextArea Text) {
this.Text = Text;
}
public boolean importData(JComponent c, Transferable t) {
try {
List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
addFilesToFilePathList(files);
return true;
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean canImport(JComponent c, DataFlavor[] flavors) {
for (int i = 0; i < flavors.length; i++) {
if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
return true;
}
}
return false;
}
private void addFilesToFilePathList(List files) {
for (Iterator iter = files.iterator(); iter.hasNext();) {
File file = (File) iter.next();
String str = null;
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
Text.setText("");
while ((str = bfr.readLine()) != null) {
Text.append(str + "\15\12");
}
} catch (Exception b) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -