Swing组件三

列表组件

Swing中提供了两种列表组件,下拉列表和列表框。

1. 下拉列表组件

下拉列表是一个带条状的显示区,Swing的下拉列表框使用JComboBox()类对象来表示,它是java.Swing.JComponent类的子类。

常用的构造方法:

Public JComboBox()

Public JComboBox(ComboBoxModel dataModel)

Public JComboBox(Object[]arrayData)

Public JComboBox(Vector vector)

初始化下拉列表中,可以选择同时指定下拉列表框中的项目内容,也可以在程序中使用其他方法设置下拉列表中的内容,下拉列表中的内容可以封装在ComboBoxModel类型、数组或Vector类型中。

2. JComboBox模型

在开发程序中,一般将下拉列表中的项目封装为ComboBoxModel的情况较多。ComboBoxModel为接口,它代表一般模型。但必须实现两种方法

public void setSelectedItem(Object item)

Public Object getSelectedItem()

SelectedItem()方法用于设置下拉列表的选中项,getSelectedItem()方法用于返回下拉列表框的中选中项。自定义这个类除了实现接口之外,还可以继承AbstractListModel类,在该类中也有两个操作下拉列表的重要方法

getSize():返回指定索引处的值

getElementAt(int index):返回指定索引处的值

案例:


public class JComboBoxModelTest extends JFrame {
    JComboBox jc = new JComboBox(new MyComboBox());
    JLabel jl = new JLabel("请选择证件:");

    public JComboBoxModelTest() {
        setSize(new Dimension(160, 180));
        setVisible(true);
        setTitle("在窗口中设置下拉列表框");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(jl);
        cp.add(jc);
    }

    public static void main(String[] args) {
        new JComboBoxModelTest();
    }
}

class MyComboBox extends AbstractListModel implements ComboBoxModel {
    String selecteditem = null;
    String[] test = { "身份证", "军人证", "学生证", "工作证" };
    public Object getElementAt(int index) {
        return test[index];
    }
    public int getSize() {
        return test.length;
    }
    public void setSelectedItem(Object item) {
        selecteditem = (String) item;
    }
    public Object getSelectedItem() {
        return selecteditem;
    }
    public int getIndex() {
        for (int i = 0; i < test.length; i++) {
            if (test[i].equals(getSelectedItem()))
                return i;
            break;
        }
        return 0;
    }
}

结果:


列表框组件

列表框(JList)与下拉列表的区别不仅表现在外观上,当激活下拉表框时还会出现下拉列表的内容。Swing中使用JList类对象来表示列表框,下面是常用的构造方法:

Public void JList()

Public void JList(Object[]listData)

Public void JList(Vector listDta)

Public void JList(ListModel dataModel)

上述构造方法中,存在一个没有参数的构造方法,可以通过初始化列表框使用setListDat()方法对列表进行设置。

案例:


public class JListTest extends JFrame {
    public JListTest() {
        Container cp = getContentPane();
        cp.setLayout(null);
        JList jl = new JList(new MyListModel());
        JScrollPane js = new JScrollPane(jl);
        js.setBounds(10, 10, 100, 100);
        cp.add(js);
        setTitle("在这个窗体中使用了列表框");
        setSize(200, 150);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String args[]) {
        new JListTest();
    }
}

结果:

文本组件:

文本框(JTextField)用来显示或编辑一个单行文本,在Swing中通过javax.swing.JFtextField类对象创建,该类继承了javax.swing.text.JTextComponent类

常用的构造方法:

public JTextField()

public JTextField(String text)

public JTextField(int fieldwidth)

public JTextField(String text,int fieldwidth)

public JTextField(Document docMode,String text,int fieldwidth)

案例:


public class JTextFieldTest extends JFrame {
    public JTextFieldTest(){
        setSize(250,100);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        Container container = getContentPane();
        getContentPane().setLayout(new FlowLayout());
        final JTextField jTextField = new JTextField("aaa",20);
        final  JButton jButton = new JButton("清除");
        container.add(jTextField);
        container.add(jButton);
        jTextField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField.setText("触发事件");
            }
        });
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField.setText(" ");
                jTextField.requestFocus();
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextFieldTest();
    }
}

结果:

密码框组件:

密码框(JPasswordField)与文本框的定义与用法基本相同,唯一不同的是密码框将用户输入的字符串以某种符号进行加密。密码框对象是通过javax.swing.JPasswordField类来创建的。常用的构造方法:

public JPassword()

public JPassword(String text)

public JPassword(int fieldwidth)

public JPassword(String text,int fieldwidth)

public JPassword(Document docModel,String text,int fieldwidth)

文本域组件:

Swing中任何一个文本区域都是JTextArea类型的对象。JTextArea常用的构造方法如下:

public JTextArea()

public JTextArea(String text)

public JTextArea(int rows,int columns)

public JTextArea(Document doc)

public JTextArea(Document doc,String text,int rows,int columns)

案例:


public class JTextAreaTest  extends JFrame{
    public JTextAreaTest(){
        setSize(200,100);
        setTitle("自动换行的文本域");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        Container container = getContentPane();
        JTextArea jTextArea = new JTextArea("文本域",6,6);
        jTextArea.setLineWrap(true);//自动换行
        container.add(jTextArea);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextAreaTest();
    }
}

结果:

常用事件的监听:

Swing事件模型中由三个分离的对象完成对事件的处理,分别为事件源、事件以及监听程序。

事件监听器:实际上就是一个“实现特定类型监听器接口”的类对象。具体的说,事件几乎都已对象来表示,它是某种事件类的对象,事件源(如按钮)会在用户做出相应的动作(如按钮被按下)时产生事件。

动作事件监听器

动作事件(ActionEvent)监听器是Swing中比较常用的事件监听器,很多组件的动作都会使用它监听,如按钮被被单击。

动作事件监听器

事件名称

事件源

监听接口

添加或删除相应类型监听器的方法

ActionEevent

JButton,JList,JTextField等

ActionListener

addActionListener(),removeActionListener()

焦点事件监听器:

焦点事件(FoucsEvent)监听器在实际项目开发中应用也比较广泛,如将光标焦点离开一个文本框时需要弹出一个对话框,或者将焦点返回给该文本框等。

焦点事件监听器

事件名称

事件源

监听接口

添加或删除相应类型监听器的方法

FocusEvent

Compontent以及派生类

FoucsListener

addFocusListener(),removeFocusListener()


猜你喜欢

转载自blog.csdn.net/qq_41934990/article/details/80374865