📄 mynotepadui.java
字号:
});
//MenuItem-Cut
cut.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
cut();
}
});
//MenuItem-Copy
copy.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
copy();
}
});
//MenuItem-Paste
paste.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
paste();
}
});
//MenuItem-Auto change line
changeLine.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
changeLine();
}
});
//MenuItem-Cancle change line
cancleChangeLine.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
cancleChangeLine();
}
});
//MenuItem-Always on top
top.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
alwaysOnTop();
}
});
//MenuItem-Canle Always on top
cancleOnTop.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
cancleAlwaysOnTop();
}
});
//MenuItem-About MyNotePad
about.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//Display the dialogue
JOptionPane.showOptionDialog(null,
"The program's name:\n MyNotePad\n" +
"Author:\n Bryant.Hang\n" +
"Introduction:\n A tool including a word editing tool and a chating tool.\n" +
"My blog:\n http://blog.csdn.net/superhj1987\n"+"If you have questions,please send email to superhj1987@126.com!",
"About MyNotePad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,null,null);
}
});
chatServer.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
new ChatServer();
}
});
chatServerButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
new ChatServer();
}
});
chatClient.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
new ChatClient();
}
});
chatClientButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
new ChatClient();
}
});
redItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setForeground(Color.red);
}
});
greenItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setForeground(Color.green);
}
});
yellowItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setForeground(Color.yellow);
}
});
blackItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setForeground(Color.black);
}
});
blueItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setForeground(Color.blue);
}
});
redBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.red);
}
});
greenBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.green);
}
});
yellowBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.yellow);
}
});
blackBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.black);
}
});
blueBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.blue);
}
});
whiteBG.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setBackground(Color.white);
}
});
fontItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setFont(JFontChooser.showDialog(null,"Set the Font"));
}
});
resetItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.setFont(Font.getFont("宋体"));
textArea.setBackground(Color.white);
textArea.setForeground(Color.black);
}
});
helpItem.addActionListener(this);
//The event of textArea
textArea.addKeyListener(
new KeyAdapter(){
public void keyTyped(KeyEvent e){
processTextArea();
}
});
//The event of mouse
textArea.addMouseListener(
new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(editMenu,e.getX()-20,e.getY()+70);
}
public void mouseClicked(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if (obj == helpItem) { //菜单栏中的帮助
//调出帮助对话框
HelpMain helpDialog = new HelpMain(this);
helpDialog.setVisible(true);
}
}
private void openFile(){
if(isCurrentFileSaved()){//is the File saved?
open();//open the new file
}
else{
//display the dialog
int option = JOptionPane.showConfirmDialog(null,"The file has been edited,do you save it?",
"Save the file?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
switch(option){
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
open();
break;
}
}
}
private void open(){
int option = fileChooser.showDialog(null,"Open");
if(option == JFileChooser.APPROVE_OPTION){
try{
BufferedReader buf =
new BufferedReader(new FileReader(
fileChooser.getSelectedFile()));
//title
setTitle(fileChooser.getSelectedFile().toString());
//delete
textArea.setText("");
//stateBar
stateBar.setText("Not changed");
//changeline
String lineSeparator = System.getProperty("line.separator");
//read the file
String text;
while((text = buf.readLine()) != null){
textArea.append(text);
textArea.append(lineSeparator);
}
buf.close();
textArea.setCaretPosition(0);//到顶端
}
catch(IOException e){
JOptionPane.showMessageDialog(null,e.toString(),"Open the file - Fail!",
JOptionPane.ERROR_MESSAGE);
}
}
}
private boolean isCurrentFileSaved(){
if(stateBar.getText().equals("Not changed")){
return true;
}
else{
return false;
}
}
private void closeFile(){
if(isCurrentFileSaved()){
dispose();
}
else{
int option = JOptionPane.showConfirmDialog(null,"The file has been changed,do you save it?","Do you save the file?",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null
);
switch(option){
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void saveFile(){
File file = new File(getTitle());
if(!file.exists()){
saveFileAs();
}
else{
try{
//打开指定的文件
BufferedWriter buf =new BufferedWriter(
new FileWriter(file));
//把编辑区文字写入文件
buf.write(textArea.getText());
buf.close();
stateBar.setText("Not changed");
}
catch(IOException e){
JOptionPane.showMessageDialog(null,e.toString(),"Failed!",JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs(){
//display the file
fileChooser.setDialogTitle("SaveFileAs(请务必输入文件的扩展名)");
int option = fileChooser.showDialog(null,"Save");
if(option == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
setTitle(file.toString());
try{
file.createNewFile();
saveFile();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,e.toString(), "Can't creat new file",JOptionPane.ERROR_MESSAGE);
}
}
}
private void cut(){
textArea.cut();
stateBar.setText("Changed");
popUpMenu.setVisible(false);
}
private void copy(){
textArea.copy();
popUpMenu.setVisible(false);
}
private void paste(){
textArea.paste();
stateBar.setText("Changed");
popUpMenu.setVisible(false);
}
private void changeLine(){
textArea.setLineWrap(true);
popUpMenu.setVisible(false);
}
private void cancleChangeLine(){
textArea.setLineWrap(false);
popUpMenu.setVisible(false);
}
private void alwaysOnTop(){
setAlwaysOnTop(true);
popUpMenu.setVisible(false);
}
private void cancleAlwaysOnTop(){
setAlwaysOnTop(false);
popUpMenu.setVisible(false);
}
private void processTextArea(){
popUpMenu.setVisible(false);
stateBar.setText("Changed");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -