📄 main.java
字号:
barMenuBar.add(appearanceMenu);
helpMenu=new JMenu("帮助(H)");
helpMenu.setMnemonic('H');
aboutMenuItem=new JMenuItem("关于");
aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
helpMenu.add(aboutMenuItem);
helpMenu.add(showtimeMenuItem);
barMenuBar.add(helpMenu);
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
assaveMenuItem.addActionListener(this);
printMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
redoMenuItem.addActionListener(this);
undoMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
findMenuItem.addActionListener(this);
selectallMenuItem.addActionListener(this);
linewrapMenuItem.addActionListener(this);
redoMenuItem2.addActionListener(this);
undoMenuItem2.addActionListener(this);
cutMenuItem2.addActionListener(this);
copyMenuItem2.addActionListener(this);
pasteMenuItem2.addActionListener(this);
findMenuItem2.addActionListener(this);
selectallMenuItem2.addActionListener(this);
pb.setString("90%");pb.setValue(90);
try {
Thread.currentThread().sleep(31);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
linewrapMenuItem2.addActionListener(this);
javaappearanceMenuItem.addActionListener(this);
unixappearanceMenuItem.addActionListener(this);
windowsappearanceMenuItem.addActionListener(this);
aboutMenuItem.addActionListener(this);
showtimeMenuItem.addActionListener(this);
container.add(bar1,BorderLayout.NORTH);
bar1.setVisible(true);
bar1.setVisible(true);
bar1.setVisible(true);
setJMenuBar(barMenuBar);
setResizable(false);
pb.setString("100%");pb.setValue(100);
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
window.dispose();
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Main one=new Main();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
textAction = true;
}
public void keyReleased(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
jl.setText("所在位置:"+"x="+e.getX()+",y="+e.getY());
}
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
textPopup.show(text, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
textPopup.show(text, e.getX(), e.getY());
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void undoableEditHappened(UndoableEditEvent parm1) {
undoManger.addEdit(parm1.getEdit());
System.out.println(editMenu);
if (undoManger.canUndo()) {
undoMenuItem.setEnabled(true);
undoButton.setEnabled(true);
} else {
undoMenuItem.setEnabled(false);
undoButton.setEnabled(false);
}
if (true) {
redoMenuItem.setEnabled(true);
redoButton.setEnabled(true);
} else {
redoMenuItem.setEnabled(false);
redoButton.setEnabled(false);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == fontMenuItem){
FontDialog fontdialog = new FontDialog(this,text);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int px = (screensize.width - fontdialog.getWidth())/2;
int py = (screensize.height - fontdialog.getHeight())/2;
fontdialog.setLocation(px,py);
fontdialog.show(true);
}
if (e.getSource() == newMenuItem || e.getSource() == newButton) {
if (true ==textAction) {
saveDialog();
}
Main one =new Main();
}
if (e.getSource() == openMenuItem || e.getSource() == openButton) {
openDialog();
}
if (e.getSource() == saveMenuItem || e.getSource() == saveButton) {
saveDialog();
}
if(e.getSource() == assaveMenuItem || e.getSource() == assaveButton) {
assaveDialog();
}
if (e.getSource() == printMenuItem || e.getSource() == printButton) {
try {
attributes = new HashPrintRequestAttributeSet();
PrinterJob job = PrinterJob.getPrinterJob();
if (job.printDialog())
job.print(attributes);
} catch (Exception ex) {
JOptionPane.showMessageDialog(Main.this, e);
}
}
if (e.getSource() == redoMenuItem || e.getSource() == redoButton
|| e.getSource() == redoMenuItem2) {
undoManger.redo();
}
if (e.getSource() == undoMenuItem || e.getSource() == undoButton
|| e.getSource() == undoMenuItem2) {
undoManger.undo();
number++;
System.out.println(number);
}
if (e.getSource() == cutMenuItem || e.getSource() == cutButton
|| e.getSource() == cutMenuItem2) {
text.cut();
}
if (e.getSource() == copyMenuItem || e.getSource() == copyButton
|| e.getSource() == copyMenuItem) {
text.copy();
}
if (e.getSource() == pasteMenuItem || e.getSource() == pasteButton
|| e.getSource() == pasteMenuItem2 ) {
text.paste();
}
if (e.getSource() == findMenuItem || e.getSource() == findButton || e.getSource() == findMenuItem2 ) {
FindDialog findDialog = new FindDialog(text, Main.this);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int px = (screensize.width - findDialog.getWidth())/2;
int py = (screensize.height - findDialog.getHeight())/2;
findDialog.setLocation(px,py);
findDialog.setVisible(true);
}
if (e.getSource() == showtimeMenuItem||e.getSource()==showtimeButton) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy'年'MM'月'dd'日'+HH:mm:ss");
Calendar today = Calendar.getInstance();
String timeDate = dateFormat.format(today.getTime());
text.append(timeDate);
}
if (e.getSource() == selectallMenuItem || e.getSource() == selectallButton || e.getSource() == selectallMenuItem2 )
{
text.requestFocus(true);
text.selectAll();
}
if(e.getSource() == linewrapButton ){
linewrapMenuItem.setSelected(true);
text.setLineWrap(true);
}
if (e.getSource() == linewrapMenuItem || e.getSource() == linewrapMenuItem2 ) {
if(linewrapMenuItem.isSelected() == true )text.setLineWrap(true);
else{
text.setLineWrap(false);
}
}
if (e.getSource() == showtoolbarMenuItem) {
if (showtoolbarMenuItem.isSelected())
bar1.setVisible(true);
else
bar1.setVisible(false);
}
if (e.getSource() == aboutMenuItem || e.getSource() == aboutButton) {
JDialog jd=new JDialog(this,true);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
int x=(screensize.width-300)/2;
int y=(screensize.height-300)/2;
jd.setSize(300,600);
jd.setLocation(x,y);
jd.setResizable(false);
jd.setTitle("帮助");
JTextArea text=new JTextArea();
text.setLineWrap(true);
text.setText(" 作者: 06级软件五班 麦健桦");
text.append(" ");
text.append(" 版本---NotePad 1.0 ");
text.append("功能键使用: ");
text.append("文件 --- F; ");
text.append("新建 --- ctrl+N; ");
text.append("打开 --- ctrl+O; ");
text.append("保存 --- ctrl+S; ");
text.append("另存为 --- ctrl+A; ");
text.append("打印 --- ctrl+P; ");
text.append("退出 --- ctrl+E; ");
text.append("编辑 --- E; ");
text.append("重做 --- ctrl+A; ");
text.append("撤销 --- ctrl+U; ");
text.append("剪切 --- ctrl+Z; ");
text.append("复制 --- ctrl+C; ");
text.append("粘贴 --- ctrl+P; ");
text.append("查找/替换 --- ctrl+F; ");
text.append("全选 --- ctrl+B; ");
text.append("视图 --- V; ");
text.append("自动换行 --- ctrl+V; ");
text.append("是否显示工具栏 --- ctrl+Y; ");
text.append("工具 --- T; ");
text.append("字体/背景颜色 --- ctrl+8; ");
text.append("风格 --- S; ");
text.append("JAVA --- ctrl+L; ");
text.append("WINDOWS --- ctrl+J; ");
text.append("UNIX --- ctrl+K; ");
text.append("帮助 --- H; ");
text.append("关于 --- ctrl+E; ");
text.append("插入时间 --- ctrl+D; ");
text.setEditable(false);
jd.getContentPane().add(text);
jd.setVisible(true);
}
if (e.getSource() == exitMenuItem || e.getSource() == exitButton) {
if (true ==textAction) {
saveDialog();
setVisible(false);
this.dispose();
} else {
setVisible(false);
this.dispose();
}
}
if (e.getSource() == javaappearanceButton||e.getSource() == javaappearanceMenuItem) {
setFace("javax.swing.plaf.metal.MetalLookAndFeel");
}
if (e.getSource() == unixappearanceButton||e.getSource() == unixappearanceMenuItem) {
setFace("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
if (e.getSource() == windowsappearanceButton||e.getSource() == windowsappearanceMenuItem) {
setFace("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void setFace(String plaf) {
try {
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void assaveDialog() {
chooser.setCurrentDirectory(new File("."));
// 文件过滤
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String fileName = f.getName().toLowerCase();
return fileName.endsWith(".txt") || f.isDirectory();
}
public String getDescription() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -