版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Swing components are based on AWT classes Java 2 released Swing as an alternative to AWT AWT uses platform-specific GUI components Swing is less reliant on platform-specific componentsmorelessheavyweightcomponentsAWTlightweightcomponentsSwingreliance on platform-specific GUI componentsimport javax.
2、swing.*;import java.awt.*; import java.awt.event.*; ObjectColorcontainerComponentLayoutManagerPanelWindowAppletFrameDialogJComponentJAppletJFrameJDialoglightweightheavyweightSwingAWTprimitivecontainerEvery widget is a container!Heavyweight ComponentsEvery program with a Swing GUI must have at least
3、one top-level Swing container.JFrame implements a single main windowJDialog creates a dialog window.JApplet implements an applet display area within a browser window. FrameMenu BarContent Pane JPanelJScrollPaneJToolBarJSplitPaneJTabbedPane3.2.1 Swing Components and Containers 3.2.1 Swing Components
4、and Containers Component JLabelComponent JLabelComponents of class Components of class JLabelJLabel can display text, an image, or both. can display text, an image, or both. import java.awt.*; import javax.swing.*; public class JLabelDemo extends JPanel private JLabel labelLeft; private JLabel label
5、Center; private JLabel labelRight; public static void main(String args) JFrame frame = new JFrame(JLabelDemo); frame.setContentPane(new JLabelDemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); / Adjust the size of the window frame.setVisible(true); public JLabelDemo() setLay
6、out(new GridLayout(1, 3); setBackground(Color.white); ImageIcon icon = new ImageIcon(images/bananas.gif, an image with bananas); labelLeft = new JLabel(Hello, icon, JLabel.CENTER); labelCenter = new JLabel(Hello); labelRight = new JLabel(icon); labelLeft.setVerticalTextPosition(JLabel.BOTTOM); label
7、Left.setHorizontalTextPosition(JLabel.CENTER); labelLeft.setFont(new Font(Serif, Font.BOLD, 16); labelCenter.setHorizontalAlignment(JLabel.CENTER); labelCenter.setFont(new Font(Serif, Font.BOLD, 30); add(labelLeft); add(labelCenter); add(labelRight); Component JButtonComponent JButtonComponents of c
8、lass JButton can display text, an image, or both. Component JRadioButtonComponent JRadioButtonComponents of class JRadioButton can be selected or deselected by the user. If JRadioButton components are grouped, by means of the class ButtonGroup, only one button at a time can be selected. Component JT
9、extFieldComponent JTextFieldComponents of class JTextField let the user enter (or edit) a small quantity of text. These components are also used to display small amounts of text. import java.awt.*; import javax.swing.*; public class JTextFieldDemo extends JPanel private JLabel labelCountry; private
10、JLabel labelCapital; private JTextField textFieldCountry; private JTextField textFieldCapital; public static void main(String args) JFrame frame = new JFrame(JTextFieldDemo); frame.setContentPane(new JTextFieldDemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); / Adjust the s
11、ize of the window frame.setVisible(true); public JTextFieldDemo() setLayout(new GridLayout(2, 1); setBackground(Color.white); labelCountry = new JLabel(Country:); textFieldCountry = new JTextField(Spain, 10); labelCapital = new JLabel(Capital:); textFieldCapital = new JTextField(Madrid, 10); textFie
12、ldCapital.setBackground(new Color(150, 0, 0); textFieldCapital.setForeground(Color.white); textFieldCapital.setHorizontalAlignment(JTextField.CENTER); textFieldCapital.setEditable(false); JPanel panelCountry = new JPanel(); panelCountry.setBackground(Color.white); panelCountry.add(labelCountry); pan
13、elCountry.add(textFieldCountry); add(panelCountry); JPanel panelCapital = new JPanel(); panelCapital.setBackground(Color.white); panelCapital.add(labelCapital); panelCapital.add(textFieldCapital); add(panelCapital); Component JTextAreaComponent JTextAreaComponents of class JTextArea let the user ent
14、er (or edit) multiple lines of text. A JTextArea component does not have scroll bars. If scroll bars are needed, the JTextArea is wrapped in a JScrollPane, which provides the scroll bars. import java.awt.*; import javax.swing.*; public class JTextAreaDemo extends JPanel private JTextArea textAreaOne
15、; private JTextArea textAreaTwo; public static void main(String args) JFrame frame = new JFrame(JTextAreaDemo); frame.setContentPane(new JTextAreaDemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); / Adjust the size of the window frame.setVisible(true); public JTextAreaDemo()
16、 setBackground(Color.white); String text = Line1nLine2nLine3nLine4nLine5n + Line6nLine7nLine8nLine9nLine10n; textAreaOne = new JTextArea(text, 7, 10); textAreaTwo = new JTextArea(text, 7, 10); textAreaOne.setBackground(new Color(0, 0, 150); textAreaTwo.setBackground(new Color(0, 150, 0); textAreaOne
17、.setForeground(Color.white); textAreaTwo.setForeground(Color.white); textAreaTwo.setEditable(false); add(new JScrollPane(textAreaOne, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); add(new JScrollPane(textAreaTwo, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产业技术联盟协议书
- 飞轮、电化学混合储能调频电站项目风险评估报告
- 休闲浴室转让协议书
- 隧道施工材料选择与使用方案
- 铺面装修住宿合同范本
- 企业房屋修建协议书
- 专利授权协议书范本
- 临时签订就业协议书
- 先付款发货合同协议
- 学前教育中的营养干预对儿童人力资本的积累
- 水质自动站运维服务(含修复)项目 投标方案(技术方案)
- 2025重庆轨道交通集团招聘130人笔试参考题库附带答案详解
- 手术室护理核心制度
- 吊篮软挂专项施工方案
- 《我国社会保障》课件
- 安徽省蚌埠市A层学校2024-2025学年高一上学期11月期中考试物理试题 含解析
- 30人小公司管理制度
- 虚拟主播公司管理制度
- Unit 2 Were Family!Grammar Focus教学设计 2024-2025学年人教版七年级英语上册
- 2025年全国硕士研究生考试(心理学312)真题及答案
- 第二单元+新音乐启蒙+课件【高效课堂精研】高中音乐粤教花城版必修音乐鉴赏
评论
0/150
提交评论