¡¶Java»ù´¡ÈëÃÅ¡·¿ÎºóÏ°Ìâ¼°´ð°¸ ÏÂÔر¾ÎÄ

²©Ñ§¹È¡ª¡ªÈÃIT½Ìѧ¸ü¼òµ¥£¬ÈÃITѧϰ¸üÓÐЧ

? ͨ¹ýʵÏÖXxxListener½Ó¿Ú»òÕ߼̳ÐXxxAdapterÀàʵÏÖÒ»¸öʼþ¼àÌýÆ÷À࣬²¢¶Ô´¦Àí¼àÌý¶¯×÷µÄ

·½·¨½øÐÐÖØд

? ´´½¨Ê¼þÔ´¶ÔÏóºÍʼþ¼àÌýÆ÷¶ÔÏó

? µ÷ÓÃʼþÔ´µÄaddXxxLisntener()·½·¨£¬ÎªÊ¼þÔ´×¢²áʼþ¼àÌýÆ÷¶ÔÏó

2¡¢AWTºÍSwingµÄÇø±ð ²Î¿¼´ð°¸

AWT ÊÇAbstract Window ToolKit (³éÏó´°¿Ú¹¤¾ß°ü)µÄËõд£¬Õâ¸ö¹¤¾ß°üÌṩÁËÒ»Ì×Óë±¾µØͼÐνçÃæ½øÐн»»¥µÄ½Ó¿Ú¡£AWT ÖеÄͼÐκ¯ÊýÓë²Ù×÷ϵͳËùÌṩµÄͼÐκ¯ÊýÖ®¼äÓÐ×ÅÒ»Ò»¶ÔÓ¦µÄ¹Øϵ£¬µ±ÎÒÃÇÀûÓà AWT À´¹¹¼þͼÐÎÓû§½çÃæµÄʱºò£¬ÎÒÃÇʵ¼ÊÉÏÊÇÔÚÀûÓòÙ×÷ϵͳËùÌṩµÄͼÐο⡣ÓÉÓÚ²»Í¬²Ù×÷ϵͳµÄͼÐοâËùÌṩµÄ¹¦ÄÜÊDz»Ò»ÑùµÄ£¬ÔÚÒ»¸öƽ̨ÉÏ´æÔڵŦÄÜÔÚÁíÍâÒ»¸öƽ̨ÉÏÔò¿ÉÄܲ»´æÔÚ¡£ÎªÁËʵÏÖJavaÓïÑÔËùÐû³ÆµÄ\Ò»´Î±àÒ룬µ½´¦ÔËÐÐ\µÄ¸ÅÄAWT ²»µÃ²»Í¨¹ýÎþÉü¹¦ÄÜÀ´ÊµÏÖÆäƽ̨ÎÞ¹ØÐÔ£¬Ò²¾ÍÊÇ˵£¬AWT ËùÌṩµÄͼÐι¦ÄÜÊǸ÷ÖÖͨÓÃÐͲÙ×÷ϵͳËùÌṩµÄͼÐι¦ÄܵĽ»¼¯¡£ÓÉÓÚAWT ÊÇÒÀ¿¿±¾µØ·½·¨À´ÊµÏÖÆ书Äܵģ¬ÎÒÃÇͨ³£°ÑAWT¿Ø¼þ³ÆΪÖØÁ¿¼¶¿Ø¼þ¡£

Swing ÊÇÔÚAWTµÄ»ù´¡ÉϹ¹½¨µÄÒ»Ì×еÄͼÐνçÃæϵͳ£¬ËüÌṩÁËAWT ËùÄܹ»ÌṩµÄËùÓй¦ÄÜ£¬²¢ÇÒÓô¿´âµÄJava´úÂë¶ÔAWT µÄ¹¦ÄܽøÐÐÁË´ó·ù¶ÈµÄÀ©³ä¡£ÓÉÓÚÔÚSwing ÖÐûÓÐʹÓñ¾µØ·½·¨À´ÊµÏÖͼÐι¦ÄÜ£¬ÎÒÃÇͨ³£°ÑSwing¿Ø¼þ³ÆΪÇáÁ¿¼¶¿Ø¼þ¡£

AWTºÍSwingÖ®¼äµÄ»ù±¾Çø±ð£ºAWT ÊÇ»ùÓÚ±¾µØ·½·¨µÄC/C++³ÌÐò£¬ÆäÔËÐÐËٶȱȽϿ죻SwingÊÇ»ùÓÚAWT µÄJava³ÌÐò£¬ÆäÔËÐÐËٶȱȽÏÂý¡£¶ÔÓÚÒ»¸öǶÈëʽӦÓÃÀ´Ëµ£¬Ä¿±êƽ̨µÄÓ²¼þ×ÊÔ´ÍùÍù·Ç³£ÓÐÏÞ£¬¶øÓ¦ÓóÌÐòµÄÔËÐÐËÙ¶ÈÓÖÊÇÏîÄ¿ÖÐÖÁ¹ØÖØÒªµÄÒòËØ¡£ÔÚÕâÖÖì¶ÜµÄÇé¿öÏ£¬¼òµ¥¶ø¸ßЧµÄAWT µ±È»³ÉÁËǶÈëʽJavaµÄµÚһѡÔñ¡£¶øÔÚÆÕͨµÄ»ùÓÚPC»òÕßÊǹ¤×÷Õ¾µÄ±ê×¼JavaÓ¦ÓÃÖУ¬Ó²¼þ×ÊÔ´¶ÔÓ¦ÓóÌÐòËùÔì³ÉµÄÏÞÖÆÍùÍù²»ÊÇÏîÄ¿ÖеĹؼüÒòËØ£¬ËùÒÔÔÚ±ê×¼°æµÄJavaÖÐÔòÌᳫʹÓÃSwing£¬Ò²¾ÍÊÇͨ¹ýÎþÉüËÙ¶ÈÀ´ÊµÏÖÓ¦ÓóÌÐòµÄ¹¦ÄÜ¡£

²©Ñ§¹È¡ª¡ªÈÃIT½Ìѧ¸ü¼òµ¥£¬ÈÃITѧϰ¸üÓÐЧ Áù¡¢±à³ÌÌâ

1¡¢±àдһ¸öJFrame´°¿Ú£¬ÒªÇóÈçÏ£º

1£©ÔÚ´°¿ÚµÄ×îÉÏ·½·ÅÖÃÒ»¸öJLabel±êÇ©£¬±êÇ©ÖÐĬÈϵÄÎı¾ÊÇ¡°´Ë´¦ÏÔʾÊó±êÓÒ¼üµã»÷µÄ×ø±ê¡± 2£©ÎªJFrame´°¿ÚÌí¼ÓÒ»¸öÊó±êʼþ£¬µ±Êó±êÓÒ¼üµã»÷´°¿Úʱ£¬Êó±êµÄ×ø±êÔÚJLabel±êÇ©ÖÐÏÔʾ ²Î¿¼´ð°¸

import java.awt.*;

import java.awt.event.*;import javax.swing.*; public class MyMouseHandler extends JFrame { }

public MyMouseHandler() { }

public static void main(String[] args) { }

new MyMouseHandler();

final JLabel label = new JLabel(\´Ë´¦ÏÔʾÊó±êÓÒ¼üµã»÷µÄ×ø±ê\label.setOpaque(true);

label.setBackground(Color.PINK); this.add(label, BorderLayout.NORTH); this.setSize(300, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) { }

if (e.getButton() == e.BUTTON1) { }

int x = e.getX(); int y = e.getY();

String banner = \Êó±êµ±Ç°µã»÷λÖõÄ×ø±êÊÇ\label.setText(banner);

});

this.setVisible(true);

2¡¢±àдһ¸öJFrame´°¿Ú£¬ÒªÇóÈçÏ£º

1£©´°¿ÚÖеÄNORTHÇøÓòÖзÅÖÃÒ»¸öJPanelÃæ°å 2£©JPanelÃæ°åÖдÓ×óµ½ÓÒÒÀ´Î·ÅÖÃÈçÏÂ×é¼þ£º JLabel±êÇ©£¬±êÇ©µÄÎı¾Îª¡°ÐËȤ¡±

²©Ñ§¹È¡ª¡ªÈÃIT½Ìѧ¸ü¼òµ¥£¬ÈÃITѧϰ¸üÓÐЧ

Èý¸öJCheckBox¶àÑ¡°´Å¥£¬Îı¾·Ö±ðΪ¡°ÓðëÇò¡±¡¢¡°Æ¹ÅÒÇò¡±¡¢¡°³ª¸è¡± JLabel±êÇ©£¬±êÇ©µÄÎı¾Îª¡°ÐÔ±ð¡±

Á½¸öJRadioButton°´Å¥£¬Îı¾·Ö±ðΪ¡°ÄС±¡¢¡°Å®¡±

3£©´°¿ÚµÄCENTERÇøÓò·ÅÖÃÒ»¸öJScrollPaneÈÝÆ÷£¬ÈÝÆ÷ÖзÅÖÃÒ»¸öJTextAreaÎı¾Óò 4£©µ±µã»÷¶àÑ¡°´Å¥ºÍµ¥Ñ¡°´Å¥Ê±£¬»á°ÑÑ¡Öа´Å¥µÄÎı¾ÏÔʾÔÚJTextAreaÎı¾ÓòÖС£ ²Î¿¼´ð°¸

import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*;

public class Information extends JFrame {

// ´°¿ÚNORTH²¿µÄJPanelÃæ°å

private JPanel panel = new JPanel(); // °®ºÃ±êÇ©

private JLabel lb1 = new JLabel(\°®ºÃ\// Èý¸ö±íʾ°®ºÃµÄJCheckBox¸´Ñ¡¿ò

private JCheckBox cb1 = new JCheckBox(\ÓðëÇò\private JCheckBox cb2 = new JCheckBox(\ƹÅÒÇò\private JCheckBox cb3 = new JCheckBox(\³ª¸è\// ÐÔ±ð±êÇ©

private JLabel lb2 = new JLabel(\ÐÔ±ð\// ±íʾÐÔ±ðµÄJRadioButtonµ¥Ñ¡¿ò

private JRadioButton rb1 = new JRadioButton(\ÄÐ\private JRadioButton rb2 = new JRadioButton(\Å®\// ButtonGroupÌí¼ÓJRadioButton,ʵÏÖµ¥Ñ¡¹¦ÄÜ private ButtonGroup bg = new ButtonGroup(); // Îı¾Óò×é¼þ

private JTextArea area = new JTextArea();

// ´°¿ÚCENTER²¿µÄJScrollPaneÃæ°å£¬ÆäÖзÅÖÃareaÎı¾Óò private JScrollPane pane = new JScrollPane(area); // Set¼¯ºÏ´æ·ÅÑ¡ÖеÄÐËȤ

private Set hobbies = new HashSet(); // genderÑ¡ÖеÄÐÔ±ð

private String gender = \// JCheckBox¸´Ñ¡¿òµÄʼþ¼àÌýÆ÷

private ActionListener listener1 = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JCheckBox cb = (JCheckBox) e.getSource();

²©Ñ§¹È¡ª¡ªÈÃIT½Ìѧ¸ü¼òµ¥£¬ÈÃITѧϰ¸üÓÐЧ

};

// JRadioButtonµ¥Ñ¡¿òµÄʼþ¼àÌýÆ÷

private ActionListener listener2 = new ActionListener() { };

private void print() { }

public Information() {

//Ìí¼Ó±êÇ©¡¢µ¥Ñ¡ºÍ¸´Ñ¡°´Å¥ panel.add(lb1); panel.add(cb1); panel.add(cb2); panel.add(cb3); panel.add(lb2); panel.add(rb1); panel.add(rb2); bg.add(rb1); bg.add(rb2); // Çå¿ÕÎı¾Óò area.setText(\

// Èç¹ûSet¼¯ºÏÖÐÓÐÔªËØ£¬´òÓ¡ÐËȤ if (hobbies.size() > 0)

area.append(\ÄãµÄÐËȤ°®ºÃÓÐ: \

Iterator it = hobbies.iterator(); while (it.hasNext()) { }

// Èç¹ûgender²»Îª¿Õ×Ö·û´®£¬´òÓ¡ÐÔ±ð if (!\

area.append(\ÄãµÄÐÔ±ðΪ£º \area.append(it.next() + \

public void actionPerformed(ActionEvent e) { }

JRadioButton jb = (JRadioButton) e.getSource(); gender = jb.getText(); print(); }

// Ñ¡Öеĸ´Ñ¡¿ò°ÑÎı¾Ìí¼Óµ½Set¼¯ºÏÖÐ if (cb.isSelected()) {

hobbies.add(cb.getText());

// ·´Ö®´Ó¼¯ºÏÖÐÒƳý } else { }

print();

hobbies.remove(cb.getText());

// ´òÓ¡·½·¨