Java Swing控件属性归纳

了解各种用户界面组件:

JButton、JLabel、JTextField、JTextArea、JComboBox、 Jlist、JCheckBox、JRadioButton、JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem、JRadioButtonMenuItem、JScrollBar、JScrollPane、JTabbedPane等

一、JButton

¨      按钮是一种点击时触发行为事件的组件。

¨      按钮的四个构造方法:

"          public Jbutton()

                            创建一个空按钮

"          public JButton(String text)

                            创建一个标有指定文字的按钮

"          public JButton(Icon icon)

                            创建一个标有指定图标的按钮

"          public JButton(String text,Icon icon)

                            创建一个标有指定文字和图标的按钮

¨      图标:是一个固定大小的图片,典型的图标体形较小,用于装饰组件。利用类ImageIcon可以从图像文件中得到图标,如:Icon icon=new ImageIcon(“photo.gif”);

JButton 的属性

¨      text:按钮上的标签,例如可用jbt.setText(“OK”)给按钮jbt设置标签。

¨      icon:按钮上的图标,例如可用jbt.setTextIcon(new ImageIcon(“a.gif”))。

¨      mnemonic:指定热键。同时按下ALT键和指定热键相当于按下该按钮。例如使用jbt.setMnemonic(‘O’)可将O设置为按钮jbt的热键。

¨      horizontalAlignment:此属性只有三个值SwingConstants.LEFT,SwingConstants.CENTER, SwingConstants.RIGHT。它指定按钮上标签的水平对齐方式。默认值为居中。

¨      verticalAlignment:此属性也取三个值SwingConstants.TOP, SwingConstants.CENTER和SwingConstants.BOTTOM。它指定按钮上标签的垂直对齐方式。默认值为居中。

¨      horizontalTextPosition:此属性有三个值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。它指定文本相对于图标的水平位置,默认为SwingConstants.RIGHT。对应方法setHorizontalTextPosition。

¨      verticalTextPosition:此属性有三个值SwingConstants.TOP, SwingConstants.CENTER, SwingConstants.BOTTOM。它指定文字相对图标的垂直位置,默认值为SwingConstants.CENTER。对应方法setVerticalTextPosition。

二、JLabel

¨      标签是显示一小段文字、一幅图片或者二者皆有的区域。

¨      它的六个构造方法如下:

"          public JLabel()       

                            创建一个空标签。

"          public JLabel(String text,int horizontalAlignment)

                     创建一个指定内容字符串和水平对齐方式的标签。其中水平对齐方式可取值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。

"          public JLabel(String text)

                            创建一个指定文字的标签。

"          public JLabel(Icon icon)

                            创建一个指定图标的标签。

"          public JLabel(Icon icon,int horizontalAlignment)

                            创建一个指定图标和水平对齐方式的标签。

"          public JLabel(String text,Icon icon,int horizontalAlignment)

                            创建一个指定文本、图标和水平对齐方式的标签。

¨      例如,下面语句创建一个文本内容为“Interest Rate”的标签:

JLabel myLabel = new JLabel(“Interest Rate”);

下面的语句创建一个标签,它使用文件“images/map.gif”中的图像作图标:

JLabel mapLabel = new JLabel(new ImageIcon(“images/map.gif”);

JLabel 的属性

¨      JLabel继承了类Jcomponent的所有属性,并具有JButton类的许多属性,如:

"        text

"        icon

"        horizontalAlignment

"        verticalAlignment

三、JTextField

¨      文本域是一个用户可以输入字符的输入区。允许用户输入各种数据,如姓名和描述文字。

¨      JTextField的四个构造方法:

"          public JTextField()

                            创建一个空文本域。

"          public JTextField(int columns)

                            创建一个指定列数的空文本域。

"          public JTextField(String text)

                            用指定初始文字创建一个文本域。

"          public JTextField(String text,int columns)

                            创建一个文本域,并用指定文字和列数初始化。

JTextField 属性

¨      除了text、horizontalAlignment等属性外,JTextField还有如下属性:

"        editable

                            布尔型属性,表明用户能否修改文本域。

"        columns

                            文本域的宽度。

JTextField 方法

¨      getText()

              从文本域返回字符串。

¨      setText(String text)

              将给定字符串写入文本域当中

¨      setEditable(boolean editable)

              使文本域变为可编辑的,默认为true。

¨      setColumns(int)

              设置文本与的列数,文本域的长度可变。

四、JTextArea

¨      如想让用户输入多行文本,只能通过创建多个JTextField实例来实现,解决问题的更好办法是使用JTextField,它允许用户输入多行文字。

¨      JTextArea的三个构造方法:

"          public JTextArea()

                            创建一个空的文本区。

"        JTextArea(int rows, int columns)

                            创建一个指定行数和列数的文本区。

"        JTextArea(String s, int rows, int columns)

                            创建一个指定文本、行数和列数的文本区。

JTextArea 属性

¨      除了text、editable、columns外,JTextArea还有以下属性:

"        lineWrap

"        wrapStyleWord

"        rows

"        lineCount

"        tabSize

JTextArea 的方法

¨      以下方法用于插入、追加和替换文本:

"          public void inser(String s,int pos)

                            将字符串s插入到文本区的指定位置pos。

"          public void append(String s)

                            将字符串s添加到文本的末尾。

"          public void replaceRange(String s,int start,int end)

                            用字符串s替换文本中从位置start到end的文字。

五、JComboBox

¨      组和框是一些项目的简单列表,用户能够从中进行选择。

¨      JComboBox的两个构造方法:

"        public JComboBox()

                            默认构造方法。

"          public JComboBox(Object[] stringItems)

                            带有字符串列表的构造方法,其中stringItems是一个字符串数祖。

JComboBox的属性

¨      JComboBox的有用的属性:

"          selectedIndex

                            int值,表示组合框中选定项的序号。

"          selectedItem

                            Object类型,表示选定项。

JComboBox 的方法

¨      有用的方法:

"          public void addItem(Object item)

                            在组和框中添加一个选项,它可以是任何对象。

"          public Object getItemAt(int index)

                            得到组合框中指定序号的项。

"          public void removeItem(Object anObject)

                            删除指定的项。

"          public void removeAllItems()

                            删除列表中所有项。

六、JList

¨      列表框的作用和组合框的作用基本相同,但它允许用户同时选择多项。

¨      JList的两个构造方法:

"        JList()

       创建一个空的列表框

"        JList(Object[] stringItems)

       创建一个有初始项的列表框

¨      JList不会自动滚动。给列表框加滚动条的方法与文本区相同,只需创建一个滚动窗格并将列表框加入其中即可。

Jlist的 属性

¨      selectedIndexd

¨      selectedIndices

¨      selectedValue

¨      selectedValues

¨      selectionMode

¨      visibleRowCount

七、JCheckBox

¨      复选框是一种用户能够打开、关闭选项的组件,如同电灯开关一般。

¨      JCheckBox的七个构造方法:

"        JCheckBox()

"        JCheckBox(String text)

"        JCheckBox(String text, boolean selected)

"        JCheckBox(Icon icon)

"        JCheckBox(Icon icon, boolean selected)

"        JCheckBox(String text, Icon icon)

"        JCheckBox(String text, Icon icon, boolean selected)

JCheckBox 的属性

¨      JCheckBox 除了具有JButton的所有属性如text、icon、mnemonic、verticalAlignment、horizontalAlignment、horizontalTextPosition和verticalTextPosition 外,还有selected属性,该属性指明复选框是否被选中。

八、JRadioButton

¨      单选按钮,或叫选择按钮,让用户从一组组件中选择唯一的一个选项。

¨      JRadioButton的七个构造方法:

"        JRadioButton()

"        JRadioButton(String text)

"        JRadioButton(String text, boolean selected)

"        JRadioButton(Icon icon)

"        JRadioButton(Icon icon, boolean selected)

"        JRadioButton(String text, Icon icon)

"        JRadioButton(String text, Icon icon, boolean selected)

JRadioButton 的属性

¨      JRadioButton 除具有JButton的所有属性如text、icon、mnemonic、verticalAlignment、 horizontalAlignment、horizontalTextPosition、verticalTextPosition外,还具有 selected属性,该属性指明单选按钮是否被选中。

将单选钮组合成组

¨      单选按钮可以象按钮一样添加到容器中。要将单选按钮分组,需要创建java.swing.ButtonGroup的一个实例,并用add方法把单选按钮添加到该实例中,如:

ButtonGroup btg = new ButtonGroup();

btg.add(jrb1);

btg.add(jrb2);

上述代码创建了一个单选按钮组,这样就不能同时选择jrb1和jrb2。

九、消息对话框

¨      对话框通常用作临时窗口,用来接受用户的附加信息或提示用户发生了某事件。

¨      消息对话框是一种简单却频繁使用的对话框,用来显示消息提醒用户。

¨      消息对话框是模式的,即消息对话框消失前其他窗口均不可用。

创建消息对话框

¨      使用 JOptionPane类中的静态方法:

"          showMessageDialog(Component parentComponent, Object message, String title, int messageType)

              parentComponet是对话框的父组件,对话框都是由它派生而来的。message是要显示的对象,它通常是一个字符串。title是对话框的标题。messageType决定了所显示消息的类型。

"        showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

              除PLAIN_MESSAGE外,每种消息都有相应的图标,以上方法可支持自制图标。
posted @ 2009-07-24 17:01  翩翩起舞的猪  阅读(2490)  评论(0编辑  收藏  举报