Swing中的组件

 Swing中的组件
 1、  文本域

 构造方法:   JTextField(int cols);

             JTextField(String text,int cols)

 常用一般方法: void setColumns(int cols)

   text-显示文本,cols-列数

   常用文档监听:textfield.getDocument().addDocumentListener(listener);

    接口DocumentListener将被实现的方法:

       void inserUpdate(DocumentEvent event)

       void removeUpdate(DocumentEvent event)

       void changedUpdate(DocumentEvent event)

 2、  文本标签

  构造方法: JLabel(String text)

             JLable(Icon icon)

             JLable(String text,int align)

  一般方法: void setText(String text)

             void setIcon(Icon icon)

    text-标签中的文本,icon-标签中的图标

 3、 密码域

  构造方法: JPasswordField(String text,int columns)//text-将要显示的,colsumns-列数

  一般方法: void setEchoChar(char echo)// echo代替文本字符显示的回显字符

             char[] getPassword()//返回密码域中的文本,使用后最好覆写内容

4、 文本区

  构造方法:JTextArea(int rows,int cols)

            JTextArea(String text,int rows,int cols)

       rows-行数,cols-列数,text-初始文本

  一般方法:void setColumns(int cols)

            void setRows(int rows)

            void append(String newText)//将给定的文本附加到广西区中已有文本的尾部

            void setLineWrap(boolean wrap)//打开或关闭换行

            void setWrapStyleWord(boolean word)//设置换行时是否考虑字符界

            void setTabSize(int c)//设置跳格为c列

5、选择组件

  复选框

      构造方法: JCheckBox(String label)

                JCheckBox(String label,boolean state)

                JCheckBox(String label,Icon icon)

      一般方法: boolean isSelected()

                 void setSelected(boolean state)

  单选按钮

      构造方法: JRadioButton(String label,boolean state)

                JRadioButton(String label,Icon icon)

         构造组成部分:ButtonGroup group=new ButtonGroup();

                      group.add(radiobutton1);

      一般方法: ButtonModel getSelection()//返回该按钮的按钮模型

                String getActionCommand()//返回该按钮的动作命令

                void setActionCommand(String s)//设置这个按钮及其模型的动作命令
6、组合框

   构造方法:JComboBox()

   一般方法: void setEditable(boolean b)//设置组合框域是否可编辑

              void addItem(Object item)//将一选项添加到选项列表中

              void insertItemAt(Object item,int index)//将一选项插入到选项列表的指定位置

              void removeItem(Object item)//从列表中删除一个选项

              void removeItemAt(int index)//删除指定位置的项

              Object getSelectedItem()//返回当前所选项的项

7、滑块

    构造方法:JSlider()

              JSlider(int direction)

              JSlider(int min,int max)

              JSlider(int min,int max,int initialValue)

              JSlider(int direction,int min,intmax,int initialValue)

 direction-SwingConstants.HORIZONTAL或SwingConstants.VERTICAL之一,设置垂直或水平

 min,max-最小值,最大值  initalValue 滑块的初始化值,默认值为50

    一般方法: void setPaintTicks(boolean b)//设置是否显示标尺

              void setMajorTickSpacing(int units)//设置大标尺

              void setMinorTickSpacing(int units)//设置小标尺

              void setPanintLable(boolean b)//设置是否显示标尺标签

              void setSnapToTicks(boolean b)//如果b为true,每一次调整滑块都将对齐到最近的标尺处              void setPaintTrack(boolean b)//如果b为true,显示滑块滑动的轨迹

              void setLabelTable(Dictionary table)

8、带微调控制器的域

   构造方法:JSpinner()//初始值为0,增量为1,没有边界

JSpinner(SpinnerModel model)//构造一个使用指定模型的微调控制器

   一般方法: Object getValue()//得到微调控制器的当前值

void setValue(Object value)//设置微调控制器的值,异常:lllegalArgumentException

void setEditor(Jcomponent editor)//设置用于编辑微调控制器值的组件

数值模型:

    构造方法:SpinnerNumberModel(int initval,int minimum,int maximum,int stepSize)

SpinnerNumberModel(double initval,double minimum,doublet maximum,double stepSize)

Initval:-初始值 minimum-最小值 maximum-最大值 stepSize-增量,最大最小值可缺省

SpinnerListModel(Object[]values)

SpinnerListModel(List values)

日期模型:

构造方法: SpinnerDateModel()

SpinnerDateModel(Date initval,Comparable minimum,Comparable maximum,int step)

Initval:-初始值 minimum-最小值 maximum-最大值 step-增量,最大最小值可缺省

Step为Calendar类的常量,ERA、YEAR、MONTH、WEEK_OF_YEAR。。。。。。

一般方法:String toPattern()//得到这个日期格式器的编辑模式

DateEditor(JSpinner spinner,String pattern)//为微调节器控制器构造一个日期编辑器

 参数 spinner-编辑器所属的微调控制器 pattern 与SimpleDateFormat相关的格式模式

Object getValue()得到该模型的当前值

void setValue(Object value) 设置微调控制器的值,异常:lllegalArgumentException

     Object getNextValue(),Object getPreviousValue() 获取下一个或上一个值

9、菜单

   JMenuBar menuBar=new JmenuBar();//菜单栏

   frame.setJMenuBar(menuBar);//在框架上填加菜单档

   JMenu editMenu=NEW Jmenu(“Edit”);//菜单项

   EditMenu.add(“dd”)//填加内容

   JMenuItem pasteItem=New JmenuItem(“here”);//也是内容,但可填加ActionListener

   EditMenu.addSeparator();//填加分割符

   JMenuItem insert(JMenuItem menu,int index)//在指定位置填加一个新菜单或子菜单

   JMenuItem insert(Action a,int index)//一个名字,图标以及监听器

   void insertSeparator(int index) //填回分割符

   void remove(int index)//从菜单删除一个指定项

   void remove(JMenuItem item)//删除item

   JMenuItem(String label)

   JMenuItem(Action a)

   Void setAction(Action a)

菜单项中的图标:

     JMenuItem cutItem=new JMenuItem(“cut”,new ImageIcon(“cut.gif”));

     JMenuItem(String label,Icon icon);

     void setHorizontalTextPosition(int pos);//设置文本相对于图标的位置

pos:SwingConstants.RIGHT或SwingConstants.LEFT

复选框和单选按钮菜单项

   JCheckBoxMenuItem(String label) JCheckBoxMenuItem(String label,boolean state)

   JRadioButtonMenuItem(String label) JRadioButtonMenuItem(String llabel ,booean state)

boolean isSelected()//返回这一项的选择状态

void setSelected(boolean state)//设置这一项的选择状态

弹出菜单:

JPopupMenu popup=new JPoputMenu();//构造

popup.show(panel,x,y)

component.setComponentPopupMenu(popup);//调用弹出式触发器

child.setInherisPoputMenu(true);//子组件继承父组件的弹出菜单

快捷键和加速器

   JMenuItem cutItem=new JMenuItem(“cut”,”t”)//该菜单项的快捷键为t

   cutAction.putValue(Action.MNEMONIC_KEY,new Integer(‘t’));//效果同上


,cutAction 为该菜单项的一个Action对象

菜单只能用setMnemonic(“h”)方法添加快捷键,h为这个快捷键

openItem.setAcclerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);

组合加速器:ctrl+o

void setEnabled(boolean b);//启用和禁用菜单项

void menuSelected(MenuEvent e)//在菜单被选择但尚未打开之前被调用

void menuDeselected(MenuEvent e);//在菜单被取消并且已经关闭时被调用

void menuCanceled(MenuEvent e);//当菜单被除取消时被调用

10、工具栏

构造方法:JToolBar();

          JToolBar(String titleString);

          JToolBar(int orientation);

          JToolBar(String titleString,int orientation);

Orientation:SwingConstants.HORIZONTAL(默认)或SwingConstants.VERTICAL

   一般方法:void addSeparator();//在工具栏末尾添加一个分隔符

工具提示:exitButton.setToolTipText(“exit”)

或exitAction.putValue(Action.SHORT_DESCRIPTION,”exit”)//exitAction为工具的action对象

11、对话框

非模式对话框在弹出时允许用户进行其它操作,模式对话框不允许。

使用对话框

    * static void showMessageDialog(Component parent, Object message, String title, int messageType, Icon icon)
    * static void showMessageDialog(Component parent, Object message, String title, int messageType)
    * static void showMessageDialog(Component parent, Object message)
    * static void showInternalMessageDialog(Component parent, Object message, String title, int messageType, Icon icon)
    * static void showInternalMessageDialog(Component parent, Object message, String title, int messageType)
    * static void showInternalMessageDialog(Component parent, Object message)

show a message dialog or an internal message dialog. (An internal dialog is rendered entirely within its owner frame.)

Parameters:
    

parent
    

The parent component (can be null)

 
    

message
    

The message to show on the dialog (can be a string, icon, component, or an array of them)

 
    

title
    

The string in the title bar of the dialog

 
    

messageType
    

One of ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

 
    

icon
    

An icon to show instead of one of the standard icons

 

    * static int showConfirmDialog(Component parent, Object message, String title, int optionType, int messageType, Icon icon)
    * static int showConfirmDialog(Component parent, Object message, String title, int optionType, int messageType)
    * static int showConfirmDialog(Component parent, Object message, String title, int optionType)
    * static int showConfirmDialog(Component parent, Object message)
    * static int showInternalConfirmDialog(Component parent, Object message, String title, int optionType, int messageType, Icon icon)
    * static int showInternalConfirmDialog(Component parent, Object message, String title, int optionType, int messageType)
    * static int showInternalConfirmDialog(Component parent, Object message, String title, int optionType)
    * static int showInternalConfirmDialog(Component parent, Object message)

show a confirmation dialog or an internal confirmation dialog. (An internal dialog is rendered entirely within its owner frame.) Returns the option selected by the user (one of OK_OPTION, CANCEL_OPTION, YES_OPTION, NO_OPTION), or CLOSED_OPTION if the user closed the dialog.

Parameters:
    

parent
    

The parent component (can be null)

 
    

message
    

The message to show on the dialog (can be a string, icon, component, or an array of them)

 
    

title
    

The string in the title bar of the dialog

 
    

messageType
    

One of ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

 
    

optionType
    

One of DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION

 
    

icon
    

An icon to show instead of one of the standard icons

 

    * static int showOptionDialog(Component parent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object default)
    * static int showInternalOptionDialog(Component parent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object default)

show an option dialog or an internal option dialog. (An internal dialog is rendered entirely within its owner frame.) Returns the index of the option selected by the user, or CLOSED_OPTION if the user canceled the dialog.

Parameters:
    

parent
    

The parent component (can be null)

 
    

message
    

The message to show on the dialog (can be a string, icon, component, or an array of them)

 
    

title
    

The string in the title bar of the dialog

 
    

messageType
    

One of ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

 
    

optionType
    

One of DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION

 
    

icon
    

An icon to show instead of one of the standard icons

 
    

options
    

An array of options (can be strings, icons, or components)

 
    

default
    

The default option to present to the user

 

    * static Object showInputDialog(Component parent, Object message, String title, int messageType, Icon icon, Object[] values, Object default)
    * static String showInputDialog(Component parent, Object message, String title, int messageType)
    * static String showInputDialog(Component parent, Object message)
    * static String showInputDialog(Object message)
    * static String showInputDialog(Component parent, Object message, Object default) 1.4
    * static String showInputDialog(Object message, Object default) 1.4
    * static Object showInternalInputDialog(Component parent, Object message, String title, int messageType, Icon icon, Object[] values, Object default)
    * static String showInternalInputDialog(Component parent, Object message, String title, int messageType)
    * static String showInternalInputDialog(Component parent, Object message)

show an input dialog or an internal input dialog. (An internal dialog is rendered entirely within its owner frame.) Returns the input string typed by the user, or null if the user canceled the dialog.

Parameters:
    

parent
    

The parent component (can be null)

 
    

message
    

The message to show on the dialog (can be a string, icon, component, or an array of them)

 
    

title
    

The string in the title bar of the dialog

 
    

messageType
    

One of ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

 
    

icon
    

An icon to show instead of one of the standard icons

 
    

values
    

An array of values to show in a combo box

 
    

default
    

The default value to present to the user

创建对话框

    * public JDialog(Frame parent, String title, boolean modal)

constructs a dialog. The dialog is not visible until it is explicitly shown.

Parameters:
    

parent
    

The frame that is the owner of the dialog

 
    

title
    

The title of the dialog

 
    

modal
    

True for modal dialogs (a modal dialog blocks input to other windows)

设置默认按钮:dialog.getRootPane().setDefaultButton(okButton);

Container getAncestorOfClass(Class c,Component comp)//返回给定组件的最先的父容器。该组件属于给定的类或者子类之一。  

文本对话框

javax.swing.JFileChooser 1.2

 

    * JFileChooser()

creates a file chooser dialog box that can be used for multiple frames.

    * void setCurrentDirectory(File dir)

sets the initial directory for the file dialog box.

    * void setSelectedFile(File file)
    * void setSelectedFiles(File[] file)

set the default file choice for the file dialog box.

    * void setMultiSelectionEnabled(boolean b)

sets or clears multiple selection mode.

    * void setFileSelectionMode(int mode)

lets the user select files only (the default), directories only, or both files and directories. The mode parameter is one of JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, and JFileChooser.FILES_AND_DIRECTORIES.

    * int showOpenDialog(Component parent)
    * int showSaveDialog(Component parent)
    * int showDialog(Component parent, String approveButtonText)

show a dialog in which the approve button is labeled "Open", "Save", or with the approveButtonText string. Returns APPROVE_OPTION, CANCEL_OPTION (if the user selected the cancel button or dismissed the dialog), or ERROR_OPTION (if an error occurred).

    * File getSelectedFile()
    * File[] getSelectedFiles()

get the file or files that the user selected (or return null if the user didn't select any file).

    * void setFileFilter(FileFilter filter)

sets the file mask for the file dialog box. All files for which filter.accept returns true will be displayed. Also adds the filter to the list of choosable filters.

    * void addChoosableFileFilter(FileFilter filter)

adds a file filter to the list of choosable filters.

    * void setAcceptAllFileFilterUsed(boolean b)

includes or suppresses an "All files" filter in the filter combo box.

    * void resetChoosableFileFilters()

clears the list of choosable filters. Only the "All files" filter remains unless it is explicitly suppressed.

    * void setFileView(FileView view)

sets a file view to provide information about the files that the file chooser displays.

    * void setAccessory(JComponent component)

sets an accessory component.

javax.swing.filechooser.FileFilter 1.2

 

    * boolean accept(File f)

returns true if the file chooser should display this file.

    * String getDescription()

returns a description of this file filter, for example, "Image files (*.gif,*.jpeg)".

 

 

javax.swing.filechooser.FileView 1.2

 

    * String getName(File f)

returns the name of the file f, or null. Normally, this method simply returns f.getName().

    * String getDescription(File f)

returns a humanly readable description of the file f, or null. For example, if f is an HTML document, this method might return its title.

    * String getTypeDescription(File f)

returns a humanly readable description of the type of the file f, or null. For example, if f is an HTML document, this method might return a string "Hypertext document".

    * Icon getIcon(File f)

returns an icon for the file f, or null. For example, if

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值