1、1几个常用的 GUI 构件的用法一:常用的 GUI 构件JButton, JCheckBox, JRadioButton, JComboBox, JList, JTextField, JTextArea, JPanel JSlide; 二:基于 GUI 程序设计的一些基本思想 构件的基本功能。 每一构件的事件模型,包括发生何类事件,应该注册何种事件侦听器,程序是如何处理事件。关键语句:构件.AddTypeListener(实现相应事件侦听器接口的对象);同时定义内部类,该类实现相应事件侦听器接口,在类中,重新定义侦听器接口中的方法体内容(这就是事件处理的用户程序的关键内容) 。 了解各构件(类
2、)的主要变量属性和方法成员,必须从其类的层次结构开始了解。若有构件所提供的功能不能满足用户程序的要求,则可用以下方法:即继承的方法,例:myButton extends JButton. MyJframe 中的各构件的互动及相互通信机制。按构件的功能进行分类说明一:文本类构件Jlabel: 显示文本或信息(其内容可改变) 。JtextField: 可提供用户输入的文本框。JpasswordField : 可提供用户输入的特殊文本框(内容不显示) 。以上三个都是 Jcomponent 的子类。Jlabel: 一般接受 mouse 事件。JtextField:一般接受动作事件。注册:addActi
3、onListener() ,事件处理:public void actionPerformed(ActionEvent e),由部类中重写。JpasswordField : 一般接受动作事件。注册:addActionListener() ,同上。认识类的功能主要从以下几个方面着手:1:构造函数,2:从 Jcomponent 中继承而来的方法成员。Methods inherited from class javax.swing.JComponent3:Methods inherited from class java.awt.Containeradd, add, add, add, add, ad
4、dContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents。 。 。 。 。 。 。 。4:本身的方法。2例 1:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class LabelTest extends JFrame private JLabel label1, label2, label3;public LabelTest()super( “Testing JL
5、abel“ );Container c = getContentPane();c.setLayout( new FlowLayout() );/ JLabel constructor with a string argumentlabel1 = new JLabel( “Label with text“ );label1.setToolTipText( “This is label1“ ); /javax.swing.JComponent 功能。c.add( label1 );/ JLabel constructor with string, Icon and/ alignment argum
6、entsIcon bug = new ImageIcon( “bug1.gif“ );label2 = new JLabel( “Label with text and icon“,bug, SwingConstants.LEFT );label2.setToolTipText( “This is label2“ );c.add( label2 );/ JLabel constructor no argumentslabel3 = new JLabel();label3.setText( “Label with icon and text at bottom“ );label3.setIcon
7、( bug );label3.setHorizontalTextPosition(SwingConstants.CENTER );label3.setVerticalTextPosition(SwingConstants.BOTTOM );label3.setToolTipText( “This is label3“ );c.add( label3 );setSize( 275, 170 );show();3public class mylabel public static void main( String args ) LabelTest app = new LabelTest();例
8、2import java.awt.*;import java.awt.event.*;import javax.swing.*;class TextFieldTest extends JFrame private JTextField text1, text2, text3;private JPasswordField password;public TextFieldTest()super( “Testing JTextField and JPasswordField“ );Container c = getContentPane();c.setLayout( new FlowLayout(
9、) );/ construct textfield with default sizingtext1 = new JTextField( 10 );c.add( text1 );/ construct textfield with default texttext2 = new JTextField( “Enter text here“ );c.add( text2 );/ construct textfield with default text and/ 20 visible elements and no event handlertext3 = new JTextField( “Une
10、ditable text field“, 20 );text3.setEditable( false );c.add( text3 );/ construct textfield with default textpassword = new JPasswordField( “Hidden text“ );c.add( password );TextFieldHandler handler = new TextFieldHandler();text1.addActionListener( handler );4text2.addActionListener( handler );text3.a
11、ddActionListener( handler );password.addActionListener( handler );setSize( 325, 100 );this.setVisible(true);/ inner class for event handlingprivate class TextFieldHandler implements ActionListener public void actionPerformed( ActionEvent e )String s = “;if ( e.getSource() = text1 )s = “text1: “ + e.
12、getActionCommand();else if ( e.getSource() = text2 )s = “text2: “ + text2.getText();else if ( e.getSource() = text3 )s = “text3: “ + e.getActionCommand();else if ( e.getSource() = password ) /*JPasswordField pwd =(JPasswordField) e.getSource();*/s = “password: “ +new String( password.getPassword() )
13、;JOptionPane.showMessageDialog( null, s ); public class wcbtpublic static void main( String args ) TextFieldTest app = new TextFieldTest();5二:按钮类构件1:JButton 按钮构件:一般接受动作事件。注册:addActionListener() ,事件处理:public void actionPerformed(ActionEvent e),由部类中重写。2:JCheckBox 按钮与 JRadioButton 按钮:注册:addItemListener
14、 () ,事件处理:Here is a picture of an application that uses four check boxes to customize a cartoon: public void itemStatedChanged(ItemEvent e),由部类中重写。import java.awt.*;import java.awt.event.*;import javax.swing.*;class myframe extends JFrame private JTextField t;private Font plainFont, boldFont,Iavax.s
15、wing.JComponentIavax.swing.AbstractButtonJButton ToggleButtonJCheckBox JRadioButton6italicFont, boldItalicFont;private JRadioButton plain, bold, italic, boldItalic;private ButtonGroup radioGroup;private JButton jb1,jb2;myframe()super( “RadioButton Test“ );Container c = getContentPane();c.setLayout(
16、new FlowLayout() );t = new JTextField( “Watch the font style change“, 25 );c.add( t ); / Create radio buttonsplain = new JRadioButton( “Plain“, true );c.add( plain );bold = new JRadioButton( “Bold“, false);c.add( bold );italic = new JRadioButton( “Italic“, false );c.add( italic );boldItalic = new JR
17、adioButton( “Bold/Italic“, false );c.add( boldItalic );jb1=new JButton(“ok“);jb2=new JButton(“cancel“);c.add(jb1);c.add(jb2);/ register eventsRadioButtonHandler handler = new RadioButtonHandler();plain.addItemListener( handler );bold.addItemListener( handler );italic.addItemListener( handler );boldI
18、talic.addItemListener( handler );ButtonHandler h1 = new ButtonHandler();jb1.addActionListener(h1);jb2.addActionListener(h1);/ create logical relationship between JRadioButtonsradioGroup = new ButtonGroup();radioGroup.add( plain );radioGroup.add( bold );radioGroup.add( italic );radioGroup.add( boldIt
19、alic );7plainFont = new Font( “TimesRoman“, Font.PLAIN, 14 );boldFont = new Font( “TimesRoman“, Font.BOLD, 14 );italicFont = new Font( “TimesRoman“, Font.ITALIC, 14 );boldItalicFont =new Font( “TimesRoman“, Font.BOLD + Font.ITALIC, 14 );t.setFont( plainFont );setSize( 500, 300 );show();private class
20、 ButtonHandler implements ActionListener public void actionPerformed( ActionEvent e )if ( e.getSource() = jb1 ) System.exit(0);else if ( e.getSource() = jb2 ) /t.setFont( boldFont );System.out.println(“hello“);private class RadioButtonHandler implements ItemListener public void itemStateChanged( Ite
21、mEvent e )if ( e.getSource() = plain ) t.setFont( plainFont );else if ( e.getSource() = bold ) t.setFont( boldFont );else if ( e.getSource() = italic ) t.setFont( italicFont );else if ( e.getSource() = boldItalic ) t.setFont( boldItalicFont );/else if(e.getSource() = jb1 ) t.repaint();public class w
22、cb public static void main( String args )myframe app = new myframe();8三:组合框构件 1213下拉式构件(组合框):JComBox, 列表显示构件:Jlist.JcomBox::addItemListener() ,事件处理:Item Listenerpublic void itemStatedChanged(ItemEvent e),由部类中重写。import java.awt.*;import java.awt.event.*;import javax.swing.*;class ComboBoxTest extends
23、 JFrame private JComboBox images;private JLabel label;private String names = “bug1.gif“, “bug2.gif“,“travelbug.gif“, “buganim.gif“ ;private Icon icons = new ImageIcon( names 0 ),new ImageIcon( names 1 ),new ImageIcon( names 2 ),new ImageIcon( names 3 ) ;public ComboBoxTest()super( “Testing JComboBox
24、“ );Container c = getContentPane();c.setLayout( new FlowLayout() ); images = new JComboBox( names );images.setMaximumRowCount( 5);itemhand h1=new itemhand();images.addItemListener(h1);c.add( images );label = new JLabel( icons 0 );c.add( label );setSize( 350, 100 );9show();private class itemhand impl
25、ements ItemListener public void itemStateChanged( ItemEvent e )label.setIcon(icons images.getSelectedIndex() ) ;public class wcbpublic static void main( String args ) ComboBoxTest app = new ComboBoxTest();例 ch13Swing13_12.java四:mouse 事件与事件适配器类1:所有 Jcomponent 的子类都可以发生 mouse 事件(MouseEvent) 。2:mouse 事件
26、有二种侦听器接口:MouseListener 和 MouseMotionListener事件源、事件、监听器接口、方法关系事件 事件源 事件监听器接口 方法ActionEvent Button(Jbutton)ListMenuItemTextFielf(Password)ActionListener ActionPerformed()AdjustmentEvent JScrolbar AdjustmentListener AdjustmentValueChanged()ItemEvent JcheckBoxCheckBoxMenuItemListItemListener ItemStateCh
27、anged()KeyEvent Jcomponent KeyListener KeyPressed()KeyReleased()KeyTyped()MouseEvent Jcomponent MouseListener MousePressed()10MouseReleased()MouseEntered()MouseExited()MouseClicked()MouseMotionEvent Jcomponent MouseMotionListener MouseDragged()MouseMoved()教材 p149 表达式12-2MouseListener 接口包括下列方法:public
28、 void mousePressed(MouseEvent e);public void mouseClicked(MouseEvent e);public void mouseReleased(MouseEvent e);public void mouseEntered(MouseEvent e);public void mouseExited(MouseEvent e);MouseMotionListener 接口包括下列方法:public void mouseDragged(MouseEvent e);public void mouseMoved(MouseEvent e);import
29、 java.awt.*;import java.awt.event.*;import javax.swing.*;class MouseTracker extends JFrame private JLabel statusBar;public MouseTracker()super( “Demonstrating Mouse Events“ );statusBar = new JLabel();Container c = getContentPane();c.add( statusBar, “South“ );/ application listens to its own mouse eventsmousehand h=new mousehand();addMouseListener( h );addMouseMotionListener( h );setSize( 275, 100 );show();