ImageVerifierCode 换一换
格式:PPT , 页数:62 ,大小:1.05MB ,
资源ID:8169988      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-8169988.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(图像界面组件.ppt)为本站会员(hskm5268)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

图像界面组件.ppt

1、第15讲. 图像界面组件,常用用户界面组件,JButton 按钮jbt JCheckBox 复选框jchk JRadioButton 单选框jrb JLabel 标签 jlbl JTextField 文本域 jtf JTextArea 文本区域 jta JComboBox 组合框 jcbo JList 列表 jlst,GUI Class Hierarchy (Swing) GUI类的继承关系,常用用户界面组件,Java事件对象的层次结构,用户行为、源对象和事件 对应关系,Source Event Type User Action Object GeneratedClick a button点击

2、按钮 JButton ActionEvent Click a check box点击复选框 JCheckBox ItemEvent, ActionEvent Click a radio button单击单选框 JRadioButton ItemEvent, ActionEvent Press return on a text field文本回车 JTextField ActionEvent Select a new item选一个新项 JComboBox ItemEvent, ActionEvent Select a list new item 选多项 JList JSelectionList

3、Event Window opened, closed, etc. Window WindowEvent Mouse pressed, released, etc. Component MouseEvent Key released, pressed, etc. Component KeyEvent,AbstractButton,1 JButton,JButton Constructors 构造方法,The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon i

4、con) JButton(Icon icon),JButton Properties 常用属性,text icon mnemonic horizontalAlignment verticalAlignment horizontalTextPosition verticalTextPosition,Default Icons, Pressed Icon, and Rollover Icon 默认图标、按下图标和翻转图标,A regular button has a default icon, pressed icon, and rollover icon. Normally, you use t

5、he default icon. All other icons are for special effects. A pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but not pressed.,(A) Default icon (B) Pressed icon (C) Rollover icon,Demo TestButtonIcons.java,import javax.swing.*; publi

6、c class TestButtonIcons extends JFrame public static void main(String args) / Create a frame and set its properties JFrame frame = new TestButtonIcons();frame.setTitle(“ButtonIcons“);frame.setSize(165, 80);frame.setLocationRelativeTo(null); / Center the frameframe.setDefaultCloseOperation(JFrame.EXI

7、T_ON_CLOSE);frame.setVisible(true); public TestButtonIcons() ImageIcon usIcon = new ImageIcon(“image/usIcon.gif“);ImageIcon caIcon = new ImageIcon(“image/caIcon.gif“); ImageIcon ukIcon = new ImageIcon(“image/ukIcon.gif“);JButton jbt = new JButton(“Click it“, usIcon);jbt.setPressedIcon(caIcon);jbt.se

8、tRolloverIcon(ukIcon);add(jbt); ,Horizontal Alignments 水平对齐,Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. At present, LEADING and LEFT are the sam

9、e and TRAILING and RIGHT are the same. Future implementation may distinguish them. The default horizontal alignment is SwingConstants.TRAILING.,Vertical Alignments 垂直对齐,Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using one

10、of the three constants: TOP, CENTER, BOTTOM. The default vertical alignment is SwingConstants.CENTER.,Horizontal Text Positions 文本水平,Horizontal text position specifies the horizontal position of the text relative to the icon. You can set the horizontal text position using one of the five constants:

11、LEADING, LEFT, CENTER, RIGHT, TRAILING. The default horizontal text position is SwingConstants.RIGHT.,Vertical Text Positions 文本垂直,Vertical text position specifies the vertical position of the text relative to the icon. You can set the vertical text position using one of the three constants: TOP, CE

12、NTER. The default vertical text position is SwingConstants.CENTER.,JButton触发事件,当按钮回车时,触发一个ActionEvent。,Example: Using Buttons,Write a program that displays a message on a panel and uses two buttons, , to move the message on the panel to the left or right.,触发ActionEvent,import java.awt.*; import java

13、.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.*;public class ButtonDemo extends JFrame / Create a panel for displaying messageprotected MessagePanel messagePanel= new MessagePanel(“Welcome to Java“);/ Declare two buttons to move the message left and rightprivate JB

14、utton jbtLeft = new JButton(“);public static void main(String args) ButtonDemo frame = new ButtonDemo();frame.setTitle(“ButtonDemo“);frame.setLocationRelativeTo(null); / Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500, 200);frame.setVisible(true);public ButtonD

15、emo() / Set the background color of messagePanelmessagePanel.setBackground(Color.white);/ Create Panel jpButtons to hold two Buttons “JPanel jpButtons = new JPanel();jpButtons.setLayout(new FlowLayout();jpButtons.add(jbtLeft);jpButtons.add(jbtRight);/ Set keyboard mnemonicsjbtLeft.setMnemonic(L);jbt

16、Right.setMnemonic(R);/ Set icons and remove text / jbtLeft.setIcon(new ImageIcon(“image/left.gif“); / jbtRight.setIcon(new ImageIcon(“image/right.gif“); / jbtLeft.setText(null); / jbtRight.setText(null);/ Set tool tip text on the buttonsjbtLeft.setToolTipText(“Move message to left“);jbtRight.setTool

17、TipText(“Move message to right“);/ Place panels in the framesetLayout(new BorderLayout();add(messagePanel, BorderLayout.CENTER);add(jpButtons, BorderLayout.SOUTH);/ Register listeners with the buttonsjbtLeft.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) messagePan

18、el.moveLeft(););jbtRight.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) messagePanel.moveRight();); ,Demo ButtonDemo .java,2 JCheckBox 复选框,JCheckBox inherits all the properties such as text, icon, mnemonic, verticalAlignment, horizontalAlignment, horizontalTextPosi

19、tion, verticalTextPosition, and selected from AbstractButton, and provides several constructors to create check boxes.,JCheckBox触发事件,当复选框被点击(选中或未选中)时,先触发一个ItemEvent,然后触发一个ActionEvent。要确定是否选中,用isSelected()方法,返回true表示被选中。 对于监听器的设置,可以选用ItemEvent 或者ActionEvent 两者之一。,Example: Using Check Boxes,Add three

20、check boxes named Centered, Bold, and Italic into Example 15.1 to let the user specify whether the message is centered, bold, or italic.,ButtonDemo,CheckBoxDemo,import java.awt.*; import java.awt.event.*; import javax.swing.*;public class CheckBoxDemo extends ButtonDemo / Create three check boxes to

21、 control the display of messageprivate JCheckBox jchkCentered = new JCheckBox(“Centered“);private JCheckBox jchkBold = new JCheckBox(“Bold“);private JCheckBox jchkItalic = new JCheckBox(“Italic“);public static void main(String args) CheckBoxDemo frame = new CheckBoxDemo();frame.setTitle(“CheckBoxDem

22、o“);frame.setLocationRelativeTo(null); / Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500, 200);frame.setVisible(true);public CheckBoxDemo() / Set mnemonic keysjchkCentered.setMnemonic(C);jchkBold.setMnemonic(B);jchkItalic.setMnemonic(I);/ Create a new panel to

23、hold check boxesJPanel jpCheckBoxes = new JPanel();jpCheckBoxes.setLayout(new GridLayout(3, 1);jpCheckBoxes.add(jchkCentered);jpCheckBoxes.add(jchkBold);jpCheckBoxes.add(jchkItalic);add(jpCheckBoxes, BorderLayout.EAST);/ Register listeners with the check boxesjchkCentered.addActionListener(new Actio

24、nListener() public void actionPerformed(ActionEvent e) messagePanel.setCentered(jchkCentered.isSelected(););jchkBold.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) setNewFont(););jchkItalic.addActionListener(new ActionListener() public void actionPerformed(ActionEv

25、ent e) setNewFont(););private void setNewFont() / Determine a font styleint fontStyle = Font.PLAIN;fontStyle += (jchkBold.isSelected() ? Font.BOLD : Font.PLAIN);fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN);/ Set font for the messageFont font = messagePanel.getFont();messagePanel

26、.setFont(new Font(font.getName(), fontStyle, font.getSize(); ,Demo CheckBoxDemo .java,3 JRadioButton 单选按钮,Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time.,Grouping Radio Buttons 单选按钮组群,ButtonGroup btg = new ButtonGroup(); btg.

27、add(jrb1); btg.add(jrb2);,JRadioButton触发事件,当单选框被点击(选中或未选中)时,先触发一个ItemEvent,然后触发一个ActionEvent。要确定是否选中,用isSelected()方法,返回true表示被选中。 对于监听器的设置,可以选用ItemEvent 或者ActionEvent 两者之一。,Example: Using Radio Buttons,Add three radio buttons named Red, Green, and Blue into the preceding example to let the user choo

28、se the color of the message.,ButtonDemo,CheckBoxDemo,RadioButtonDemo,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonDemo extends CheckBoxDemo / Declare radio buttonsprivate JRadioButton jrbRed, jrbGreen, jrbBlue;public static void main(String args) RadioBut

29、tonDemo frame = new RadioButtonDemo();frame.setLocationRelativeTo(null); / Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setTitle(“RadioButtonDemo“);frame.setSize(500, 200);frame.setVisible(true);public RadioButtonDemo() / Create a new panel to hold check boxesJPanel jpR

30、adioButtons = new JPanel(); jpRadioButtons.setLayout(new GridLayout(3, 1);jpRadioButtons.add(jrbRed = new JRadioButton(“Red“);jpRadioButtons.add(jrbGreen = new JRadioButton(“Green“);jpRadioButtons.add(jrbBlue = new JRadioButton(“Blue“);add(jpRadioButtons, BorderLayout.WEST);/ Create a radio button g

31、roup to group three buttonsButtonGroup group = new ButtonGroup();group.add(jrbRed);group.add(jrbGreen);group.add(jrbBlue);/ Set keyboard mnemonicsjrbRed.setMnemonic(E); jrbGreen.setMnemonic(G);jrbBlue.setMnemonic(U);/ Register listeners for check boxesjrbRed.addActionListener(new ActionListener() pu

32、blic void actionPerformed(ActionEvent e) messagePanel.setForeground(Color.red););jrbGreen.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) messagePanel.setForeground(Color.green););jrbBlue.addActionListener(new ActionListener() public void actionPerformed(ActionEvent

33、 e) messagePanel.setForeground(Color.blue););/ Set initial message color to bluejrbBlue.setSelected(true);messagePanel.setForeground(Color.blue); ,Demo RadioButtonDemo .java,4 JLabel 标签,A label is a display area for a short text, an image, or both.,JLabel Constructors 构造方法,The constructors for label

34、s are as follows: JLabel() JLabel(String text, int horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment) JLabel(String text, Icon icon, int horizontalAlignment),JLabel Properties 属性,JLabel inherits all the properties from JComponent and has many proper

35、ties similar to the ones in JButton, such as text, icon, horizontalAlignment, verticalAlignment, horizontalTextPosition, verticalTextPosition.,Using Labels,/ Create an image icon from image file ImageIcon icon = new ImageIcon(“image/grapes.gif“);/ Create a label with text, an icon, / with centered h

36、orizontal alignment JLabel jlbl = new JLabel(“Grapes“, icon, SwingConstants.CENTER);/ Set labels text alignment and gap between text and icon jlbl.setHorizontalTextPosition(SwingConstants.CENTER); jlbl.setVerticalTextPosition(SwingConstants.BOTTOM); jlbl.setIconTextGap(5);,5 JTextField 文本域,A text fi

37、eld is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description).,JTextField Constructors 构造方法,JTextField(int columns)Creates an empty text field with the specified number of columns. JTextFiel

38、d(String text)Creates a text field initialized with the specified text. JTextField(String text, int columns)Creates a text field initialized with the specified text and the column size.,JTextField Properties 常用属性,text horizontalAlignment editable columns,JTextField Methods 常用方法,getText()Returns the

39、string from the text field. setText(String text)Puts the given string in the text field. setEditable(boolean editable)Enables or disables the text field to be edited. By default, editable is true. setColumns(int)Sets the number of columns in this text field. The length of the text field is changeabl

40、e.,JTextField 触发事件,当光标移动到文本域,并且按回车键时,触发一个ActionEvent事件。,Example: Using Text Fields,Add a text field to the preceding example to let the user set a new message.,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextFieldDemo extends RadioButtonDemo private JTextField jtfM

41、essage = new JTextField(10);/* Main method */public static void main(String args) TextFieldDemo frame = new TextFieldDemo();frame.pack();frame.setTitle(“TextFieldDemo“);frame.setLocationRelativeTo(null); / Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);pu

42、blic TextFieldDemo() / Create a new panel to hold label and text fieldJPanel jpTextField = new JPanel();jpTextField.setLayout(new BorderLayout(5, 0); jpTextField.add(new JLabel(“Enter a new message“), BorderLayout.WEST);jpTextField.add(jtfMessage, BorderLayout.CENTER);add(jpTextField, BorderLayout.N

43、ORTH);jtfMessage.setHorizontalAlignment(JTextField.LEFT);/ Register listenerjtfMessage.addActionListener(new ActionListener() /* Handle ActionEvent */public void actionPerformed(ActionEvent e) messagePanel.setMessage(jtfMessage.getText();/ jtfMessage.requestFocusInWindow(); ); ,Demo TextFieldDemo .j

44、ava,6 JTextArea 文本区域,If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.,JTextArea Constructors,JTextArea(int rows, int columns)Creates a text

45、 area with the specified number of rows and columns. JTextArea(String s, int rows, int columns)Creates a text area with the initial text and the number of rows and columns specified.,JTextArea Properties,text editable columns lineWrap wrapStyleWord rows lineCount tabSize,JTextArea Methods 常用方法,getTe

46、xt()Returns the string from the text field. setText(String text)Puts the given string in the text field. setEditable(boolean editable)Enables or disables the text field to be edited. By default, editable is true. setColumns(int)Sets the number of columns in this text field. The length of the text fi

47、eld is changeable.,JTextArea 触发事件,当光标移动到文本区域,并且按回车键时,触发一个ActionEvent事件。,Example: Using Text Areas,This example gives a program that displays an image in a label, a title in a label, and a text in a text area.,Example, cont.,JTextArea不处理滚动,但可以创建一个的对象,将JTextArea的实例放入其中,让JScrollPane为JTextArea处理滚动。JText

48、Area jtaDescription=new JTextArea (“dfdfd”); JScrollPane scrollPane = new JScrollPane(jtaDescription); add(scrollPane, BorderLayout.CENTER);,import java.awt.*; import javax.swing.*; public class TextAreaDemo extends JFrame / Declare and create a description panelprivate DescriptionPanel descriptionP

49、anel = new DescriptionPanel();public static void main(String args) TextAreaDemo frame = new TextAreaDemo();frame.pack();frame.setLocationRelativeTo(null);/ Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setTitle(“TextAreaDemo“);frame.setVisible(true);public TextAreaDemo()

50、 / Set title, text and image in the description paneldescriptionPanel.setTitle(“Canada“);String description = “The Maple Leaf flag nn“ +“The Canadian National Flag was adopted by the Canadian “ +“Parliament on October 22, 1964 and was proclaimed into law “ +“by Her Majesty Queen Elizabeth II (the Qu

51、een of Canada) on “ +“February 15, 1965. The Canadian Flag (colloquially known “ +“as The Maple Leaf Flag) is a red flag of the proportions “ +“two by length and one by width, containing in its center a “ +“white square, with a single red stylized eleven-point “ +“mapleleaf centered in the white squ

52、are.“;descriptionPanel.setDescription(description);descriptionPanel.setImageIcon(new ImageIcon(“image/ca.gif“);/ Add the description panel to the framesetLayout(new BorderLayout();add(descriptionPanel, BorderLayout.CENTER); / Define a panel for displaying image and text class DescriptionPanel extend

53、s JPanel /* Label for displaying an image icon and a text */private JLabel jlblImageTitle = new JLabel();/* Text area for displaying text */private JTextArea jtaDescription = new JTextArea();public DescriptionPanel() / Center the icon and text and place the text under the iconjlblImageTitle.setHoriz

54、ontalAlignment(JLabel.CENTER);jlblImageTitle.setHorizontalTextPosition(JLabel.RIGHT);jlblImageTitle.setVerticalTextPosition(JLabel.BOTTOM);/ Set the font in the label and the text fieldjlblImageTitle.setFont(new Font(“SansSerif“, Font.BOLD, 16);jtaDescription.setFont(new Font(“Serif“, Font.PLAIN, 14

55、);/ Set lineWrap and wrapStyleWord true for the text areajtaDescription.setLineWrap(true);jtaDescription.setWrapStyleWord(true);jtaDescription.setEditable(false);/ Create a scroll pane to hold the text areaJScrollPane scrollPane = new JScrollPane(jtaDescription);/ Set BorderLayout for the panel, add

56、 label and scrollpanesetLayout(new BorderLayout(5, 5); add(scrollPane, BorderLayout.CENTER);add(jlblImageTitle, BorderLayout.WEST);/* Set the title */public void setTitle(String title) jlblImageTitle.setText(title);/* Set the image icon */public void setImageIcon(ImageIcon icon) jlblImageTitle.setIcon(icon);/* Set the text description */public void setDescription(String text) jtaDescription.setText(text); ,

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报