µ±Ç°Î»ÖãºLinux½Ì³Ì - Linux - ʹÓá°¶èÐÔ¡±ÆÀ¹ÀÌá¸ß GUI µÄÐÔÄÜ

ʹÓá°¶èÐÔ¡±ÆÀ¹ÀÌá¸ß GUI µÄÐÔÄÜ

Mark Roulo


ÕªÒª
»ºÂýµÄ GUI ÊÇ Java ×î´óµÄÐÔÄÜÎÊÌâÖ®Ò»¡£Õâƪ Java ¼¼ÇÉ˵Ã÷ÈçºÎ´´½¨¹¹ÔìËٶȽϿìµÄ GUI£¬ÕâÊÇͨ¹ýÖ»¹¹½¨±ØÒªµÄ²¿¼þʵÏֵġ£
»ºÂýµÄͼÐÎÓû§½çÃæ (GUI) ÊÇÈËÃÇ¶Ô Java ³£·¢µÄÀÎɧ¡£¾¡¹Ü¹¹Ôì¿ìËÙµÄ GUI ¿ÉÄܼȷÑʱÓÖ·ÑÁ¦£¬µ«Èç¹ûʹÓÃÎÒÌṩµÄÕâ¸öÀ࣬ÄúÖ»Ðë×öÉÙÁ¿µÄ¹¤×÷¾Í¿ÉÒÔ¼Ó¿ì GUI µÄËٶȡ£¶ÔÓÚ³¹µ×µÄ GUI ¸Ä½ø¶øÑÔ£¬ÕâÖ»ÊǵÚÒ»²½£»µ«ÊÇ£¬Õâ¸öÀàÊǼ«ºÃµÄÒ»ÖÖ½â¾ö·½°¸£¬ÒòΪËüºÜ¼òµ¥¡£ÓÐʱÕâ¸öÀà¶ÔÐÔÄܵÄÌá¸ß×ãÒÔÂú×ãÄúµÄÐèÒª£¬ÄúÎÞÐë×öÈκζîÍâµÄ¹¤×÷¡£
·½·¨
ÕâÖÖ·½·¨ºÜÈÝÒ×±íÊö£ºÖ±µ½±ØҪʱ²Å¹¹½¨ GUI ×é¼þ¡£´Óºê¹ÛÉϽ²£¬ÕâÊÇËùÓÐ Java ³ÌÐòµÄĬÈϹ¤×÷·½Ê½¡£±Ï¾¹£¬Ë­»áÔÚ³õʼ»¯Ê±¾Í¹¹Ôì³ÌÐòµÄÈ«²¿¿ÉÄܵĿò¼ÜºÍ´°¿ÚÄØ£¿µ«ÊÇ£¬ÎÒÔÚ±¾ÎÄÌṩµÄÕâ¸öÀàÔÊÐíÄú½øÐиü¾«Ï¸¡¢¸ü¶èÐԵĹ¹Ôì¡£

ÎÒ¹¹½¨µÄÓÃÀ´Ö§³Ö GUI ×é¼þ¶èÐÔ¹¹ÔìµÄÕâ¸öʵÓù¤¾ßÀà»ùÓÚÈýµã¾­Ñé¡£µÚÒ»µã£¬´ó¶àÊý GUI Ãæ°å¶¼ºÜ¼òµ¥£¬²¢ÇÒÎÞÐè×öÈκζîÍâµÄ¹¤×÷¾Í¿ÉÒԺܿìÍê³É³õʼ»¯¡£

½«´óÁ¿ GUI ×é¼þ¶Ñ»ýµ½ÆÁÄ»ÉϵÄÃæ°åÊÇ´ó¶àÊý»ºÂý³õʼ»¯µÄÔ­Òò¡£ÕâЩÃæ°åÖ÷ÒªÊôÓÚÁ½ÖÖÀàÐÍ£¬¼´´øÑ¡ÏµÄÃæ°å»ò¹ö¶¯Ãæ°å¡£

µÚ¶þµã£¬Í¨³£Ö±µ½Òª²é¿´ GUI ×é¼þʱ²ÅÐèÒª¹¹ÔìËü¡£µÚÈýµã£¬Ö»ÓÐÔÚÏÂÁз½·¨Ö®Ò»±»µ÷ÓÃÖ®ºó GUI ×é¼þ²ÅÊǿɼûµÄ£º


public void paint (Graphics)
public void paintComponents(Graphics)
public void paintAll (Graphics)
public void repaint ()
public void repaint (long)
public void repaint (int, int, int, int)
public void repaint (long, int, int, int, int)
public void update (Graphics)
´´½¨ÊµÓù¤¾ßÃæ°åÀàµÄ˼ÏëÊÇ£¬½«´ó¶àÊý GUI ¹¹½¨´úÂëÒƳö³õʼ»¯¿éºÍ¹¹Ô캯Êý¡£ÎÒÃǽ«ÕâЩ´úÂë·ÅÈëÒ»¸öÃûΪ lazyConstructor() µÄº¯ÊýÖС£Õâ¸öº¯ÊýÍê³Éƽ³£ÓÉ GUI ¹¹Ô캯ÊýÍê³ÉµÄ´ó¶àÊý¹¤×÷£¬µ«Ëü±¾Éí²¢²»Êǹ¹Ô캯Êý¡£Õâ¸öʵÓù¤¾ßÃæ°åÀàÈ·±£Ã¿µ±µ÷ÓÃÈκλæÖÆ»ò¸üз½·¨Ö®Ç°¶¼µ÷ÓÃÒ»´Î lazyConstructor ·½·¨¡£

ÔÚÑ¡ÏÃæ°åÄÚʹÓÃÕâ¸öÃæ°åÀà¿ÉÒÔʹ´øÑ¡ÏµÄÃæ°å¸ü¿ìµØÍê³É¹¹Ô죬ÒòΪ×î³õÖ»Ðè¹¹Ôì¿É¼ûµÄÄǸöÑ¡ÏÃæ°å¡£

´úÂë
¸ÃÀàµÄ´úÂëÈçÏÂËùʾ£º

import java.awt.*;

/**
* LazyPanel ÊÇÒ»¸ö³éÏó»ùÀ࣬ËüÌṩ½« Panel ¶ÔÏóµÄÖÃÈë²Ù×÷ÑÓ³Ùµ½
* ʵ¼Ê²é¿´¸ÃÃæ°åʱÔÙ½øÐеŦÄÜ¡£µ±Ê¹Óà CardLayout ºÍÑ¡ÏÃæ°å
* ÊÓͼʱÕâ¸öÀ༫ΪÓÐÓã¬ÒòΪËüÔÊÐí¸ù¾ÝÄúµÄ²Ù×÷¹¹Ôì×ÓÊÓͼ£¬¶ø²»±Ø
* Ò»¿ªÊ¼¾ÍµÈ´ýÈ«²¿¹¹ÔìÍê³É¡£
*
* Èç¹û×ÓÀàÑ¡Ôñ¸²¸ÇÏÂÃæµÄÈÎÒ»¸ö·½·¨£¬ÔòËüÃDZØÐë±£Ö¤±»¸²¸ÇµÄ·½·¨
* Ê×Ïȵ÷Óø¸ÀàµÄ·½·¨¡£ÕâЩ·½·¨ÊÇ£º
*
* public void paint (Graphics)
* public void paintComponents(Graphics)
* public void paintAll (Graphics)
* public void repaint ()
* public void repaint (long)
* public void repaint (int, int, int, int)
* public void repaint (long, int, int, int, int)
* public void update (Graphics)
*
* ÆäÖеÄÿ¸ö·½·¨¶¼È·±£Ê×Ïȹ¹ÔìÃæ°å£¬È»ºó±ã½«µ÷ÓÃת·¢¸ø¸¸Àà¡£
*
* Èç¹ûÄúҪʹÓÃÕâ¸öÀ࣬ÇëÊ×ÏÈ´´½¨ËüµÄÒ»¸ö×ÓÀ࣬Ȼºó¾¡¿ÉÄܵØ
* ½«×ÓÀ๹Ô캯ÊýÖеĴúÂëÒƵ½ lazyConstructor ·½·¨ÖС£ÏÂÃæÊÇ
* ʹÓà LazyPanel µÄÒ»¸öʾÀý£º
*
*
*
* import java.awt.*;
*
* class BusyPanel extends LazyPanel
* {
* public BusyPanel (int rows, int cols)
* {
* this.rows = rows;
* this.cols = cols;
* }
*
* protected void lazyConstructor()
* {
* setLayout (new GridLayout (rows, cols));
* for (int i = 0; i < rows * cols; ++i)
* {
* add (new Button (Integer.toString (i + startValue)));
* ++startValue;
* }
* }

*
* static private int startValue = 0;
*
* private int rows;
* private int cols;
* }
*

* Äú¿ÉÒÔÕâÑùʹÓÃËü£º
*
* import java.awt.*;
* import java.awt.event.*;
*
* class TestFrame extends Frame
* {
* public TestFrame ()
* {

* setLayout (new BorderLayout ());
* add (nextButton, "South");
*
* framePanel.setLayout (layout);
* for (int i = 0; i < 30; ++i)
* framePanel.add (new BusyPanel (8, 8), "");
* add (framePanel, "Center");
*
* nextButton.addActionListener (
* new ActionListener()
* {
* public void actionPerformed (ActionEvent event)
* {
* layout.next (framePanel);
* }
* }
* );
*
* setSize (400, 300);
* }
*
* private CardLayout layout = new CardLayout();
* private Button nextButton = new Button ("Next Panel");
* private Panel framePanel = new Panel();
*
* static public void main (String args[])
* {
* (new TestFrame()).show();
* }
* }
*

* Òª²é¿´Ê¹Óà LazyPanel µÄÓÅÔ½ÐÔ£¬ÇëÊÔ׎« lazyConstructor() ·½·¨ÖÐ
* µÄ´úÂëÒƵ½ BusyPanel µÄ¹¹Ô캯ÊýÖУ¬ÖØбàÒë¸ÃÀ࣬Ȼºó»ØÍ·ÔÙ¿´±¾Àý¡£
* Æô¶¯Ê±µÄ¶îÍâÑÓ³Ù¾ÍÊÇ LazyPanel ÀàÒªÏû³ýµÄÎÊÌâ¡£
*
* ÕâÖÖ´¦Àí¶Ô swing ͬÑùÓÐЧ¡£Ö»Ðë¶Ô LazyPanel ÉÔ×÷Ð޸ģ¬ÈÃËü
* À©Õ¹ JPanel£¨¶ø²»ÊÇ Panel£©¼´¿É¡£
*/
public abstract class LazyPanel extends Panel
{
// ÎÒֻϣÍûµ÷Óà lazyConstructor Ò»´Î¡£
private boolean lazyConstructorCalled = false;

// Swing µÄijЩ°æ±¾ÔÚ½«×é¼þÌí¼Óµ½ËüÃǵÄÈÝÆ÷֮ǰ
// µ÷Óà paint() ·½·¨¡£ÎÒÃDz»Ï£ÍûÔÚÏÔʾ×é¼þ֮ǰ
// µ÷ÓÃ lazyConstructor¡£
private boolean isConstructorFinished = false;

/**
* ´´½¨Ò»¸ö LazyPanel¡£
*/
protected LazyPanel ()
{
isConstructorFinished = true;
}

public void paint (Graphics g)
{
callLazyConstructor();

super.paint (g);
}

public void paintAll(Graphics g)
{
callLazyConstructor();

super.paintAll (g);
}

public void paintComponents (Graphics g)
{
callLazyConstructor();

super.paintComponents (g);
}

public void repaint ()
{
callLazyConstructor();

super.repaint();
}

public void repaint (long l)
{
callLazyConstructor();

super.repaint (l);
}

public void repaint (int i1, int i2, int i3, int i4)
{
callLazyConstructor();

super.repaint (i1, i2, i3, i4);
}

public void repaint (long l, int i1, int i2, int i3, int i4)
{
callLazyConstructor();

super.repaint (l, i1, i2, i3, i4);
}

public void update (Graphics g)
{
callLazyConstructor();

super.update (g);
}

/**
* Ç¿ÖƵ÷ÓÃÔÚ×ÓÀàÖÐʵÏÖµÄ lazyConstructor() ·½·¨¡£
* Èç¹û¶à´Îµ÷ÓÃij¸ö¸ø¶¨¶ÔÏóµÄÕâ¸ö·½·¨£¬Ôò³ýµÚÒ»´Î
* µ÷ÓÃÒÔÍâµÄÈκÎÆäËûµ÷Óò»»áÖ´ÐÐÈκβÙ×÷¡£
*/
public synchronized final void callLazyConstructor()
{
// ÒÔÏ´úÂëµÄ´óÖÂ˼ÏëÈçÏÂËùʾ£º
// 1) ¼ì²é¸Ã·½·¨ÊÇ·ñ±»³É¹¦µ÷Óùý¡£
// Èç¹ûÊÇ£¬Ö»·µ»Ø¶ø²»Ö´ÐÐÈκβÙ×÷¡£
//
// 2) ·ñÔò ... µ÷ÓöèÐÔ¹¹Ô캯Êý¡£
// 3) µ÷Óà validate ÒÔʹËùÌí¼ÓµÄÈ«²¿×é¼þ±äΪ¿É¼ûµÄ¡£
// 4) ×¢ÒâÎÒÃÇÒÑÔÚÔËÐеijÌÐò¡£

if ((lazyConstructorCalled == false) && (getParent() != null))
{
lazyConstructor();
lazyConstructorCalled = true;
validate();
}
}

/**
* ÈκÎ×ÓÀ඼±ØÐëʵÏÖÕâ¸ö·½·¨¡£±¾Ó¦¸Ã
* ³öÏÖÔÚ×ÓÀ๹Ô캯ÊýÖеĴó¶àÊý×é¼þ´´½¨
* ´úÂ뽫³öÏÖÔÚ´Ë´¦¡£Çë²ÎÔÄÉÏÃæµÄ
* ʾÀý¡£
*/
abstract protected void lazyConstructor ();
}


ʹÓà LazyPanel
LazyPanel ÀàµÄʹÓúܼòµ¥£¬µ«ÄúÓ¦¸Ã×¢Ò⼸µã¡£

±ðÔÚ lazyConstructor Öз¢³öÒì³£
¶ÔÓÚÃæ°å£¬ÎÒÃǺÜÈÝÒ×±àдÔÚ·¢Éú´íÎóʱ·¢³öÒì³£µÄ¹¹Ô캯Êý¡£²»ÐÒµÄÊÇ£¬lazyConstructorÖ»ÊÇÒ»¸öÆÕͨ·½·¨£¬¶ø²»Êǹ¹Ô캯Êý¡£ÒòΪ lazyConstructor ÊÇ´Ó paint() Öб»µ÷Óõģ¬ËùÒÔËü²»ÄÜ·¢³ö³ý RuntimeExceptions ºÍ Errors ÒÔÍâµÄÈκÎÒì³£¡£ÒòΪ³ýÁË AWT Ïß³ÌÖ®ÍâÔÙûÓÐÈκαðµÄµ÷ÓóÌÐò²¶»ñÕâЩÒì³£ºÍ´íÎó£¬ËùÒÔ×îºÃ²»Òª·¢³öÈκÎÒì³£¡£ÈçÓбØÒª£¬Äú¿ÉÒÔ½« lazyConstructor ·½·¨µÄÖ÷Ìå·ÅÔÚÒ»¸ö try/catch ¿éÖУ¬È»ºóÔÚÕâ¸ö catch ¿éÖнøÐÐһЩÃô¸ÐµÄ²Ù×÷¡£

GUI ±àÖÆÆ÷²»ºÏ×÷
Óà Inprise ¹«Ë¾µÄ JBuilder »ò Symantec ¹«Ë¾µÄ Cafe ÖÐµÄ GUI ±àÖÆÆ÷Éú³ÉµÄ´úÂë²»ÄܺܺõØÓë LazyPanel ÀàÅäºÏʹÓá£GUI ±àÖÆÆ÷Éú³ÉµÄ´úÂëʹÓóõʼ»¯º¯ÊýΪÃæ°å¹¹Ôì´ó¶àÊý GUI ×é¼þ¡£ÕâÑù¾ÍÌåÏÖ²»³ö LazyPanel µÄÓŵ㣬ÒòΪûÓн«ÈκβÙ×÷ÒƵ½ lazyConstructor() ·½·¨ÖС£

Ò»ÖÖ·½·¨ÊÇÊÖ¶¯½«³õʼ»¯º¯ÊýÖеÄ×é¼þ¹¹Ôì´úÂëÒƵ½ lazyConstructor ·½·¨ÖС£²»ÐÒµÄÊÇ£¬ÕâÑùÒ»À´ GUI ±àÖÆÆ÷½«²»ÄÜÔÙÐÞ¸ÄÃæ°åÒÔ×÷½øÒ»²½µÄÔöÇ¿¡£¸üºÃµÄÒ»ÖÖ·½·¨ÊDZ£Áô GUI ±àÖÆÆ÷Éú³ÉµÄÃæ°å²»¶¯£¬¶ø½«Ëü·ÅÈëÒ»¸ö LazyPanel ÖС£ÕâÑù LazyPanel ¾ÍÖ»°üº¬Ò»¸ö×é¼þ -- GUI ±àÖÆÆ÷¹¹½¨µÄ×é¼þ¡£

´úÂëµÄ×îÖÕÖ´ÐÐËٶȲ¢Ã»Óмӿì
´Ó×îÖÕ½á¹ûÀ´¿´£¬Õâ¸ö·½·¨Êµ¼ÊÉϲ¢Ã»ÓÐʹӦÓóÌÐò¼ÓËÙ¡£Èç¹ûÄúµÄ GUI ×î³õÐèÒªÕ¼Óà 1 MB Äڴ棬´´½¨È«²¿×é¼þÒª»¨ 30 ÃëµÄʱ¼ä£¬ÔòʹÓÃÕâ¸ö·½·¨Ö®ºóÈÔÈ»ÐèÒª 1 MB Äڴ棬ÍêÈ«¹¹½¨ËüÈÔȻҪ»¨ 30 ÃëµÄʱ¼ä¡£LazyPanel ½«¹¹½¨ËùÐè GUI ×é¼þµÄ¿ªÏú·Ö̯ÔÚ²»Í¬µÄʱ¼ä¶ÎÉÏ£¬¶ø²»ÊÇÔ¤ÏȾÍÇëÇóÈ«²¿µÄÄÚ´æºÍʱ¼ä¡£µ«ÊÇ£¬LazyPanel ²¢Ã»ÓвÉÈ¡ÈκδëÊ©À´¼õÉÙ¿ªÏú¡£Í¨³££¬ÓÃÕâ¸öÀàÀ´¼ÓËÙ GUI ¾Í×ã¹»ÁË¡£µ«ÊÇÔÚÁíһЩÇé¿öÏ£¬Ê¹ÓÃÒ»Á½¸ö LazyPanel Ö»ÊÇÖØ´óÐÔÄÜÓÅ»¯µÄ¿ªÊ¼¡£

С½á
ͨ¹ýÖ»½øÐбØÐèµÄ×îС³õʼ»¯£¬´øÓÐÐí¶à×é¼þµÄ Java GUI ¿É¸ü¿ìµØÍê³É³õʼ»¯¡£ LazyPanel ÀàʹÄú¼¸ºõ²»±Ø½øÐжîÍâµÄ±à³Ì¼´¿É¼Ó¿ì¸´ÔÓ GUI µÄ³õʼ»¯¡£¶ÔÓÚÌá¸ß¸´ÔÓ GUI µÄËٶȶøÑÔ£¬ÕâÊǸö²»´íµÄÆðµã£¬¶ÔÓÚ²»Ì«¸´Ô GUI£¬Í¨³£Ö»½øÐÐÕâÒ»²½¾Í×ã¹»ÁË¡£

×÷Õß¼ò½é
Mark Roulo ÊÇ JavaWorld É쵀 Java ¼¼Çɵļ¼ÊõЭµ÷Ô±¡£Ëû´Ó 1989 Ä꿪ʼ´ÓÊÂרҵ±à³Ì£¬²¢ÇÒÔÚ Java Éд¦ÓÚ alpha-3 °æʱ¾Í¿ªÊ¼Ê¹Óà Java¡£ËûÊÇ KLA-Tencor µÄÈ«Ö°Ô±¹¤£¬Ëû²ÎÓ뿪·¢ÁËÒ»ÖÖ 500KLOC¡¢·Ö²¼Ê½¡¢²¢ÐС¢¶à¼ÆËã»úµÄͼÏñ´¦Àí£¨»¹ÓÐÆäËû¹¦ÄÜ£©Ó¦ÓóÌÐò£¬Õû¸ö³ÌÐò¼¸ºõ¶¼ÊÇÓà Java ±àдµÄ¡£


²Î¿¼×ÊÔ´

ÓÐ¹Ø CardLayout µÄÎĵµ×ÊÁÏ£º
http://java.sun.com/products/jdk/1.2/docs/api/java/awt/CardLayout.html
¹ØÓÚ´øÑ¡ÏµÄÃæ°åµÄһƪ½Ì³Ì£º
http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html