当前位置: 首页 > news >正文

宜兰县网站建设_网站建设公司_jQuery_seo优化

武功县住房和城乡建设局网站,做网站张家口,上海传媒公司总裁,制作凡客诚品帮助中心页面目录 概述 Swing常用窗体 JFrame 窗体 JDialog 对话框 JOptionPane 小型对话框 1.自定义对话框 2.确认框 3.输入框 4.通知框 常用布局管理器 null绝对布局 FlowLayout 流布局管理器 BorderLayout 边界布局管理器 GridLayout 网络布局管理器 常用面板 JPa…目录 概述 Swing常用窗体  JFrame 窗体 JDialog 对话框  JOptionPane 小型对话框  1.自定义对话框 2.确认框  3.输入框  4.通知框  常用布局管理器  null绝对布局 FlowLayout 流布局管理器 BorderLayout 边界布局管理器  GridLayout 网络布局管理器 常用面板  JPanel  面板 JScrollPane 滚动面板  文字标签组件与图标 JLable 标签 图标的使用 按钮组件  JButton 按钮 JRadinButton 单选按钮  1.单选按钮 2.按钮组 列表组件  JComboBox 下拉列表框 JList 列表框  文本组件  JTextField文本框 JPasswordField 密码框 JTextArea 文本域 表格组件 创建表格 DefaultTableModel 表格数据模型 维护表格模型  事件监听器  ActionEvent 动作事件 KeyEvent 键盘事件  MouseEvent 鼠标事件  概述 String包的层次结构和继承关系如下  常用的Swing组件如下表 Swing常用窗体  JFrame 窗体 JFrame 类的常用构造方法包括以下两种形式 public JFrame创建一个初始不可见、没有标题的窗体。 public JFrame(String title)创建一个不可见、具有标题的窗体。 例如创建一个不可见、具有标题的窗体关键代码如下   JFrame jf new JFrame(“登录系统”) Container container jf.getContentPane(); 在创建窗体后先调用getContentPaneO方法将窗体转换为容器再调用addO方法或者removeO方法向容器中添加组件或者删除容器中的组件。向容器中添加按钮关键代码如下 JButton okBtn new JButton(“确定“) container.add(okBtn); 删除容器中的按钮关键代码如下: container.remove(okBtn);  创建窗体后要对窗体进行设置如设置窗体的位置、大小、是否可见等。JFrame 类提供的相应方法可实现上述设置操作具体如下 setBounds(int x, int y, int width, int leight)设置窗体左上角在屏幕中的坐标为(xy窗体的宽度为width窗体的高度为height。 setLocation(int x,int y)设置窗体左上角在屏幕中的坐标为(xy。 setSize(int width, int height)设置窗体的宽度为width高度为height。 setVisibale(boolean b)设置窗体是否可见。b为true时表示可见b为false时表示不可见。setDefaultCloseOperation(int operation)设置窗体的关闭方式默认值为DISPOSE_ON_CLOSE  Java 语言提供了多种窗体的关闭方式常用的有4种如表18.2所示。例题18.1 import java.awt.*; //导入AWT包 import javax.swing.*; //导入Swing包 public class JFreamTest {public static void main(String args[]) { //主方法JFrame jfnew JFrame(); //创建窗体对象jf.setTitle(创建一个JFream 窗体); //设置窗体标题Container containerjf.getContentPane(); //获取主容器JLabel jlnew JLabel(这是一个JLrame 窗体); //一个文本标签jl.setHorizontalAlignment(SwingConstants.CENTER); //使标签上的文字居中container.add(jl); //将标签添加到主容器中jf.setSize(300, 150); //设置窗体宽高jf.setLocation(320,240); //设置窗体在屏幕中出现的位置jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭窗体则停止程序jf.setVisible(true); //让窗体展示出来} }//例题18.1运行结果如下 JDialog 对话框  JDialog 类常用的构造方法如下 public JDialogO创建一个没有标题和父窗体的对话框。 public JDialog(Frame f)创建一个没有标题但指定父窗体的对话框。 public JDialog(Frame f, boolean model)创建一个没有标题但指定父窗体和模式的对话框。如果model为true那么弹出对话框后用户无法操作父窗体。 public JDialog(Frame f, String title)创建一个指定标题和父窗体的对话框.。 public JDialog(Frame f String title, boolean model)创建一个指定标题、父窗体和模式的对话框。 例题18.2   import java.awt.*; import java.awt.event.*; import javax.swing.*;class MyJDialog extends JDialog{ //自定义对话框类继承 JDialogpublic MyJDialog(MyFrame frame) {//调用各类构造方法第一个参数是父窗口第二个参数是窗体标题第三个参数表示阻塞父窗体super(frame,第一个JDialog窗体,true);Container container getContentPane();container.add(new JLabel(这是一个对话框)); //在容器中添加标签setBounds(120,120,100,100); //设置对话框窗体再桌面显示的坐标和大小}} public class MyFrame extends JFrame { //自定义窗体类继承JFrame public MyFrame(){ //窗体的构造方法Container container getContentPane(); //获得窗体主容器container.setLayout(null); //容器使用绝对布局JButton blnew JButton(弹出对话框); //创建一个按钮bl.setBounds(10,10,100,21); //定义按钮在容器中的坐标和大小bl.addActionListener(new ActionListener() { //为按钮添加单击事件public void actionPerformed(ActionEvent e) { //单击事件触发的方法MyJDialog dialog new MyJDialog(MyFrame.this); //创建MyJDialog对话框dialog.setVisible(true); //使对话框可见}});container.add(bl); //将按钮添加到容器中setSize(200,200); //窗体的宽高setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭窗体则停止程序setVisible(true); //使窗体可见 }public static void main(String[] args) {new MyFrame();}}//例题18.2 运行结果如下 JOptionPane 小型对话框  JOptionPane提供了4种创建对话框的方法如下 下面分别介绍这4种对话框的外观样式和使用方法 1.自定义对话框 参数说明如下 parentComponent指明对话框在哪个窗体上显示如果传入具体的窗体对象对话框会在该窗体居中位置显示如果传入null则在屏幕中间弹出对话框。 message提示的信息。 title对话框的标题。 optionType指定可用于对话框的选项的整数DEFAULT_OPTION、YES NO_OPTION.YES NO_CANCEL_OPTION 或 OK_CANCEL_OPTION。 messageType指定消息种类的整数主要用于确定来自可插入外观的图标ERRORMESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE。 icon在对话框中显示的图标。 options指示用户可能选择的对象组成的数组。如果对象是组件则可以正确呈现非String对象使用其toString方法呈现如果此参数为null则由外观确定选项。 initialValue表示对话框的默认选择的对象只有在使用options 时才有意义可以为null。 例题18.3 import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JOptionPane;//例题18.3public class Demo{public static void main(String[] args){Object o[] { new JButton(是的),new JButton(再想想)}; //按钮对象的Object数组Icon icon new ImageIcon(src/pic.png); //获取图标对象JOptionPane.showOptionDialog(null,你做好准备了吗,注意了,JOptionPane.DEFAULT_OPTION,JOptionPane.DEFAULT_OPTION,icon, o, null);} } 运行结果如下 2.确认框  调出带有选项 Yes、No 和Cancel的对话框标题为 Select an Option。 static int showConfirmDialog(Component parentComponent, Object message) 调出一个由optionType参数确定其中选项数的对话框。 static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) 调用一个由optionType参数确定其中选项数的对话框messageType参数确定要显示的图标。 static int showConfirmDialog(Component parentComponent,         Object message,         String title,         int optionType,         int messageType)          Icon icon) 例题18.4 import javax.swing.JOptionPane;public class Demo{//例题18.4public static void main(String[] args){int answer JOptionPane.showConfirmDialog(null,确定离开吗,标题,JOptionPane.YES_NO_CANCEL_OPTION);} } 运行结果如下 3.输入框  输入框已经封装好了一套外观样式弹出后要求用户在文本框中输入文本用户完成输入操作后输入框可以返回用户输入的结果。创建输入框的方法有以下几种重载形式 显示请求用户输入内容的问题消息对话框它以parentComponent为父级。 static String showlnputDialog(Component parentComponent, Object message) 显示请求用户输入内容的问题消息对话框它以parentComponent为父级。 static String showlnputDialog(Component parentComponent, Object message, Object initialSelectionValue) 显示请求用户输入内容的对话框它以parentComponent为父级该对话框的标题为title消息类型为messageType static String showlnputDialog(Component parentComponent, Object message, String title, int messageType) 提示用户在可以指定初始选择、可能选择及其他所有选项的模块化的对话框中输入内容。 static Object showInputDialog(Component parentComponent,                 Object message,                 String title,                 int messageType,                 Icon icon,                 Objectü selectionValues,                 Object initialSelectionValue)   显示请求用户输入的问题消息对话框。 static String showInputDialog(Object message) 显示请求用户输入的问题消息对话框它带有已初始化为initialSelectionValue的输入值 static String showInputDialog(Object message, Object initialSelectionValue) 例题18.5 import javax.swing.JOptionPane;public class Demo{public static void main(String[] args){String name JOptionPane.showInputDialog(null,请输入您的名字);} }//例题18.5 运行结果如下 4.通知框  创建通知框方法有以下几种重载形式 调出标题为Message的信息消息对话框。 static void showMessageDialog(Component parentComponent, Object message) 调出对话框它显示使用由messageType 参数确定的默认图标的message。 static void showMessageDialog(Component parentComponent,                 Object message,                 String title,                 int messageType)   调出一个显示信息的对话框为其指定了所有参数。 static void showMessageDialog(Component parentComponent,                 Object message,                 String title,                 int messageType,                 Icon icon)   例题18.6 import javax.swing.JOptionPane;public class Demo{public static void main(String[] args){JOptionPane.showMessageDialog(null, 您与服务器断开连接,发生错误,JOptionPane.ERROR_MESSAGE);} }//例题18.6 运行结果如下 常用布局管理器  null绝对布局 例题18.7 import java.awt.*; import javax.swing.*; public class AbsolutePosition extends JFrame{//例题18.7public AbsolutePosition(){setTitle(本窗体使用绝对布局); //窗体标题setLayout(null); //使用null布局setBounds(0, 0, 300, 150); //设置窗体的坐标与宽高Container c getContentPane(); //获取主容器JButton b1new JButton(按钮1); //创建按钮JButton b2 new JButton(按钮2);b1.setBounds(10, 30, 80, 30); //设置按钮的位置与大小b2.setBounds(60, 70, 100, 20);c.add(b1); //将按钮添加到容器中c.add(b2);setVisible(true); //使窗体可见setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体则停止程序}public static void main(String[] args) {new AbsolutePosition();} } 运行结果如下 FlowLayout 流布局管理器 FlowLayout类具有以下常用的构造方法 public FlowLayout()。 public FlowLayout(int alignment)。 public FlowLayout(int alignment,int horizGap,int vertGap)。 例题18.8 import java.awt.*; import javax.swing.*; public class FlowLayoutPosition extends JFrame {public FlowLayoutPosition(){setTitle(本窗体使用流布局管理器); //设置窗体标题Container c getContentPane();//窗体使用流布局组件右对齐组件之间的水平间隔为 10 像素垂直间隔为 10像素setLayout(new FlowLayout(FlowLayout.RIGHT,10, 10));for (int i 0;i 10; i){ //在容器中循环添加10个按钮c.add(new JButton(button i));}setSize(300,200); //设置窗体大小setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //关闭窗体则停止程序setVisible(true); //设置窗体可见}public static void main(String[] args){new FlowLayoutPosition();} }//例题18.8 运行结果如下 BorderLayout 边界布局管理器  addo方法被用于实现向容器中添加组件的功能它可以设置组件的摆放位置。addO方法常用的语 法格式如下 public void add(Component comp, Object constraints) comp被添加的组件。constraints被添加组件的布局约束对象。 例题18.9   import java.awt.*; import javax.swing.*; public class BorderLayoutPosition extends JFrame{public BorderLayoutPosition(){setTitle(这个窗体使用边界布局管理器);Container c getContentPane(); //获取主容器setLayout(new BorderLayout()); //容器使用边界布局JButton centerBtn new JButton(中);JButton northBtn new JButton(北);JButton southBtn new JButton(南);JButton westBtn new JButton(西);JButton eastBtn new JButton(东);c.add(centerBtn, BorderLayout.CENTER); //中部添加按钮c.add(northBtn, BorderLayout.NORTH); //北部添加按钮c.add(southBtn, BorderLayout.SOUTH); //南部添加按钮c.add(westBtn, BorderLayout.WEST); //西部添加按钮c.add(eastBtn, BorderLayout.EAST); //东部添加按钮setSize(350, 200); //设置窗体大小setVisible(true); //设置窗体可见setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //关闭窗体则停止程序}public static void main(String[] args){new BorderLayoutPosition();} }//例题18.9 运行结果如下 GridLayout 网络布局管理器 网格布局管理器主要有以下两个常用的构造方法 public GridLayout(int rows, int columns)。 public GridLayout(int rows, int columns, int horizGap, int vertGap)。 其中参数rows 和columns分别代表网格的行数和列数这两个参数只允许有一个参数可以为0被用于表示一行或一列可以排列任意多个组件参数horizGap和vertGap分别代表网格之间的水平间距和垂直间距。 例题18.10 import java.awt.*; import javax.swing.*; public class GridLayoutPosition extends JFrame{public GridLayoutPosition(){Container c getContentPane();//设置容器使用网格布局管理器设置7行3列的网格。组件间水平间距为5像素垂直间距为5像素setLayout(new GridLayout(7, 3, 5, 5));for (int i0;i20; i){c.add(new JButton(buttoni)); //循环添加按钮}setSize(300, 300);setTitle(这是一个使用网格布局管理器的窗体);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args){new GridLayoutPosition();}}//例题18.10 运行结果如下 常用面板  JPanel  面板 JPanel面板必须在窗体容器中使用无法脱离窗体显示 例题18.11 import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.WindowConstants;public class JPaneITest extends JFrame{public JPaneITest(){Container c getContentPane();//将整个容器设置为2行2列的网格布局组件水平间隔10像素垂直间隔10像素c.setLayout(new GridLayout(2, 2, 10, 10));//初始化一个面板此面板使用1行4列的网格布局组件水平间隔10像素垂直间隔10像素JPanel p1 new JPanel(new GridLayout(1, 4, 10, 10));//初始化一个面板此面板使用边界布局JPanel p2 new JPanel(new BorderLayout());//初始化一个面板此面板使用1行2列的网格布局组件水平间隔10像素垂直间隔10像素JPanel p3 new JPanel(new GridLayout(1, 2, 10, 10));//初始化一个面板此面板使用2行1列的网格布局组件水平间隔10像素垂直间隔10像素JPanel p4 new JPanel(new GridLayout(2, 1, 10, 10));//给每个面板都添加边框和标题使用BorderFactory工厂类生成带标题的边框对象p1.setBorder(BorderFactory.createTitledBorder(面板 1));p2.setBorder(BorderFactory.createTitledBorder(面板 2));p3.setBorder(BorderFactory.createTitledBorder(面板 3));p4.setBorder(BorderFactory.createTitledBorder(面板 4));//向面板1中添加按钮p1.add(new JButton(b1));p1.add(new JButton(b1));p1.add(new JButton(b1));p1.add(new JButton(b1));//向面板2中添加按钮p2.add(new JButton(b2), BorderLayout.WEST);p2.add(new JButton(b2), BorderLayout.EAST);p2.add(new JButton(b2), BorderLayout.NORTH);p2.add(new JButton(b2), BorderLayout.SOUTH);p2.add(new JButton(b2), BorderLayout.CENTER);//向面板3中添加按钮p3.add(new JButton(b3));p3.add(new JButton(b3));//向面板4中添加按钮p4.add(new JButton(b4));p4.add(new JButton(b4));//向容器中添加面板c.add(p1);c.add(p2);c.add(p3);c.add(p4);setTitle(在这个窗体中使用了面板);setSize(500, 300); //窗体宽高setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //关闭动作}public static void main(String[] args){JPaneITest test new JPaneITest();test.setVisible(true);} }//例题18.11 运行结果如下 JScrollPane 滚动面板  JScrollPane 滚动面板 不能使用布局管理器且只能容纳一个组件 例题18.12 import java.awt.Container;import javax.swing.*;public class JScrollPaneTest extends JFrame{public JScrollPaneTest() {Container c getContentPane(); //获取主容器JTextArea ta new JTextArea(20,50);JScrollPane sp new JScrollPane(ta);c.add(sp); //将该面板添加到主容器中setTitle(带滚动条的文字编译器);setSize(40,200);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {JScrollPaneTest test new JScrollPaneTest();test.setVisible(true);}}//例题18.12 运行结果如下 文字标签组件与图标 JLable 标签 JLabel类常用的构造方法如下 public JLabel0创建一个不带图标或文本的标签。 public JLabel(Icon icon)创建一个带图标的标签。 public JLabel(Icon icon, int aligment)创建一个带图标的标签并设置图标的水平对齐方式。 public JLabel(String text, int aligment)创建一个带文本的标签并设置文本的水平对齐方式。 public JLabel(String text, Icon icon, int aligment)创建一个带文本和图标的JLabel对象并设置文本和图标的水平对齐方式。 例题18.13 import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants;public class JLableITest extends JFrame{public JLableITest(){Container container getContentPane();JLabel jI new JLabel(这是一个 JFrame 窗体); //创建标签container.add(jI); //将标签添加到容器中setSize(200, 100); //设置窗体大小setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗体关闭模式setVisible(true); //使窗体可见} public static void main(String args[]){new JLableITest();} }//例题18.13 运行结果如下 图标的使用 在Swing 程序设计中图标经常被添加到标签、按钮等组件使用javax.swing.Imagelcon类可以依据现有的图片创建图标。ImageIcon类实现了Icon接口它有多个构造方法常用的如下 public ImagelconO创建一个 Imagelcon 对象创建 ImageIcon对象后使用其调用 setImage(Image image方法设置图片。 public Imagelcon(Image image)依据现有的图片创建图标。 public ImageIcon(URL url)依据现有图片的路径创建图标。 例题18.14 import java.awt.*; import java.net.URL; import javax.swing.*;public class MyImageIcon extends JFrame{public MyImageIcon() {Container container getContentPane();JLabel jl new JLabel(这是一个JLabel 窗体); //创建标签URL url MyImageIcon.class.getResource(pic.png); //获取图片所在的URLIcon icon new ImageIcon(url); //获取图片的Icon对象jl.setIcon(icon); //为标签设置图片jl.setHorizontalAlignment(SwingConstants.CENTER); //设置文字放置在标签中间jl.setOpaque(true); //设置标签为不透明状态container.add(jl); //将标签添加到容器中setSize(300,200); //设置窗体大小setVisible(true); //使窗体可见setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭窗体则停止程序}public static void main(String[] args) {new MyImageIcon();}}//例题18.14 运行结果如下 按钮组件  JButton 按钮 Swing 按钮由JButton对象表示JButton常用的构造方法如下 public JButtonO创建一个不带文本或图标的按钮。 public JButton(String text)创建一个带文本的按钮。 public JButton(Icon icon)创建一个带图标的按钮。 public JButton(String text, Icon icon)创建一个带文本和图标的按钮。 创建JButon 对象后如果要对JButton 对象进行设置那么可以使用JButton类提供的方法。 JButton 类的常用方法及其说明如表18.6所示 例题18.15 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonTest extends JFrame {public JButtonTest(){Icon icon new ImageIcon(src/pic.jpg); //获取图片文件setLayout(new GridLayout(3, 2, 5, 5)); //设置网格布局管理器Container c getContentPane(); //获取主容器JButton btn[]new JButton[6]; //创建按钮数组for (int i 0; i btn.length; i){btn[i] new JButton(); //实例化数组中的对象c.add(btn[i]); //将按钮添加到容器中}btn[0].setText(不可用);btn[0].setEnabled(false); //设置按钮不可用btn[1].setText(有背景色);btn[1].setBackground(Color.YELLOW);btn[2].setText(无边框);btn[2].setBorderPainted(false); //设置按钮边框不显示btn[3].setText(有边框);btn[3].setBorder(BorderFactory.createLineBorder(Color.RED)); //添加红色线型边框btn[4].setIcon(icon); //为按钮设置图标btn[4].setToolTipText(图片按钮); //设置鼠标悬停时提示的文字btn[5].setText(可点击);btn[5].addActionListener(new ActionListener(){ //为按钮添加监听事件public void actionPerformed(ActionEvent e){JOptionPane.showMessageDialog(JButtonTest.this,点击按钮); //出确认对话框}});setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);setTitle(创建不同样式的按钮);setBounds(100, 100, 400,200);}public static void main(String[] args){new JButtonTest();} }//例题18.15 运行结果如下 JRadinButton 单选按钮  1.单选按钮 创建JRadioButton 对象需要使用JRadioButton 类的构造方法。JRadioButton类常用的构造方法如下 public JRadioButton()创建一个未被选中、文本未被设定的单选按钮。 public JRadioButton(Icon icon创建一个未被选中、文本未被设定但具有指定图标的单选按钮。 public JRadioButton(Icon icon, boolean selected)创建一个具有指定图标、选择状态但文本区未被设定的单选按钮。 public JRadioButton(String text)创建一个具有指定文本但未被选中的单选按钮。 public JRadioButton(String text Icon icon)创建一个具有指定文本、指定图标但未被选中的 单选按钮。 public JRadioButton(String text, Icon icon, boolean selected)创建一个具有指定的文本、指定图标和选择状态的单选按钮。 根据上述构造方法的相关介绍不难发现单选按钮的图标、文本和选择状态等属性能够被同时设定。例如使用JRadioButton 类的构造方法创建一个文本为“选项 A”的单选按钮关键代码如下   JRadioButton rbtn new JRadioButton(选项 A);  2.按钮组 Swing 按钮组由 ButtonGroup对象表示多个单选按钮被添加到按钮组后能够实现“选项有多个, 但只能选中一个”的效果。ButtonGroup 对象被创建后可以使用addO方法把多个单选按钮添加到 ButtonGroup对象中。 例题18.6 import javax.swing.*; public class RadioButtonTest extends JFrame {public RadioButtonTest() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle(单选按钮的使用);setBounds(100, 100, 240, 120);getContentPane().setLayout(null);JLabel IblNewLabel new JLabel(请选择性别);IblNewLabel.setBounds(5, 5, 120, 15);getContentPane().add(IblNewLabel);JRadioButton rbtnNormal new JRadioButton(男);rbtnNormal.setSelected(true);rbtnNormal.setBounds(40, 30, 75, 22);getContentPane().add(rbtnNormal);JRadioButton rbtnPwd new JRadioButton(女);rbtnPwd.setBounds(120, 30, 75, 22);getContentPane().add(rbtnPwd);ButtonGroup group new ButtonGroup();//创建按钮组把交互面板中的单选按钮添加到按钮组中group.add(rbtnNormal);group.add(rbtnPwd);}public static void main(String[] args){RadioButtonTest frame new RadioButtonTest(); //创建窗体对象frame.setVisible(true); //使窗体可见} }//例题18.16 运行结果如下 JCheckBox 复选框  JCheckBox的常用构造方法如下: public JCheckBox()创建一个文本、图标未被设定且默认未被选中的复选框。 public JCheckBox(Icon icon, Boolean checked)创建一个具有指定图标、指定初始时是否被选中但文本未被设定的复选框。 public JCheckBox(String text, Boolean checked)创建一个具有指定文本、指定初始时是否被选中但图标未被设定的复选框。 例题18.17   import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.WindowConstants; import javax.swing.event.AncestorListener;public class CheckBoxTest extends JFrame{public CheckBoxTest() {setBounds(100,100,170,110); //床楼大小和坐标setDefaultCloseOperation(EXIT_ON_CLOSE);Container c getContentPane(); //获取主容器c.setLayout(new FlowLayout()); //容器使用流布局JCheckBox c1 new JCheckBox(1); //创建复选框JCheckBox c2 new JCheckBox(2);JCheckBox c3 new JCheckBox(3);c.add(c1); //容器添加复选框c.add(c2);c.add(c3);JButton btn new JButton(打印); //创建“打印”按钮btn.addActionListener(new ActionListener() { //“打印”按钮动作事件public void actionPerformed(ActionEvent e) {System.out.println(c1.getText()按钮选中状态c1.isSelected());System.out.println(c2.getText()按钮选中状态c2.isSelected());System.out.println(c3.getText()按钮选中状态c3.isSelected());}});c.add(btn); //容器添加“打印”按钮setVisible(true);}public static void main(String[] args) {new CheckBoxTest();}}//例题18.17 运行结果如下 选中复选框后运行结果如下 列表组件  JComboBox 下拉列表框 JComboBox 类的常用构造方法如下 public JComboBox(ComboBoxModeldataModel)创建一个 JComboBox对象下拉列表中的列表项使用ComboBoxModel中的列表项ComboBoxModel 是一个用于组合框的数据模型。 public JComboBox(Object[]arrayData)创建一个包含指定数组中的元素的JComboBox对象。 public JComboBox(Vector vector):创建一个包含指定 Vector 对象中的元素的JComboBox 对象.Voetor对象中的元素可以通过整数索引进行访问而且 Vector 对象中的元素可以根据需求被添加或者移除。 JComboBox类的常用方法及其说明如表18.7所示。 例题18.18 import java.awt.event.*; import javax.swing.*; public class JComboBoxTest extends JFrame{public JComboBoxTest() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle(下拉列表框的使用);setBounds(100, 100, 317,147);getContentPane().setLayout(null); //设置绝对布局JLabel IbINewLabel new JLabel(请选择证件); //设置坐标IbINewLabel.setBounds(28, 14, 80, 15); //为下拉列表中添加项getContentPane().add(IbINewLabel);JComboBoxString comboBox new JComboBoxString(); //创建一个下拉列表框comboBox.setBounds(110, 11, 80,21);comboBox.addItem(军人证);comboBox.addItem(身份证);comboBox.addItem(学生证);comboBox.addItem(工作证);comboBox.setEditable(true);getContentPane().add(comboBox); //将下拉列表添加到容器中JLabel IblResult new JLabel();IblResult.setBounds(0, 57, 146, 15);getContentPane().add(IblResult);JButton btnNewButton new JButton(确定);btnNewButton.setBounds(200,10, 67, 23);getContentPane().add(btnNewButton);btnNewButton.addActionListener(new ActionListener() { //为按钮添加监听事件Overridepublic void actionPerformed(ActionEvent argo){//获取下拉列表中的选中项IblResult.setText(您选择的是comboBox.getSelectedItem());}});}public static void main(String[] args){JComboBoxTest frame new JComboBoxTest(); //创建窗体对象frame.setVisible(true); //使窗体可见} }//例题18.18 运行结果如下 JList 列表框  JList类的常用构造方法如下 public void JList()创建一个空的JList对象。 public void JList(Object[] listData)创建一个显示指定数组中的元素的JList对象。 public void JList(Vector listData)创建一个显示指定 Vector 中的元素的JList对象。 public void JList(ListModel dataModel)创建一个显示指定的非 null模型的元素的JList对象。 例题18.19 import java.awt.Container; import java.awt.event.*; import javax.swing.*; public class JListTest extends JFrame {public JListTest(){Container cp getContentPane(); //获取窗体主容器cp.setLayout(null);//容器使用绝对布局//创建字符串数组保存列表中的数据String[] contents {列表 1,列表 2,列表 3,列表 4,列表 5,列表6};JListString jl new JList(contents); //创建列表框并将字符串数组作为构造参数JScrollPane js new JScrollPane(jl); //将列表框放入滚动面板js.setBounds(10, 10, 100, 109); //设没定滚动面板的坐标和大小cp.add(js);JTextArea area new JTextArea(); //创建文本域 JScrollPane scrollPane new JScrollPane(area); //将文本域放入滚动面板scrollPane.setBounds(118, 10, 73, 80);cp.add(scrollPane);JButton btnNewButtonnew JButton(确认); //创建确认按钮btnNewButton.setBounds(120,96, 71, 23); //设定按钮的坐标和大小cp.add(btnNewButton);btnNewButton.addActionListener(new ActionListener() { 添加按钮事件public void actionPerformed(ActionEvent e) {//获取列表中选中的元素返回java.util.List类型java.util.ListString values jl.getSelectedValuesList();area.setText(); //清空文本域for (String value : values){area.append(value n); //在文本域循环追加列表框中选中的值}}});setTitle(在这个窗体中使用了列表框);setSize(217,167);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}public static void main(String args[]) {new JListTest();} }//例题18.19 运行结果如下 文本组件  JTextField文本框 文本框组件由JTextField对象表示。JTextField类的常用构造方法如下 public JTextFieldO创建一个文本未被指定的文本框。 public JTextField(String text)创建一个指定文本的文本框。 public JTextField(int fieldwidth)创建一个指定列宽的文本框。 public JTextField(String text, int fieldwidth)创建一个指定文本和列宽的文本框。 public JTextField(Document docModel, String text, int fieldWidth)创建一个指定文本模型、本内容和列宽的文本框。 如果要为一个文本未被指定的文本框设置文本内容那么需要使用 setTextO方法。setText0方法的语法如下   public void setText(String t) 其中t表示文本框要显示的文本内容。 例题18.20 import java.awt.*; import java.awt.event.*; import javax.swing.*;public class JTextFieldText extends JFrame{public JTextFieldText() {Container c getContentPane(); //获取窗体主容器c.setLayout(new FlowLayout());JTextField jt new JTextField(请点击清除按钮); //设定文本框初始值jt.setColumns(20); //设置文本框长度jt.setFont(new Font(宋体,Font.PLAIN,20)); //设置字体JButton jb new JButton(清除);jt.addActionListener(new ActionListener() { //为文本框添加回车事件public void actionPerformed(ActionEvent arg0) {jt.setText(触发事件); //设置文本框中的值}});jb.addActionListener(new ActionListener() { //为按钮添加事件public void actionPerformed(ActionEvent arg0) {System.out.println(jt.getText()); //输出当前文本框的值jt.setText(); //将文本框置空jt.requestFocus(); //焦点回到文本框}});c.add(jt); //窗体容器添加文本框c.add(jb); //窗体添加按钮setBounds(100,100,250,110);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}public static void main(String[] args) {new JTextFieldText();}}//例题18.20 运行结果如下 JPasswordField 密码框 密码框组件由JPasswordField对象表示其作用是把用户输入的字符串以某种符号进行加密。JPasswordField类的常用构造方法如下 public JPasswordFieldO创建一个文本未被指定的密码框。 public JPasswordFiled(String text)创建一个指定文本的密码框。 public JPasswordField(int fieldwidth)创建一个指定列宽的密码框。 public JPasswordField(String text, int fieldwidth)创建一个指定文本和列宽的密码框。 public JPasswordField(Document docModel, String text, int fieldWidth)创建一个指定文本模型和列宽的密码框。 JPasswordField 类提供了setEchoCharO方法这个方法被用于改变密码框的回显字符。setEchoCharO方法的语法如下 public void setEchoChar(char c) 其中c表示密码框要显示的回显字符 JTextArea 文本域 文本城组件曲 JTextArea 对象表示其作用是接受用户的多行文本输入。JTextArea类的常用构造方法如下 patie TextArea0创建一个文本未被指定的文本域。 publie NTtextArea(String text)创建一个指定文本的文本域。 pabic leatAesfint rows,int columns)创建一个指定行高和列宽但文本未被指定的文本域。 public JTextArea(Document doc) 创建一个指定文档模型的文本域。 public JTextArea(Document doc,String Text,int rows,int columns)创建内容以及行高和列宽的文本域。 例题18.21 import java.awt.*; import javax.swing.*; public class JTextAreaTest extends JFrame {public JTextAreaTest(){setSize(200,100);setTitle(定义自动换行的文本域);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container cp getContentPane(); //获取窗体主容器//创建一个文本内容为“文本域”、行高和列宽均为6的文本域JTextArea jt new JTextArea(文本域,6,6);jt.setLineWrap(true); //可以自动换行cp.add(jt);setVisible(true);}public static void main(String[] args) {new JTextAreaTest();}}//例题18.21 运行结果如下 表格组件 创建表格 JTable 类除提供了默认的构造方法外还提供了被用于显示二维数组中的元素的构造方法这个 构造方法的语法如下 JTable(Object[][] rowData, Objectl columnNames) rowData存储表格数据的二维数组。columnNames存储表格列名的一维数组。 例题18.22 import java.awt.*; import javax.swing.*; public class JTableDemo extends JFrame {public static void main(String args[]) {JTableDemo frame new JTableDemo();frame.setVisible(true);}public JTableDemo(){setTitle(创建可以滚动的表格);setBounds(100, 100, 240, 150);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);String[] columnNames {A,B}; //定义表格列名数组//定义表格数据数组String[][] tableValues {{A1,B1},{A2,B2},{A3,B3},{A4,B4}, {A5, B5}};//创建指定列名和数据的表格JTable table new JTable(tableValues, columnNames);//创建显示表格的滚动面板JScrollPane scrollPane new JScrollPane(table);//将滚动面板添加到边界布局的中间getContentPane().add(scrollPane, BorderLayout.CENTER);} }//例题18.22 运行结果如下 DefaultTableModel 表格数据模型 Swing 使用 TableModel 接口定义了一个表格模型AbstractTableModel 抽象类实现了 TableModel接口的大部分方法只有以下3个抽象方法没有实现 public int getRowCountO); public int getColumnCountO); public Object getValueAt(int rowIndex, int columnIndex); 为了实现使用表格模型创建表格的功能Swing 提供了表格模型类即DefaultTableModel类。DefaultTableModel 类继承了 AbstractTableModel 抽象类且实现了上述3个抽象方法。DefaultTableModel类提供的常用构造方法如表18.8所示 例题18.23 import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class SortingTable extends JFrame {private static final long seriaIVersionUID 1L;public static void main(String args[]){SortingTable frame new SortingTable();frame.setVisible(true);}public SortingTable(){setTitle(表格模型与表格);setBounds(100, 100, 240, 150);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JScrollPane scrollPane new JScrollPane();getContentPane().add(scrollPane, BorderLayout.CENTER);String[] columnNames {A,B}; //定义表格列名数组//定义表格数据数组String[][] tableValues {{A1,B1}, {A2,B2}, {A3,B3}};//创建指定表格列名和表格数据的表格模型DefaultTableModel tableModel new DefaultTableModel(tableValues,columnNames);JTable table new JTable(tableModel);table.setRowSorter(new TableRowSorter(tableModel));scrollPane.setViewportView(table);} }//例题18.23 运行结果如下 维护表格模型  例题18.24 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*;//例题18.24 public class AddAndDeleteDemo extends JFrame{private DefaultTableModel tableModel;private JTable table;private JTextField aTextField;private JTextField bTextField;public static void main(String args[]){AddAndDeleteDemo frame new AddAndDeleteDemo();frame.setVisible(true);}public AddAndDeleteDemo(){setTitle(维护表格模型);setBounds(100, 100, 520, 200);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JScrollPane scrollPane new JScrollPane();getContentPane().add(scrollPane, BorderLayout.CENTER);String[] columnNames {A,B}; //定义表格列名数组//定义表格数据数组String[][] tableValues {{A1,B1}, {A2,B2},{A3,B3}};//创建指定表格列名和表格数据的表格模型tableModel new DefaultTableModel(tableValues,columnNames);table new JTable(tableModel); //创建指定表格模型的表格table.setRowSorter(new TableRowSorter(tableModel)); //设置表格的排序器//设置表格的选择模式为单选table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//为表格添加鼠标事件监听器table.addMouseListener(new MouseAdapter(){public void mouseClicked(MouseEvent e) { //发生了单击事件int selectedRow table.getSelectedRow(); //获得被选中行的索引//从表格模型中获得指定单元格的值Object oa tableModel.getValueAt(selectedRow, 0);//从表格模型中获得指定单元格的值Object ob tableModel.getValueAt(selectedRow, 1);aTextField.setText(oa.toString()); //将值赋值给文本框bTextField.setText(ob.toString()); //将值赋值给文本框}});scrollPane.setViewportView(table);JPanel panel new JPanel();getContentPane().add(panel, BorderLayout.SOUTH);panel.add(new JLabel(A:));aTextField new JTextField(A4, 10);panel.add(aTextField);panel.add(new JLabel(B:));bTextField new JTextField(B4, 10);panel.add(bTextField);JButton addButton new JButton(添加);addButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String[] rowValues {aTextField.getText(),bTextField.getText()}; //创建表格行数组tableModel.addRow(rowValues); //向表格模型中添加一行int rowCount table.getRowCount() 1;aTextField.setText(A rowCount);bTextField.setText(B rowCount);}});panel.add(addButton);JButton updButton new JButton(修改);updButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){int selectedRow table.getSelectedRow(); //获得被选中行的索引if (selectedRow !-1){ //判断是否存在被选中行//修改表格模型中的指定值tableModel.setValueAt(aTextField.getText(), selectedRow, 0);//修改表格模型中的指定值tableModel.setValueAt(bTextField.getText(), selectedRow, 1);}}}); panel.add(updButton);JButton delButton new JButton(删除);delButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){int selectedRow table.getSelectedRow(); //获得被选中行的索引if (selectedRow !-1); //判断是否存在被选中行tableModel.removeRow(selectedRow); //从表格模型中删除指定行}});panel.add(delButton);} } 运行结果如下 事件监听器  ActionEvent 动作事件 动作时间ActionEvent监听器是Swing中比较常用的事件监听器很多组件的动作都会使用它监听例如按钮被单击等。 例题18.25 import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.WindowConstants;public class SimpleEvent extends JFrame{private JButton jb new JButton(我是按钮点击我);public SimpleEvent() {setLayout(null);setSize(200,100);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container cpgetContentPane();cp.add(jb);jb.setBounds(10,10,150,30);jb.addActionListener(new jbAction());setVisible(true);}class jbAction implements ActionListener{public void actionPerformed(ActionEvent arg0) {jb.setText(我被点击了);}}public static void main(String[] args) {new SimpleEvent();}}//例题18.25 运行结果如下 KeyEvent 键盘事件  当向文本框中输入内容时将发生键盘事件。KeyEvent类负责捕获键盘事件可以通过为组件添加实现了KeyListener接口的监听器类来处理相应的键盘事件。 KeyListener接口共有三个抽象方法分别在发生击键事件按下并释放键、按键被按下手指按下键但不松开和按键被释放手指从按下的键松开时被触发具体如下   public interface KeyListener extends EventListener {         public void keyTyped(KeyEvent e);                //发生击键事件时被触发         public void keyPressed(KeyEvent e);              //按键被按下时被触发         public void keyReleased(KeyEvent e);                //按键被释放时被触发   KeyEvent常用方法如下 例题18.26 import java.awt.BorderLayout; import java.awt.EventQueue;import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.Color; import java.awt.Component;import javax.swing.JButton; import java.awt.Font; import javax.swing.SwingConstants; import javax.swing.border.TitledBorder; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList;import javax.swing.JTextField;// 虚拟键盘(键盘的按下与释放)public class KeyBoard extends JFrame { // 创建“键盘”类继承JFrame// 声明窗体中的成员组件private JPanel contentPane;private JTextField textField;private JButton btnQ;private JButton btnW;private JButton btnE;private JButton btnR;private JButton btnT;private JButton btnY;private JButton btnU;private JButton btnI;private JButton btnO;private JButton btnP;private JButton btnA;private JButton btnS;private JButton btnD;private JButton btnF;private JButton btnG;private JButton btnH;private JButton btnJ;private JButton btnK;private JButton btnL;private JButton btnZ;private JButton btnX;private JButton btnC;private JButton btnV;private JButton btnB;private JButton btnN;private JButton btnM;Color green Color.GREEN;// 定义Color对象用来表示按下键的颜色Color white Color.WHITE;// 定义Color对象用来表示释放键的颜色ArrayListJButton btns new ArrayListJButton();// 定义一个集合用来存储所有的按键ID// 自定义一个方法用来将容器中的所有JButton组件添加到集合中private void addButtons() {for (Component cmp : contentPane.getComponents()) {// 遍历面板中的所有组件if (cmp instanceof JButton) {// 判断组件的类型是否为JButton类型btns.add((JButton) cmp);// 将JButton组件添加到集合中}}}//主方法public static void main(String[] args) {EventQueue.invokeLater(new Runnable() { // 使得Runnable中的的run()方法在the system EventQueue的指派线程中被调用public void run() {try {KeyBoard frame new KeyBoard(); // 创建KeyBoard对象frame.setVisible(true); // 使frame可视frame.addButtons();// 初始化存储所有按键的集合} catch (Exception e) {e.printStackTrace();}}});}// 创建JFrame窗体public KeyBoard() { // KeyBoard的构造方法setTitle(\u865A\u62DF\u952E\u76D8\uFF08\u6A21\u62DF\u952E\u76D8\u7684\u6309\u4E0B\u4E0E\u91CA\u653E\uFF09); // 设置窗体题目setResizable(false); // 不可改变窗体宽高setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭的方式setBounds(100, 100, 560, 280); // 设置窗体的位置和宽高//创建JPanel面板contentPane置于JFrame窗体中并设置面板的背景色、边距和布局contentPane new JPanel();contentPane.setBackground(Color.WHITE);contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);//创建按钮button置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnQ new JButton(Q);btnQ.setBackground(white);btnQ.setVerticalAlignment(SwingConstants.TOP);btnQ.setHorizontalAlignment(SwingConstants.LEADING);btnQ.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnQ.setBounds(0, 60, 47, 45);contentPane.add(btnQ);// 创建按钮button_2置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnW new JButton(W);btnW.setBackground(white);btnW.setVerticalAlignment(SwingConstants.TOP);btnW.setHorizontalAlignment(SwingConstants.LEADING);btnW.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnW.setBounds(55, 60, 49, 45);contentPane.add(btnW);// 创建按钮button_3置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnE new JButton(E);btnE.setBackground(white);btnE.setVerticalAlignment(SwingConstants.TOP);btnE.setHorizontalAlignment(SwingConstants.LEADING);btnE.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnE.setBounds(110, 60, 45, 45);contentPane.add(btnE);// 创建按钮button_4置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnR new JButton(R);btnR.setBackground(white);btnR.setVerticalAlignment(SwingConstants.TOP);btnR.setHorizontalAlignment(SwingConstants.LEADING);btnR.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnR.setBounds(165, 60, 45, 45);contentPane.add(btnR);// 创建按钮button_5置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnF new JButton(F);btnF.setBackground(white);btnF.setVerticalAlignment(SwingConstants.TOP);btnF.setHorizontalAlignment(SwingConstants.LEADING);btnF.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnF.setBounds(195, 125, 45, 45);contentPane.add(btnF);//创建按钮button_6置于面板contentPane中设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式btnD new JButton(D);btnD.setBackground(white);btnD.setVerticalAlignment(SwingConstants.TOP);btnD.setHorizontalAlignment(SwingConstants.LEADING);btnD.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnD.setBounds(137, 125, 45, 45);contentPane.add(btnD);btnT new JButton(T);btnT.setVerticalAlignment(SwingConstants.TOP);btnT.setHorizontalAlignment(SwingConstants.LEADING);btnT.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnT.setBackground(white);btnT.setBounds(220, 60, 45, 45);contentPane.add(btnT);btnY new JButton(Y);btnY.setVerticalAlignment(SwingConstants.TOP);btnY.setHorizontalAlignment(SwingConstants.LEADING);btnY.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnY.setBackground(white);btnY.setBounds(275, 60, 45, 45);contentPane.add(btnY);btnU new JButton(U);btnU.setVerticalAlignment(SwingConstants.TOP);btnU.setHorizontalAlignment(SwingConstants.LEADING);btnU.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnU.setBackground(white);btnU.setBounds(330, 60, 45, 45);contentPane.add(btnU);btnI new JButton(I);btnI.setVerticalAlignment(SwingConstants.TOP);btnI.setHorizontalAlignment(SwingConstants.LEADING);btnI.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnI.setBackground(white);btnI.setBounds(385, 60, 45, 45);contentPane.add(btnI);btnO new JButton(O);btnO.setVerticalAlignment(SwingConstants.TOP);btnO.setHorizontalAlignment(SwingConstants.LEADING);btnO.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnO.setBackground(white);btnO.setBounds(440, 60, 46, 45);contentPane.add(btnO);btnP new JButton(P);btnP.setVerticalAlignment(SwingConstants.TOP);btnP.setHorizontalAlignment(SwingConstants.LEADING);btnP.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnP.setBackground(white);btnP.setBounds(495, 60, 45, 45);contentPane.add(btnP);btnA new JButton(A);btnA.setVerticalAlignment(SwingConstants.TOP);btnA.setHorizontalAlignment(SwingConstants.LEADING);btnA.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnA.setBackground(white);btnA.setBounds(23, 125, 45, 45);contentPane.add(btnA);btnS new JButton(S);btnS.setVerticalAlignment(SwingConstants.TOP);btnS.setHorizontalAlignment(SwingConstants.LEADING);btnS.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnS.setBackground(white);btnS.setBounds(82, 125, 45, 45);contentPane.add(btnS);btnG new JButton(G);btnG.setVerticalAlignment(SwingConstants.TOP);btnG.setHorizontalAlignment(SwingConstants.LEADING);btnG.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnG.setBackground(white);btnG.setBounds(251, 125, 45, 45);contentPane.add(btnG);btnH new JButton(H);btnH.setVerticalAlignment(SwingConstants.TOP);btnH.setHorizontalAlignment(SwingConstants.LEADING);btnH.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnH.setBackground(white);btnH.setBounds(306, 125, 45, 45);contentPane.add(btnH);btnJ new JButton(J);btnJ.setVerticalAlignment(SwingConstants.TOP);btnJ.setHorizontalAlignment(SwingConstants.LEADING);btnJ.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnJ.setBackground(white);btnJ.setBounds(361, 125, 45, 45);contentPane.add(btnJ);btnK new JButton(K);btnK.setVerticalAlignment(SwingConstants.TOP);btnK.setHorizontalAlignment(SwingConstants.LEADING);btnK.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnK.setBackground(white);btnK.setBounds(416, 125, 47, 45);contentPane.add(btnK);btnL new JButton(L);btnL.setVerticalAlignment(SwingConstants.TOP);btnL.setHorizontalAlignment(SwingConstants.LEADING);btnL.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnL.setBackground(white);btnL.setBounds(471, 125, 45, 45);contentPane.add(btnL);btnZ new JButton(Z);btnZ.setVerticalAlignment(SwingConstants.TOP);btnZ.setHorizontalAlignment(SwingConstants.LEADING);btnZ.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnZ.setBackground(white);btnZ.setBounds(39, 190, 45, 45);contentPane.add(btnZ);btnX new JButton(X);btnX.setVerticalAlignment(SwingConstants.TOP);btnX.setHorizontalAlignment(SwingConstants.LEADING);btnX.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnX.setBackground(white);btnX.setBounds(107, 190, 45, 45);contentPane.add(btnX);btnC new JButton(C);btnC.setVerticalAlignment(SwingConstants.TOP);btnC.setHorizontalAlignment(SwingConstants.LEADING);btnC.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnC.setBackground(white);btnC.setBounds(178, 190, 45, 45);contentPane.add(btnC);btnV new JButton(V);btnV.setVerticalAlignment(SwingConstants.TOP);btnV.setHorizontalAlignment(SwingConstants.LEADING);btnV.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnV.setBackground(white);btnV.setBounds(250, 190, 45, 45);contentPane.add(btnV);btnB new JButton(B);btnB.setVerticalAlignment(SwingConstants.TOP);btnB.setHorizontalAlignment(SwingConstants.LEADING);btnB.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnB.setBackground(white);btnB.setBounds(315, 190, 45, 45);contentPane.add(btnB);btnN new JButton(N);btnN.setVerticalAlignment(SwingConstants.TOP);btnN.setHorizontalAlignment(SwingConstants.LEADING);btnN.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnN.setBackground(white);btnN.setBounds(382, 190, 47, 45);contentPane.add(btnN);btnM new JButton(M);btnM.setVerticalAlignment(SwingConstants.TOP);btnM.setHorizontalAlignment(SwingConstants.LEADING);btnM.setFont(new Font(Times New Roman, Font.PLAIN, 14));btnM.setBackground(white);btnM.setBounds(449, 190, 48, 45);contentPane.add(btnM);// 创建面板panel置于面板contentPane中设置面板panel的位置、宽高、TitledBorder、背景色以及布局方式边界布局JPanel panel new JPanel();panel.setBorder(new TitledBorder(null, 文本显示区, TitledBorder.LEADING, TitledBorder.TOP, null, null));panel.setBackground(Color.WHITE);panel.setBounds(0, 0, 540, 45);contentPane.add(panel);panel.setLayout(new BorderLayout(0, 0));// 创建文本框textField置于面板panel的中间textField new JTextField();textField.addKeyListener(new KeyAdapter() { // 文本框添加键盘事件的监听char word;Overridepublic void keyPressed(KeyEvent e) { // 按键被按下时被触发word e.getKeyChar();// 获取按下键表示的字符for (int i 0; i btns.size(); i) {// 遍历存储按键ID的ArrayList集合// 判断按键是否与遍历到的按键的文本相同if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {btns.get(i).setBackground(green);// 将指定按键颜色设置为绿色}}}Overridepublic void keyReleased(KeyEvent e) { // 按键被释放时被触发word e.getKeyChar();// 获取释放键表示的字符for (int i 0; i btns.size(); i) {// 遍历存储按键ID的ArrayList集合// 判断按键是否与遍历到的按键的文本相同if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {btns.get(i).setBackground(white);// 将指定按键颜色设置为白色}}}});panel.add(textField, BorderLayout.CENTER);textField.setColumns(10);} } //例题18.26 运行结果如下 MouseEvent 鼠标事件  所有组件都能发生鼠标事件MouseEvent类负责捕获鼠标事件可以通过为组件添加实现MouseListener接口的监听器来处理相应的鼠标事件。 MouseListener接口共有5个抽象方法分别在光标移入或者移出组件、鼠标按键被按下或释放和发生单击事件时被触发。所谓单击事件就是按键被按下并释放。需要注意的是如果按键是在移出组件之后才被释放则不会触发单击事件。MouseListener接口的具体定义如下   public interface MouseListener extends EventListener {         public void mouseEntered(MouseEvent e);        //光标移入组件时被触发         public void mousePressed(MouseEvent e);        //鼠标按键被按下时被触发         public void mouseReleased(MouseEvent e);      //鼠标按键被释放时被触发         public void mouseClicked(MouseEvent e);        //发生单击事件时被触发         public void mouseExited(MouseEvent e);        //光标移出组件时被触发 在上述每个抽象方法中均传入了MouseEvent类的对象。MouseEvent类中比较常用的方法如表18.11 当 需要判断触发此次事件的按键时可以通过表18.12中的静态常量判断由getButton方法返回的int型值代表的键。 例题18.27 import java.awt.BorderLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener;import javax.swing.JFrame; import javax.swing.JLabel;public class MouseEventDemo extends JFrame { // 继承窗体类JFramepublic static void main(String args[]) {MouseEventDemo frame new MouseEventDemo();frame.setVisible(true); // 设置窗体可见默认为不可见}/*** 判断按下的鼠标键并输出相应提示* * param e 鼠标事件*/private void mouseOper(MouseEvent e) {int i e.getButton();if(iMouseEvent.BUTTON1)System.out.println(按下的是鼠标左键);else if(iMouseEvent.BUTTON2)System.out.println(按下的是鼠标滚轮);else if(iMouseEvent.BUTTON3)System.out.println(按下的是鼠标右键);}public MouseEventDemo() {super(); // 继承父类的构造方法setTitle(鼠标事件示例); // 设置窗体的标题setBounds(100, 100, 500, 375); // 设置窗体的显示位置及大小// 设置窗体关闭按钮的动作为退出setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JLabel label new JLabel();label.addMouseListener(new MouseListener() {public void mouseEntered(MouseEvent e) {System.out.println(光标移入组件);}public void mousePressed(MouseEvent e) {System.out.print(鼠标按键被按下);mouseOper(e);}public void mouseReleased(MouseEvent e) {System.out.print(鼠标按键被释放);mouseOper(e);}public void mouseClicked(MouseEvent e) {System.out.print(单击了鼠标按键);mouseOper(e);int clickCount e.getClickCount();System.out.println(单击次数为clickCount下);}public void mouseExited(MouseEvent e) {System.out.println(光标移出组件);}});getContentPane().add(label, BorderLayout.CENTER);}} 运行结果如下
http://www.lebaoying.cn/news/112855.html

相关文章:

  • 成都培训网站建设网页设计构建的基本流程
  • 做网站服务器需要自己提供吗嘉兴网站定制公司
  • 大型自适应的网站开发响应式企业网站 下载
  • 洛阳建设网站公司什么是移动应用开发
  • 怎样在赶集微网站做微招聘网页设计代码模板适应手机界面
  • 金币交易网站开发高端设计
  • 织梦网站建设培训wordpress core
  • ps设计师网站怎么能在百度上做推广
  • wordpress调用标签优化二十条
  • 网站关键词推广企业wordpress内存不足
  • 洛江区建设局网站公司网站模板 网盘下载
  • 爱站seo工具包官网成都十大广告公司
  • 二级网站建设 知乎怎么建网站卖东西
  • 怎么自己制作个网站wordpress 实用主题
  • 建设网站要点一个空间两个网站对seo
  • php企业网站开发教程手机网站宽度自适应
  • 大连建设工程招聘信息网站网站不备案不能访问
  • 万网有网站建设吗网站设计的性能需求
  • 昆明网站免费制作长治门户网
  • ppt免费制作网站深圳互联网推广公司
  • 农村网站建设补助兰州中川国际机场三期
  • 关于对网站建设工作情况的通报网站应该怎么建设
  • 平顶山城市建设局网站咪呜瀑布流WordPress模板
  • 北京建设招聘信息网站wordpress友链自定义排序
  • 黄岛网站建设网站建设准备工作
  • seo网站优化做什么网站seo化自己可以吗
  • 简述网站建设的作用隐私页 wordpress
  • 成都科技网站建设费铁岭做网站公司哪家好
  • 大庆企业做网站西安建设工程交易中心网站
  • 网站性能t恤定制网站哪个好