JavaÓ¦Óü¼ÇÉ——Îı¾³·Ïú¡¢»Ö¸´¹¦ÄܵÄʵÏÖ
×÷Îı¾±à¼Æ÷µÄʱºò£¬³·Ïú(undo)¡¢»Ö¸´(redo)ÊDZز»¿ÉÉÙµÄÁ½¸ö¹¦ÄÜ¡£ÊµÏÖÕâ¸ö¹¦ÄÜ£¬ÒªÓõ½javax.swing.undo°üÖеÄUndoManagerÀà¡£
UndoManagerÀàÓÐÈçϳ£Ó÷½·¨£º
boolean canUndo() ——ÅжÏÄÜ·ñ½øÐг·Ïû²Ù×÷£¬¿ÉÒÔÔò·µ»Øtrue¡£
boolean canRedo() ——ÅжÏÄÜ·ñ½øÐлָ´²Ù×÷£¬¿ÉÒÔÔò·µ»Øtrue¡£
void Undo() ——½øÐг·Ïû²Ù×÷¡£
void Redo() ——½øÐлָ´²Ù×÷¡£
µ«ÊÇ£¬½øÐг·Ïú¡¢»Ö¸´²Ù×÷Ç°»¹Ó¦Îª³·Ïú¡¢»Ö¸´µÄ¶ÔÏó×¢²áÕìÌýÆ÷¡£ÔÚÎı¾±à¼Æ÷ÖУ¬¶ÔÏó¾ÍÊÇÎı¾¡£ÀýÈçÏÈÉùÃ÷Á½¸ö¶ÔÏó£º
JTextArea text = new JTextArea();
UndoManager undomg = new UndoManager();
½ÓÏÂÀ´£¬ÎªtextÌí¼ÓÕìÌýÆ÷£º
text.getDocument().addUndoableEditListener(undomg);
ÕâÀïҪ˵Ãûһϣ¬Ö®ËùÒÔҪдgetDocument()ÊÇÒòΪע²áÕìÌýÆ÷µÄ¶ÔÏóÊÇÎı¾£¬²»ÊÇÎı¾¿ò¡£addUndoableEditListener(undomg)µÄ×÷ÓÃÊÇΪtextµÄÎı¾×¢²áÕìÌýÆ÷£¬¸Ã·½·¨µÄÔÐÍÊÇ£º
void addUndoableEditListener(UndoableEditListener listener)
¿ÉÒÔ¿´³ö£¬¸Ã·½·¨µÄ²ÎÊýÓ¦ÊÇÒ»¸öUndoableEditListener½Ó¿ÚÀàÐ͵IJÎÊý£¬¶øÎÒÃÇÓõIJÎÊýÊÇUndoManagerÀàÐÍ£¬ÕâÊÇÒòΪUndoManagerÀàÒѾʵÏÖÁËUndoableEditListener½Ó¿ÚµÄÔµ¹Ê¡£
ÏÖÔھͿÉÒÔ½øÐг·Ïú¡¢»Ö¸´²Ù×÷ÁË¡£·½·¨ÊÇ£º
undomg.Undo(); //³·Ïú
undomg.Redo(); //»Ö¸´
µ«ÊÇÕâÑù²»ËãÍêÃÀ£¬ÒòΪÈç¹ûÎÞ·¨³·Ïú»ò»Ö¸´£¬ÕâÁ½¸öÓï¾ä¾Í²»»áÆðÈκÎ×÷Óá£Òò´ËÎÒÃǸÄÓÃÈçÏ·½·¨£º
if(undomg.canUndo()) { //³·Ïú
undomg.Undo();
}
if(undomg.canRedo()) { //»Ö¸´
undomg.Redo();
}
ÕâÑùÒ»À´£¬Îı¾±à¼Æ÷µÄ³·Ïú¡¢»Ö¸´¹¦ÄܺÜÈÝÒ׵ؾÍʵÏÖÁË¡£ÒÔÏÂÊÇÒ»¸öÀý×Ó£º
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.*;
class UndoDemo extends JFrame {
static JTextArea text = new JTextArea();
static JPanel pnl = new JPanel();
static JButton unbtn = new JButton("³·Ïú");
static JButton rebtn = new JButton("»Ö¸´");
static UndoManager undomg = new UndoManager();
UndoDemo() {
super("³·Ïú¡¢»Ö¸´¹¦ÄÜʵÀý");
setVisible(true);
setSize(400,300);
s
Ïà¹ØÎĵµ£º
Ò»¡¢JAVA»ñÈ¡Ëæ»úÊýµÄ·½·¨
//ʹÓÃjava.lang.MathÀàµÄrandom()·½·¨
double a = Math.random();
System.out.println("0.0-1.0µÄËæ»úÊý£º" + a);
double b = Math.random()*100;
System.out.println("0.0-100.0µÄËæ»úÊý£º" + b);
int c = (int)(Math.random()*100);
System.out.println("0-100µÄËæ»úÕûÊý£º" + c);  ......
declarations n. ÉùÃ÷
extends v. ¼Ì³Ð
¼Ì³ÐÖÐ×¢ÒâµÄÎÊÌ⣺
1 ÖØд(Ò²½Ð¸²¸Ç£¬Ò²½ÐÖع¹)£ºÖØд·½·¨±ØÐëºÍ±»ÖØд·½·¨¾ßÓÐÏàͬµÄ·½·¨Ãû³Æ¡¢²ÎÊýÁбíºÍ·µ»ØÖµÀàÐÍ£»
ÖØд·½·¨²»ÄÜʹÓñȱ»ÖØд·½·¨¸üÑϸñµÄ·ÃÎÊȨÏÞ¡£
ÖØдÊÇÀàÓëÀà ......
×÷Ϊ¶ÔÏóµÄ´´½¨Ä£Ê½,µ¥Àýģʽȷ±£Ä³¸öÀàÖ»ÓÐÒ»¸öʵÀý£¬¶øÇÒ×ÔÐÐʵÀý»¯²¢ÏòÕû¸öϵͳÌṩÕâ¸öʵÀý¡£Õâ¸öÀà³ÆΪµ¥ÀýÀà¡£Óɶ¨Òå¿ÉÒÔ×ܽá³öµ¥ÀýģʽµÄÒªµãÓÐÈý¸ö£ºÒ»Êǵ¥ÀýÀàÖ»ÄÜÓÐÒ»¸öʵÀý£»¶þÊÇËü±ØÐë×ÔÐд´½¨Õâ¸öʵÀý£»ÈýÊÇËü±ØÐë×ÔÐÐÏòÕû¸öϵͳÌṩÕâ¸öʵÀý¡£
ÔÚ¼ÆËã»úϵͳÖУ¬Ï̳߳ء¢»º´æ¡¢ÈÕÖ ......
java¾µäÊé¼®»ã×Ü£¨²¿·Ö£©
Ò»¡¢ÈëÃÅ
¡¶Java 2´ÓÈëÃŵ½¾«Í¨¡·£ ÍƼö
¡¶Thinking in Java¡·£ Ç¿ÁÒÍƼö*
O’reillyµÄJava±à³Ì»ù´¡ÏµÁÐ £ ²Î¿¼*
¶þ¡¢½ø½×
¡¶Java Cook Book¡·£ ·Ç³£ÍƼö* £¨°üº¬ÁËJava±à³ÌµÄTips£¬Êʺϵ± ......