4、按钮
JButton类用来定义按钮。JButton类的常用方法如下:
>addActionListener():注册点击事件监听器;
>setText():设置按钮文字;
>setIcon():设置按钮图标。
通过组建的setMnemonic()方法可以设置组件Mnemonic助记符。通过组件的setTipText可以设置组建的ToolTip提示信息。
在Swing中,JButton是有AbstractButton类派生的。AbstractButton类派生了两个组件:JButton和JtoggleButton组件。JButton是Swing的按钮,而ToggleButton组件是单选按钮和复选框的基类。下面是一个按钮的应用程序。
public class test extendsJFrame implements ActionListener{
JButton jb = new JButton("Clickme!");
public test() throws HeadlessException {
this.setLayout(new GridLayout(2, 2));
this.add(jb);
jb.addActionListener(this);
jb.setMnemonic('b');//给按钮设置组件助记符
jb.setToolTipText("I am abutton");
this.setBounds(10, 10, 600, 400);
this.setVisible(true);
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new test();
}
@Override
public void actionPerformed(ActionEvent e){
this.jb.setText("Clicked");
}
}
5、复选框
在Swing中,用JcheckBox类来定义复选框。复选框和单选按钮有点类似,但是一组复选框中可以有任何数量的复选框被选中。复选框可以为每一次的单击操作添加一个事件。但平常只会监听事件,因为它们让客户来确定该单击操作时选中还是取消选中复选框。
复选框又被称为检测盒。JcheckBox提供选中/未选中两种状态,当用户单击复选框时改变复选框原来设置的状态。
6、弹出式菜单
JPopupMenu是一种Menu的组件,因此在使用JPopupMenu时都需要一个Container来放置JPopupMenu。
通过JpopupMenu类可以定义弹出式菜单,其重要方法有:
>add(JmenuItem e):(往菜单中增加菜单项)
>show():(显示菜单)
通过JMenuItem类来定义菜单项,通过addActionListener()为菜单项增加事件处理。
JpopupMenu是一个可弹出并显示一系列选项的小窗口,可用于用户在菜单栏上选择选项时显示菜单,还可以用于当用户选择菜单项并激活时显示“右拉式(pull-right)“菜单。通过下面的程序进一步了解相关的知识:
public class test extendsJFrame {
//定义一个弹出式菜单
JPopupMenu jpm = new JPopupMenu();
public test() throws HeadlessException {
this.setLayout(new GridLayout(2, 2));
this.setBounds(10, 10, 600, 400);
this.setVisible(true);
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//引入jiaPopMenu()方法
this.jiaPopMenu();
}
public void jiaPopMenu(){
JMenuItem item = newJMenuItem("A");
//为A添加监听事件
item.addActionListener(newActionListener() {
public voidactionPerformed(ActionEvent e) {
System.out.println("AClicked!!!");
}
});
jpm.add(item);//将弹出项添加到菜单
//定义菜单项
item = new JMenuItem("B");
item.addActionListener(newActionListener() {
public voidactionPerformed(ActionEvent e) {
System.out.println("BClicked");
}
});
jpm.add(item);
//编写右键弹出单击事件
this.addMouseListener(newMouseAdapter() {
public voidmouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpm.show(e.getComponent(),e.getX(), e.getY());
}
}
});
}
public static void main(String[] args){
new test();
}
}
使用JPopupMenu组件实现右键快捷菜单功能,并使用ActionListener的对象来将菜单激活,当右击时即可弹出菜单。
7、单选按钮
单选按钮的实质就是在一组按钮中一次只能有一个按钮被选中。单选按钮的外观类似复选框,但是复选框没有对可以选择的选项数目进行限制。对于每一组的单选按钮,必须创建一个ButtonGroup对象实例并且将每一个单选按钮添加到该ButtonGroup对象中。下面的实例讲解的单选按钮的基本用法:
public class test extendsJFrame {
//定义两个单选选项
JRadioButton r1 = newJRadioButton("No.1");
JRadioButton r2 = newJRadioButton("No.2");
//定义一个按钮组
ButtonGroup bg = new ButtonGroup();
public test() throws HeadlessException {
this.setLayout(new GridLayout(3, 1));
this.setBounds(10, 10, 600, 400);
this.setVisible(true);
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//引入jiaPopMenu()方法
this.Add();
}
public void Add(){
//将单选按钮加入按钮组中
bg.add(r1);
bg.add(r2);
//将单选按钮加入布局管理器
this.add(r1);
this.add(r2);
//默认选中r2
r2.setSelected(true);
}
public static void main(String[] args){
new test();
}
}
8、下拉列表框
下拉列表框可以让人感觉到整个界面很简洁,在大的网页中都能感觉到这一点。列表可以有许多选项,所以它们通常被放置在一个滚动窗格中。组合框与下拉列表框相似,区别在于使用组合框时用户可以不从列表中选择项目,还可以选择一个项目。在某些版本的组合框中,还可以输入自己的选择,如浏览器的地址栏。
JComboBox方法很多,它们主要用来管理列表中的数据:
>addItem():添加一个项目到JComboBox
>get/setSelectedIndex():获取/设置JComboBox中选中项目的索引
>get/setSelectedItem():获取/设置选中的对象
>removeAllItems():从JComboBox删除所有对象
>remoteItem():从JComboBox删除特定对象
下面是一个下拉别表框的实例,从中可以更详细的了解到下拉列表框的使用:
public class test extendsJFrame {
//定义下啦菜单
JComboBox jcb = new JComboBox();
public test() throws HeadlessException {
this.setLayout(new GridLayout(3, 1));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
String[] str = new String[10];
for (int i = 0; i < 10; i++) {
//jcb.addItem("此方法直接将内容添加进列表中" +i);
str[i] = "选项" + i;
}
//或者使用Vector来作为item
//Vector v = new Vector();
//v.add("选项1");
//v.add("选项2");
//v.add("选项3");
//jcb = new JComboBox(v);
jcb = new JComboBox(str);
this.add(jcb);
}
public static void main(String[] args){
new test();
}
}
9、选项卡
当今博客盛行的时代,选项卡经常被用到。在博客中,用户经常会改变北京样式,用不同的风格体现个性。而这些完全可以用选项卡来制作。
使用JTabbedPane类可以把几个组件放在一个组件中,如面板。用户可以通过选择对应于目标组件的选项卡来选择要查看的组件。要创建一个选项卡窗格,只要实例化一个JTabbedPane对象,创建要显示的租金啊,然后再将这些组件添加到选项卡窗格中即可。
当创建一个要添加到选项卡窗格的组件时,无论当前可见的是哪个子选项卡,每一个子选项都将获得相同的显示空间。只不过当前显示窗格的高度会比其他窗格稍高一点,以进行区分。下面是一个应用实例:
public class test extendsJFrame {
//定义选项卡
JTabbedPane jtb = newJTabbedPane(JTabbedPane.BOTTOM);
//定义选项
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
public test() throws HeadlessException {
this.setLayout(new GridLayout(1, 1));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
//设置选项事件
jp1.setBackground(Color.green);
jp2.setBackground(Color.red);
jp3.setBackground(Color.BLUE);
this.add(jtb);
//将选项加入选项卡中
jtb.add("绿色背景", jp1);
jtb.add("红色背景", jp2);
jtb.add("蓝色背景", jp3);
}
public static void main(String[] args){
new test();
}
}
10、滑杆
在应用程序中JSlider支持数值变化。它是一种迅速而简单的方式,不仅能让用户以可视形式获得他们当前选择的反馈,还能得到可以接受的值的范围。
JSlider类定义了滑杆组件,它的重要方法有:
>setMaxorTickSpacing():设置主刻度
>setMinorTickSpacing():设置次刻度
>setPaintTicks():设置是否绘制刻度
>setPaintLabels():设置是否绘制标签
>addChangeListener():刻度变化事件处理
>getValue():获取当前滑块位置值
>setValue():设置滑块初始位置
下面是具体应用实例:
public class test extendsJFrame implements ChangeListener{
JSlider js = new JSlider();
JLabel jl = new JLabel("20");
public test() throws HeadlessException {
this.setLayout(new GridLayout(4, 1));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
js.setMaximum(100);//最大刻度
js.setMinimum(0);//最小刻度
js.setOrientation(JSlider.HORIZONTAL);
js.setValue(20);//设置滑杆初始位置
js.setMajorTickSpacing(20);//设置主刻度
js.setMinorTickSpacing(5);//次刻度
js.setPaintTicks(true);//绘制刻度
js.setPaintLabels(true);//绘制标签
this.add(js);
this.add(jl);
js.addChangeListener(this);
}
public static void main(String[] args){
new test();
}
public void stateChanged(ChangeEvent e) {
jl.setText(js.getValue() +"");
}
}
11、滚动条
在Swing中,组件中的内容超出其区域时,就需要使用滚动条。它和网页的滚动条相似。JscrollPane的方法如下:
>getHorizontalScrollBar():返回水平的JscrollBar组件
>getVerticalScrollBar():返回垂直的JscrollBar组件
>get/setHorizontalScrollBarPolicy():Always、Never或As Needed
>get/setVerticalScrollBarPolicy():与水平函数相同
下面是对方法的演示:
public class test extendsJFrame {
JLabel jl = new JLabel();
JScrollPane jsp = new JScrollPane(jl);
JScrollBar jsb =jsp.getVerticalScrollBar();
public test() throws HeadlessException {
this.setLayout(new GridLayout(1, 2));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
this.add(jsp);
jl.setIcon(new ImageIcon("D:\\桌面\\桌面\\image062_s.jpg"));
//监听滚动条的滚动
jsb.addAdjustmentListener(newAdjustmentListener() {
@Override
public voidadjustmentValueChanged(AdjustmentEvent e) {
System.out.println(jsb.getModel() + "");
}
});
System.out.println("处理滚动条的四个基本属性的数据模型:minimum、maximum、value 和extent。" + jsb.getModel());
}
public static void main(String[] args){
new test();
}
}
12、列表框
列表框是一个非常有用的组件,也越来越受到重视。尤其是它具有多选能力,在选择选项时可以按住Shift键进行多选。
JList是一个有用的组件,其类似于一组复选框或一组单选按钮。通过设置,允许对列表框中的项目进行多项选择。JList的方法如下:
>getSelectedIndex():获取选中的第一个索引
>getSelectionMode():设置单项选择还是多项选择
>getListData():设置在列表框中使用数据的模型
>getSelectedValue():获取选中的第一个值
列表框经常与滚动条搭配,因为如果没有滚动条,列表框中的内容可能无法完全显示,下面是一个实例:
public class test extendsJFrame {
//定义列表框
JList jl = new JList(new String[]{"北京" , "天津" , "上海" , "大连" , "青岛" , "武汉" , "西安"});
JScrollPane jsp = new JScrollPane(jl);
public test() throws HeadlessException {
this.setLayout(new GridLayout(4, 2));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
this.add(jsp);
}
public static void main(String[] args){
new test();
}
}
13、菜单
JMenu、JMenuItem和JMenuBar组件是在JFrame中开发菜单系统的主要构造块。任何菜单系统的基础都是JMenuBar。它们就像Word中的文件、编辑一样,下面还有新建、复制、粘贴等一系列内容。其方法如下:
JMenuItem和JMenu:
>get/setAccelerator():获取/设置快捷键
>get/setText():获取/设置菜单的文本
>get/setIcon():获取/设置菜单使用的图片
JMenu专用:
>add():将JMenu或JMenuItem添加到JMenuBar或JMenu中。
JMenu组件使用来存放和整合JMenuItem的组件,也是构成一个菜单不可缺少的组件之一。实现包含JMenuItem的弹出窗口,用户选择JMenuBar上的选项时会显示该JMenuItem。下面是一个关于菜单的应用实例:
public class test extendsJFrame {
//定义菜单条组件
JMenuBar jmb = new JMenuBar();
//定义3个菜单组件
JMenu jm1 = new JMenu("文件");
JMenu jm2 = new JMenu("编辑");
JMenu jm3 = new JMenu("新建");
//定义3个菜单项组件
JMenuItem jmi1 = newJMenuItem("word");
JMenuItem jmi2 = new JMenuItem("复制");
JMenuItem jmi3 = new JMenuItem("粘贴");
public test() throws HeadlessException {
this.setLayout(new GridLayout(1, 1));
this.setBounds(10, 10, 600, 400);
//引入jiaPopMenu()方法
this.Add();
//设置当单机窗口的关闭按钮时退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Add(){
//调整组件间的包含关系
jmb.add(jm1);
jmb.add(jm2);
jm1.add(jm3);
jm3.add(jmi1);
jm2.add(jmi2);
jm2.add(jmi3);
this.setJMenuBar(jmb);
}
public static void main(String[] args){
new test();
}
}
包含关系为:JmenuBar包含JMenu,JMenu可以包含JMenu和JMenuItem