




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、其它常用的GUI构件设计1:JtextArea::用于多行文本的操作类层次结构:javax.swing Class JTextArea | +- | +- | +- | +- | +-主要构造函数:Constructor SummaryJTextArea() Constructs a new TextArea.JTextArea(Document doc) &
2、#160;Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).JTextArea(Document doc, String text, int rows, int columns) Constructs a new JTextArea with the spec
3、ified number of rows and columns, and the given model.JTextArea(int rows, int columns) Constructs a new empty TextArea with the specified number of rows and columns.JTextArea(String text) &
4、#160; Constructs a new TextArea with the specified text displayed.JTextArea(String text, int rows, int columns) Constructs a new TextArea with the specified text and number of rows and columns.常用方法成员分析:1:得到(g
5、et)或设置(set)区域内的文本信息。这些成员方法是从其父类继承而来。包括全部或部分(通过selected)。2:得到(get)或设置(set)区域内其它(如行数、列数)等。本类自身所有。3:其它等。事件:一般本身不发生事件,而由外部事件而触发(如save, 等)。与TextArea的区别:TextArea自身带有滚动杠,而JtextArea必须人工设置。例: class TextAreaDemo extends JFrame private JTextArea t1, t2; private JButton copy,cut,append;private JPanel jp1; publi
6、c TextAreaDemo() super( "TextArea Demo" ); Box b = Box.createHorizontalBox();Container c = getContentPane();jp1=new JPanel(); c.add( b ); / Box placed in BorderLayout.CENTER c.add(jp1,"North"); String s = "This is a demo string ton" + "illustrate copying textn"
7、; + "from one TextArea to n" + "another TextArea using ann"+ "external eventn" t1 = new JTextArea( s, 30, 20 ); b.add( new JScrollPane( t1 ) ); cut=new JButton( "cut" ); append=new JButton( "append" ); copy = new JButton( "Copy >>>"
8、; ); buttonhandle h1=new buttonhandle(); copy.addActionListener(h1); cut.addActionListener(h1); append.addActionListener(h1); jp1.add( copy );jp1.add( cut );jp1.add( append); t2 = new JTextArea( 10, 20 ); /t2.setEditable( false ); b.add( new JScrollPane( t2 ) ); / Container c = getContentPane(); / c
9、.add( b ); / Box placed in BorderLayout.CENTER setSize( 425, 200 ); show(); private class buttonhandle implements ActionListener public void actionPerformed( ActionEvent e )if(e.getSource()=copy)t2.setText( t1.getSelectedText() );else if(e.getSource()=cut)t1.cut();t2.cut();else if(e.getSource()=appe
10、nd)t2.append(t1.getSelectedText() );public class wcbtext public static void main( String args ) TextAreaDemo app = new TextAreaDemo(); /增加:全部copy, clear等功能。2:对话框:系统提供:消息框,文件对话框、选择颜色对话框等。用户设计。custom dialog1: 消息框: JOptionPane类:用于较简单的对话框,如:显示程序运行信息,提示用户简单输入等。它有四个static方法(分别用于设计四种最简单的对话框设计)。the static s
11、howXxxDialog methods shown below: showConfirmDialog Asks a confirming question, like yes/no/cancel. showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three. 例:JOptionPane.showMessageDialog(nul
12、l, "Eggs aren't supposed to be green.");The code for simple dialogs can be minimal. For example, here's an informational dialog: 当然:可以在字符串中显示程序运行的数据。如:int a=4,b=6,c;c=a*b;JOptionPane.showMessageDialog(null, "c=”+c );String inputValue = JOptionPane.showInputDialog("Please
13、input a value"); int number=Integer.parseInt(inputValue);Object possibleValues = "First", "Second", "Third" ;Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null,possibleValues, pos
14、sibleValues0);if(selectedValue=possibleValues1)JOptionPane.showMessageDialog(null,"alert","alert",JOptionPane.ERROR_MESSAGE); 文件对话框: Two other classes, JColorChooser and JFileChooser, also supply standard dialogsJFileChooser: +- |+-主要方法:1:open文件,save文件intshowOpenDialog(Component&
15、#160;parent) Pops up an "Open File" file chooser dialog. intshowSaveDialog(Component parent) Pops up a "Save File" file chooser dialog注:parent:对话框的拥有类
16、:决定显示的位置。2:构造函数:Constructor SummaryJFileChooser() Constructs a JFileChooser pointing to the user's default directory.JFileChooser(File currentDirectory) Constructs a JFile
17、Chooser using the given File as the path.3:文件的操作方面,如:显示文件的方式,单选、多选文件等。例1:JFileChooser chooser = new JFileChooser();int returnVal = chooser.showOpenDialog(parent); if(returnVal = JFileChooser.APPROVE_OPTION) chooser.getSelectedFile().getName(); Parameters: parent - the parent component of the dialog;
18、 can be null Returns: the return state of the file chooser on popdown: · JFileChooser.CANCEL_OPTION · JFileChooser.APPROVE_OPTION · JFileCHooser.ERROR_OPTION if an error occurs or the dialog is dismissed The following code pops up a file chooser for the user's home directory that
19、sees only .jpg and .gif images: JFileChooser chooser = new JFileChooser(); / Note: source for ExampleFileFilter can be found in FileChooserDemo, / under the demo/jfc directory in the Java 2 SDK, Standard Edition. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"
20、); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal = JFileChooser.APPROVE_OPTION) chooser.getSelectedFile().getName(); Class JDialog | +- | +- | +- | +- | The re
21、st of this section covers the following topics: · An Overview of Dialogs · The DialogDemo Example · JOptionPane Features · Creating and Showing Simple Dialogs · Customizing Button Text · Getting the User's Input from a Dialog · Stopping Automatic Dialog Closing
22、 · The Dialog API · Examples that Use Dialogs An Overview of Dialogs Every dialog is dependent on a frame. When that frame is destroyed, so are its dependent dialogs. When the frame is iconified, its dependent dialogs disappear from the screen. When the frame is deiconified, its dependent
23、dialogs return to the screen. The AWT automatically provides this behavior. A dialog can be modal. When a modal dialog is visible, it blocks user input to all other windows in the program. The dialogs that JOptionPane provides are modal. To create a non-modal dialog, you must use the JDialog class directly. T
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 五冶劳务派遣合同范例
- 协助入户合同范例写
- 针类器械处理流程
- 连锁餐饮工作总结
- 农村购房无证合同范例
- 仓库纸箱配送合同范例
- 银行理财经理的年终总结
- 重力势能知识点
- 野外游玩安全
- 通过创意营销提升企业品牌的知名度
- 2020年反假币单选题试题及答案(金储防伪)
- 房地产开发公司建立质量保证体系情况说明
- 初中物理-汽车前大灯的电路设计教学课件设计
- 新教材新高考新挑战新机遇-核心素养背景下的复习备考建议
- 星级少年事迹材料(精选15篇)
- 信号与系统课件-陈后金-北京交通大学教材资料
- 副井井筒永久锁口安全技术措施
- 清华大学出版社机械制图习题集参考答案(第三版)最全整理PPT通用课件
- 2023年拟任县处级领导干部任职资格考试测试题
- FZ/T 64078-2019熔喷法非织造布
- 三年级软笔书法完整课件
评论
0/150
提交评论