Swing常用控件使用笔记

xiaoxiao2026-03-11  12

 1netbeans是开发java左面应用程序的一个好的帮手,为了给自己备个案,现在决定把swing的常用的控件记录下来。以备以后使用。

 

  1文本标签控件  Jlable  jlableUserName=new JLabel("用户名:");

  2文本框控件   JtextField username=new JTextField();

  3密码控件         JPasswordField password=new JPasswordField();

      4按钮控件    JButton jbuttonEntry=new JButton();

      5文本区域     JTextArea myTextArea=new JTextArea(int rows,int columns);本身不滚动,要想滚动,需要加如到JScrollPane里面,

     如:JScrollPane myScrollPane=new JscrollPane(mytextArea);

    另外:this.myTextArea.setlineWrap(true/false);设置是否换行。

  6JCheckBox

        A有两个状态:选中,非选中

   B一定要在一个组里面才有效。

   CJCheckBox box=new JCheckBox("文本框"); JCheckBox box=new JCheckBox("check.jpg"); 

          JCheckBox box=new JCheckBox("image",selected)图标和是否选中

  7JRadioButton

        A一定要在组里面

   BButtonGroup bu=new ButtonGroup();

          JRadioButton one=new JRadioButton("one");

          JRadioButton two=new JRadioButton("two");

          bu.add(one);

          bu.add(two);

  8JComboBox

         1JComboBox  box=new JComboBox();

         2构造函数

     JComboBox()

             JComboBox(Object[] items)

             JComboBox(Vector vector)

             JComboBox(ComboBoxModel model)

         3常用方法

         setEditable(boolean flag)

            setMaximumRow(int rows)

            setMaximumRowCount();不滚动时可以看到的最大行数

      getSelectdItem,

            addItem(Object object)

2设置控件的位置:

       this.setbounds(330,250,300,150);

                  this.setVisible(true);

 

3启动一个窗体

       public static void main(String args[]) {                   java.awt.EventQueue.invokeLater(new Runnable() {                   public void run() {                      new BrookJFrame().setVisible(true);                   }                  });                 }

4主要事件

     A动作事件:ActionListener接口

     B键盘事件:KeyListener接口

     C鼠标事件:MouseListener,MouseMotionListerer,MousewheelListerer 3个接口

     D窗口事件:WindowFocusListener,WindowListerer,WindowStateListerer.

 

 

 

转载请注明原文地址: https://www.6miu.com/read-5045687.html

最新回复(0)