?? jishiben.java
字號:
shifoukeyong();
}
public static void main(String [] a){
JiShiBen jsb = new JiShiBen();
jsb.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
int alert = 0;
if (area.getText().trim().length() > 0) {
alert = JOptionPane.showConfirmDialog(null, "文件沒保存,確定要退出嗎?",
"警告", JOptionPane.YES_NO_OPTION);
if (alert == JOptionPane.NO_OPTION) {
sfd.setVisible(true);
String fname = sfd.getDirectory() + sfd.getFile();
try {
FileOutputStream fs = new FileOutputStream(fname);
fs.close();
} catch (IOException ioe) {
}
}
}
frm.setVisible(false);
System.exit(0);
}else if (e.getSource() == open) {
ofd.setVisible(true);
String fname = ofd.getDirectory() + ofd.getFile();
try {
FileInputStream fi = new FileInputStream(fname);
byte fc[] = new byte[fi.available()];
fi.read(fc);
area.setText(new String(fc));
fi.close();
} catch (IOException ioe) {}
shifoukeyong();
} else if (e.getSource() == save) {
sfd.setVisible(true);
String fname = sfd.getDirectory() + sfd.getFile();
try {
FileOutputStream fs = new FileOutputStream(fname);
fs.write(area.getText().getBytes());
fs.close();
} catch (IOException ioe) {
}
}else if(e.getSource() == huanhang){
if(huanhang.getText().charAt(0) == '√'){
area.setLineWrap(false);//設置自動換行
huanhang.setText("自動換行");
}else if(huanhang.getText().charAt(0) != '√'){
area.setLineWrap(true);
huanhang.setText("√自動換行");
}
}if(e.getSource()==ziti){
if(isfont==false){
s1.setVisible(true);
area.setFont(s1.font2);
isfont = true;
}else{
s1.setVisible(true);
}
}if(e.getSource()==tihuan){
TihuanDialog.setVisible(true);
}if(e.getSource()==searchok){
this.serch();
}
if(e.getSource()==searchcancel){
TihuanDialog.setVisible(false);
}
if(e.getSource()==changeok){
this.change();
}
if(e.getSource()==changeall){
this.changeall();
}if(e.getSource() == about){
JOptionPane.showMessageDialog(this, "軟件技術7班 張帥");
}if(e.getSource() == time){
area.setText(area.getText() +new Date().toString());
}if(e.getSource() == zhuangtai){
if(zhuangtai.getText().charAt(0) == '√'){
setBar.setVisible(false);
zhuangtai.setText("狀態欄");
}else if(zhuangtai.getText().charAt(0) != '√'){
setBar.setVisible(true);
zhuangtai.setText("√狀態欄");
}
}if(e.getSource() == zhuandao){
String locline = JOptionPane.showInputDialog("請輸入行數:");
if (locline == null)
locline = "0";// 消除String為空時轉換為int的異常
if (Integer.parseInt(locline) > area.getLineCount()) {
JOptionPane.showMessageDialog(null, "超出了文件范圍!", "定位到行",
JOptionPane.INFORMATION_MESSAGE);
} else {
int index = 0;
int count = 1;
while (count < Integer.parseInt(locline)) {
index = area.getText().indexOf('\n', index);
index++;
count++;
}
area.setSelectionStart(index);
if (Integer.parseInt(locline) == area.getLineCount()) {
area.setSelectionEnd(area.getText().length());
} else
area.setSelectionEnd(area.getText().indexOf('\n', index));
area.setSelectedTextColor(Color.blue);
}
}if(e.getSource() == all){
area.selectAll();
}if(e.getSource() == jianqie){
area.cut();
}if(e.getSource() == fuzhi){
area.copy();
}if(e.getSource() == zhantie){
area.paste();
}if(e.getSource() == shanchu){
area.replaceSelection("");
}
}
// 處理點擊窗體右上角關閉按鈕事件的方法,
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
int alert = 0;
if (area.getText().trim().length() > 0) {
alert = JOptionPane.showConfirmDialog(null, "文件沒保存,確定要退出嗎?",
"警告", JOptionPane.YES_NO_OPTION);
if (alert == JOptionPane.NO_OPTION) {
sfd.setVisible(true);
String fname = sfd.getDirectory() + sfd.getFile();
try {
FileOutputStream fs = new FileOutputStream(fname);
fs.write(area.getText().getBytes());
fs.close();
} catch (IOException ioe) {
}
}
}
frm.setVisible(true);
System.exit(0);
}
}
public void shifoukeyong(){
if(area.getText().equals("")){
chexiao.setEnabled(false);
jianqie.setEnabled(false);
fuzhi.setEnabled(false);
shanchu.setEnabled(false);
tihuan.setEnabled(false);
zhuandao.setEnabled(false);
}else{
chexiao.setEnabled(true);
jianqie.setEnabled(true);
fuzhi.setEnabled(true);
shanchu.setEnabled(true);
tihuan.setEnabled(true);
zhuandao.setEnabled(true);
}
}
public void serch(){
String needsearch=searchtext.getText();
String main=area.getText();
String temp;
if(needsearch.equals("")){
JOptionPane.showMessageDialog(this,"關鍵字為空!","警告",JOptionPane.WARNING_MESSAGE);
}
else if(needsearch.length()>main.length()){
JOptionPane.showMessageDialog(this,"沒有找到關鍵字","提示",JOptionPane.INFORMATION_MESSAGE);
}
else{
int position=area.getSelectionEnd()-needsearch.length()+1;
if(position>=main.length()-needsearch.length() ||position<0){
position=0;
}
for(;position<=main.length()-needsearch.length();position++){
temp=main.substring(position,position+needsearch.length());
if(temp.equals(needsearch)){
area.setSelectionStart(position);
area.setSelectionEnd(position+needsearch.length());
break;
}
else if(position>=main.length()-needsearch.length()){
area.setSelectionStart(0);
area.setSelectionEnd(0);
JOptionPane.showMessageDialog(this,"沒有找到關鍵字","提示",JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
}
public void change(){
String changeword=changetext.getText();
if(area.getSelectionStart()!=area.getSelectionEnd()){
area.replaceRange(changeword,area.getSelectionStart(),area.getSelectionEnd());
}
else if(area.getSelectionStart()==area.getSelectionEnd()){
JOptionPane.showMessageDialog(this,"先搜索再替換","警告",JOptionPane.WARNING_MESSAGE);
}
}
public void keyPressed(KeyEvent e) {
shifoukeyong();
}
public void keyReleased(KeyEvent e) {
shifoukeyong();
gbwz();
}
public void keyTyped(KeyEvent e) {
// TODO 自動生成方法存根
shifoukeyong();
}
public void changeall(){
String needsearch=searchtext.getText();
String changeword=changetext.getText();
String main=area.getText();
String temp;
for(int i=0;i<=main.length()-needsearch.length();i++){
temp=main.substring(i,i+needsearch.length());
if(temp.equals(needsearch)){
area.setSelectionStart(i);
area.setSelectionEnd(i+needsearch.length());
area.replaceRange(changeword,area.getSelectionStart(),area.getSelectionEnd());
main=area.getText();
}
}
}
public void gbwz(){
if(!area.getText().equals("")&&area.getCaretPosition()!=0){
int i_xy = area.getCaretPosition();
String s_all = area.getText().substring(0,i_xy);
String[] array = s_all.split("\n");
int i_ln = 1,i_col = 1;
for(int i=0;i<array.length-1;i++){
if(i_xy>=array[i].length()){
i_ln++;
i_xy = i_xy - array[i].length()-1;
}
}
i_col = i_xy;
setBar.setText("ln: "+i_ln+" , Col: "+i_col);
}
}
public void mouseClicked(MouseEvent arg0){}
public void mouseExited(MouseEvent arg0){}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -