• 第十八章:Swing自述


    18.1 Swing概述

    18.2:Swing常用窗体

    18.2.1:JFrame窗体

    1. package eightth;
    2. import java.awt.*; //导入AWT包
    3. import javax.swing.*; //导入Swing包
    4. public class JFreamTest {
    5. public static void main(String args[]) { // 主方法
    6. JFrame jf = new JFrame();// 创建窗体对象
    7. jf.setTitle("创建一个JFrame窗体");// 设置窗体标题
    8. Container container = jf.getContentPane(); // 获取主容器
    9. JLabel jl = new JLabel("这是一个JFrame窗体");// 一个文本标签
    10. jl.setHorizontalAlignment(SwingConstants.CENTER); // 使标签上的文字居中
    11. container.add(jl); // 将标签添加到主容器中
    12. jf.setSize(300, 150); // 设置窗体宽高
    13. jf.setLocation(320, 240);// 设置窗体在屏幕中出现的位置
    14. jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗体则停止程序
    15. jf.setVisible(true); // 让窗体展示出来
    16. }
    17. }
    18. //例题18.1

    18.2.2:JDialog对话框

    1. package eightth;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. class MyJDialog extends JDialog { // 自定义对话框类,继承JDialog
    6. public MyJDialog(MyFrame frame) {
    7. // 调用父类构造方法,第一个参数是父窗体,第二个参数是窗体标题,第三个参数表示阻塞父窗体
    8. super(frame, "第一个JDialog窗体", true);
    9. Container container = getContentPane(); // 获取主容器
    10. container.add(new JLabel("这是一个对话框")); // 在容器中添加标签
    11. setBounds(120, 120, 100, 100); // 设置对话框窗体在桌面显示的坐标和大小
    12. }
    13. }
    14. public class MyFrame extends JFrame { // 自定义窗体类,继承JFrame
    15. public MyFrame() {// 窗体的构造方法
    16. Container container = getContentPane(); // 获得窗体主容器
    17. container.setLayout(null); // 容器使用绝对布局
    18. JButton bl = new JButton("弹出对话框"); // 创建一个按钮
    19. bl.setBounds(10, 10, 100, 21); // 定义按钮在容器中的坐标和大小
    20. bl.addActionListener(new ActionListener() { // 为按钮添加点击事件
    21. public void actionPerformed(ActionEvent e) {// 点击事件触发的方法
    22. MyJDialog dialog = new MyJDialog(MyFrame.this); // 创建MyJDialo对话框
    23. dialog.setVisible(true); // 使对话框可见
    24. }
    25. });
    26. container.add(bl); // 将按钮添加到容器中
    27. setSize(200, 200); // 窗体的宽高
    28. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗体则停止程序
    29. setVisible(true); // 使窗体可见
    30. }
    31. public static void main(String args[]) {
    32. new MyFrame();
    33. }
    34. }
    35. //例题18.2

    18.2.3:JOptionPane小型对话框

    1.通知框

    1. package eightth;
    2. import javax.swing.Icon;
    3. import javax.swing.ImageIcon;
    4. import javax.swing.JButton;
    5. import javax.swing.JOptionPane;
    6. public class Demo {
    7. public static void main(String[] args) {
    8. Object o[] = {new JButton("是的"), new JButton("再想想")}; // 按钮对象的Object数组
    9. Icon icon = new ImageIcon("src/注意.png"); // 获取图标对象
    10. JOptionPane.showOptionDialog(null, "你做好准备了吗?", "注意了!",
    11. JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, icon, o, null);
    12. }
    13. }
    14. //例题18.3

     2.确认框

    1. package eightth;
    2. import javax.swing.JOptionPane;
    3. public class Demo4 {
    4. public static void main(String[] args) {
    5. int answer = JOptionPane.showConfirmDialog(null,
    6. "确定离开吗?",
    7. "标题",
    8. JOptionPane.YES_NO_OPTION);
    9. }
    10. }
    11. //例题18.4
    1. package eightth;
    2. import javax.swing.JOptionPane;
    3. public class Demo4 {
    4. public static void main(String[] args) {
    5. int answer = JOptionPane.showConfirmDialog(null,
    6. "确定离开吗?",
    7. "标题",
    8. JOptionPane.YES_NO_CANCEL_OPTION);
    9. }
    10. }
    11. //例题18.4

     

    3.输入框

    1. package eightth;
    2. import javax.swing.JOptionPane;
    3. public class Demo5 {
    4. public static void main(String[] args) {
    5. String name = JOptionPane.showInputDialog(null, "请输入您的姓名");
    6. }
    7. }
    8. //例题18.5

    4.通知框

    1. package eightth;
    2. import javax.swing.JOptionPane;
    3. public class Demo6 {
    4. public static void main(String[] args) {
    5. JOptionPane.showMessageDialog(null,
    6. "您与服务器断开了连接",
    7. "发生错误",
    8. JOptionPane.ERROR_MESSAGE);
    9. }
    10. }
    11. //例题18.6

    18.3:常用布局管理器 

    18.3.1:null绝对布局

    18.3.2:BorderLayout边界布局管理器

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class AbsolutePosition extends JFrame {
    5. public AbsolutePosition() {
    6. setTitle("本窗体使用绝对布局"); // 窗体标题
    7. setLayout(null); // 使用null布局
    8. setBounds(0, 0, 300, 150); // 设置窗体的坐标与宽高
    9. Container c = getContentPane(); // 获取主容器
    10. JButton b1 = new JButton("按钮1"); // 创建按钮
    11. JButton b2 = new JButton("按钮2");
    12. b1.setBounds(10, 30, 80, 30); // 设置按钮的位置与大小
    13. b2.setBounds(60, 70, 100, 20);
    14. c.add(b1); // 将按钮添加到容器中
    15. c.add(b2);
    16. setVisible(true); // 使窗体可见
    17. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗体则停止程序
    18. }
    19. public static void main(String[] args) {
    20. new AbsolutePosition();
    21. }
    22. }
    23. //例题18.7

     

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class FlowLayoutPosition extends JFrame {
    5. public FlowLayoutPosition() {
    6. setTitle("本窗体使用流布局管理器"); // 设置窗体标题
    7. Container c = getContentPane();
    8. // 窗体使用流布局,组件右对齐,组件之间的水平间隔为10像素,垂直间隔10像素
    9. setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
    10. for (int i = 0; i < 10; i++) { // 在容器中循环添加10个按钮
    11. c.add(new JButton("button" + i));
    12. }
    13. setSize(300, 200); // 设置窗体大小
    14. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // 关闭窗体则停止程序
    15. setVisible(true); // 设置窗体可见
    16. }
    17. public static void main(String[] args) {
    18. new FlowLayoutPosition();
    19. }
    20. }
    21. //例题18.8

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class BorderLayoutPosition extends JFrame {
    5. public BorderLayoutPosition() {
    6. setTitle("这个窗体使用边界布局管理器");
    7. Container c = getContentPane(); // 获取主容器
    8. setLayout(new BorderLayout()); // 容器使用边界布局
    9. JButton centerBtn = new JButton("中");
    10. JButton northBtn = new JButton("北");
    11. JButton southBtn = new JButton("南");
    12. JButton westBtn = new JButton("西");
    13. JButton eastBtn = new JButton("东");
    14. c.add(centerBtn, BorderLayout.CENTER); // 中部添加按钮
    15. c.add(northBtn, BorderLayout.NORTH); // 北部添加按钮
    16. c.add(southBtn, BorderLayout.SOUTH); // 南部添加按钮
    17. c.add(westBtn, BorderLayout.WEST); // 西部添加按钮
    18. c.add(eastBtn, BorderLayout.EAST); // 东部添加按钮
    19. setSize(350, 200); // 设置窗体大小
    20. setVisible(true); // 设置窗体可见
    21. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // 关闭窗体则停止程序
    22. }
    23. public static void main(String[] args) {
    24. new BorderLayoutPosition();
    25. }
    26. }
    27. //18.9

    18.3.4:GridLayout网格布局管理器

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class GridLayoutPosition extends JFrame {
    5. public GridLayoutPosition() {
    6. Container c = getContentPane();
    7. // 设置容器使用网格布局管理器,设置7行3列的网格。组件间水平间距为5像素,垂直间距为5像素
    8. setLayout(new GridLayout(7, 3, 5, 5));
    9. for (int i = 0; i < 20; i++) {
    10. c.add(new JButton("button" + i)); // 循环添加按钮
    11. }
    12. setSize(300, 300);
    13. setTitle("这是一个使用网格布局管理器的窗体");
    14. setVisible(true);
    15. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    16. }
    17. public static void main(String[] args) {
    18. new GridLayoutPosition();
    19. }
    20. }
    21. //例题18.10

     

    18.4:常用面板

    18.4.1:JPanel面板

    1. package eightth;
    2. import java.awt.BorderLayout;
    3. import java.awt.Container;
    4. import java.awt.GridLayout;
    5. import javax.swing.BorderFactory;
    6. import javax.swing.JButton;
    7. import javax.swing.JFrame;
    8. import javax.swing.JPanel;
    9. import javax.swing.WindowConstants;
    10. public class JPanelTest extends JFrame {
    11. public JPanelTest() {
    12. Container c = getContentPane();
    13. // 将整个容器设置为2行2列的网格布局,组件水平间隔10像素,垂直间隔10像素
    14. c.setLayout(new GridLayout(2, 2, 10, 10));
    15. // 初始化一个面板,此面板使用1行4列的网格布局,组件水平间隔10像素,垂直间隔10像素
    16. JPanel p1 = new JPanel(new GridLayout(1, 4, 10, 10));
    17. // 初始化一个面板,此面板使用边界布局
    18. JPanel p2 = new JPanel(new BorderLayout());
    19. // 初始化一个面板,此面板使用1行2列的网格布局,组件水平间隔10像素,垂直间隔10像素
    20. JPanel p3 = new JPanel(new GridLayout(1, 2, 10, 10));
    21. // 初始化一个面板,此面板使用2行1列的网格布局,组件水平间隔10像素,垂直间隔10像素
    22. JPanel p4 = new JPanel(new GridLayout(2, 1, 10, 10));
    23. // 给每个面板都添加边框和标题,使用BorderFactory工厂类生成带标题的边框对象
    24. p1.setBorder(BorderFactory.createTitledBorder("面板1"));
    25. p2.setBorder(BorderFactory.createTitledBorder("面板2"));
    26. p3.setBorder(BorderFactory.createTitledBorder("面板3"));
    27. p4.setBorder(BorderFactory.createTitledBorder("面板4"));
    28. // 在面板1中添加按钮
    29. p1.add(new JButton("b1"));
    30. p1.add(new JButton("b1"));
    31. p1.add(new JButton("b1"));
    32. p1.add(new JButton("b1"));
    33. // 向面板2中添加按钮
    34. p2.add(new JButton("b2"), BorderLayout.WEST);
    35. p2.add(new JButton("b2"), BorderLayout.EAST);
    36. p2.add(new JButton("b2"), BorderLayout.NORTH);
    37. p2.add(new JButton("b2"), BorderLayout.SOUTH);
    38. p2.add(new JButton("b2"), BorderLayout.CENTER);
    39. // 向面板3中添加按钮
    40. p3.add(new JButton("b3"));
    41. p3.add(new JButton("b3"));
    42. // 向面板4中添加按钮
    43. p4.add(new JButton("b4"));
    44. p4.add(new JButton("b4"));
    45. // 向容器中添加面板
    46. c.add(p1);
    47. c.add(p2);
    48. c.add(p3);
    49. c.add(p4);
    50. setTitle("在这个窗体中使用了面板");
    51. setSize(500, 300);// 窗体宽高
    52. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // 关闭动作
    53. }
    54. public static void main(String[] args) {
    55. JPanelTest test = new JPanelTest();
    56. test.setVisible(true);
    57. }
    58. }
    59. //例题18.11

     18.4.2:JScrollPane滚动面板

    1. package eightth;
    2. import java.awt.Container;
    3. import javax.swing.JFrame;
    4. import javax.swing.JScrollPane;
    5. import javax.swing.JTextArea;
    6. import javax.swing.WindowConstants;
    7. public class JScrollPaneTest extends JFrame {
    8. public JScrollPaneTest() {
    9. Container c = getContentPane(); // 获取主容器
    10. // 创建文本区域组件,文本域默认大小为20行、50列
    11. JTextArea ta = new JTextArea(20, 50);
    12. // 创建JScrollPane滚动面板,并将文本域放到滚动面板中
    13. JScrollPane sp = new JScrollPane(ta);
    14. c.add(sp); // 将该面板添加到主容器中
    15. setTitle("带滚动条的文字编译器");
    16. setSize(400, 200);
    17. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    18. }
    19. public static void main(String[] args) {
    20. JScrollPaneTest test = new JScrollPaneTest();
    21. test.setVisible(true);
    22. }
    23. }
    24. //例题18.12

    18.5:文字标签组件与图标

    18.5.1:JLabel标签

    1. package eightth;
    2. import java.awt.Container;
    3. import javax.swing.JFrame;
    4. import javax.swing.JLabel;
    5. import javax.swing.WindowConstants;
    6. public class JLabelTest extends JFrame {
    7. public JLabelTest() {
    8. Container container = getContentPane();
    9. // 创建一个标签
    10. JLabel jl = new JLabel("这是一个JFrame窗体");
    11. container.add(jl); // 将标签添加到容器中
    12. setSize(200, 100); // 设置窗体大小
    13. // 设置窗体关闭模式
    14. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    15. setVisible(true); // 使窗体可见
    16. }
    17. public static void main(String args[]) {
    18. new JLabelTest(); // 创建MyImageIcon对象
    19. }
    20. }
    21. //例题18.13

    18.5.2:图标的使用

    1. package eightth;
    2. import java.awt.Container;
    3. import java.net.URL;
    4. import javax.swing.Icon;
    5. import javax.swing.ImageIcon;
    6. import javax.swing.JFrame;
    7. import javax.swing.JLabel;
    8. import javax.swing.SwingConstants;
    9. import javax.swing.WindowConstants;
    10. public class MyImageIcon extends JFrame {
    11. public MyImageIcon() {
    12. Container container = getContentPane();
    13. //创建一个标签
    14. JLabel jl = new JLabel("这是一个JFrame窗体", JLabel.CENTER);
    15. //获取图片所在的URL
    16. URL url = MyImageIcon.class.getResource("pic.png");
    17. Icon icon = new ImageIcon(url); //创建Icon对象
    18. jl.setIcon(icon); //为标签设置图片
    19. //设置文字放置在标签中间
    20. jl.setHorizontalAlignment(SwingConstants.CENTER);
    21. jl.setOpaque(true); //设置标签为不透明状态
    22. container.add(jl); //将标签添加到容器中
    23. setSize(300, 200); //设置窗体大小
    24. setVisible(true); //使窗体可见
    25. //设置窗体关闭模式
    26. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    27. }
    28. public static void main(String args[]) {
    29. new MyImageIcon(); //创建MyImageIcon对象
    30. }
    31. }
    32. //例题18.14

    18.6:按钮组件 

    18.6.1:JButton按钮

    1. package eightth;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. public class JButtonTest extends JFrame {
    6. public JButtonTest() {
    7. Icon icon = new ImageIcon("src/imageButtoo.jpg"); // 获取图片文件
    8. setLayout(new GridLayout(3, 2, 5, 5)); // 设置网格布局管理器
    9. Container c = getContentPane(); // 获取主容器
    10. JButton btn[] = new JButton[6]; // 创建按钮数组
    11. for (int i = 0; i < btn.length; i++) {
    12. btn[i] = new JButton(); // 实例化数组中的对象
    13. c.add(btn[i]); // 将按钮添加到容器中
    14. }
    15. btn[0].setText("不可用");
    16. btn[0].setEnabled(false); // 设置按钮不可用
    17. btn[1].setText("有背景色");
    18. btn[1].setBackground(Color.YELLOW);
    19. btn[2].setText("无边框");
    20. btn[2].setBorderPainted(false); // 设置按钮边框不显示
    21. btn[3].setText("有边框");
    22. btn[3].setBorder(BorderFactory.createLineBorder(Color.RED)); // 添加红色线型边框
    23. btn[4].setIcon(icon); // 为按钮设置图标
    24. btn[4].setToolTipText("图片按钮"); // 设置鼠标悬停时提示的文字
    25. btn[5].setText("可点击");
    26. btn[5].addActionListener(new ActionListener() { // 为按钮添加监听事件
    27. public void actionPerformed(ActionEvent e) {
    28. JOptionPane.showMessageDialog(JButtonTest.this, "点击按钮"); // 弹出确认对话框
    29. }
    30. });
    31. setDefaultCloseOperation(EXIT_ON_CLOSE);
    32. setVisible(true);
    33. setTitle("创建不同样式的按钮");
    34. setBounds(100, 100, 400, 200);
    35. }
    36. public static void main(String[] args) {
    37. new JButtonTest();
    38. }
    39. }
    40. //例题18.15package eightth;
    41. import java.awt.*;
    42. import java.awt.event.*;
    43. import javax.swing.*;
    44. public class JButtonTest extends JFrame {
    45. public JButtonTest() {
    46. Icon icon = new ImageIcon("src/imageButtoo.jpg"); // 获取图片文件
    47. setLayout(new GridLayout(3, 2, 5, 5)); // 设置网格布局管理器
    48. Container c = getContentPane(); // 获取主容器
    49. JButton btn[] = new JButton[6]; // 创建按钮数组
    50. for (int i = 0; i < btn.length; i++) {
    51. btn[i] = new JButton(); // 实例化数组中的对象
    52. c.add(btn[i]); // 将按钮添加到容器中
    53. }
    54. btn[0].setText("不可用");
    55. btn[0].setEnabled(false); // 设置按钮不可用
    56. btn[1].setText("有背景色");
    57. btn[1].setBackground(Color.YELLOW);
    58. btn[2].setText("无边框");
    59. btn[2].setBorderPainted(false); // 设置按钮边框不显示
    60. btn[3].setText("有边框");
    61. btn[3].setBorder(BorderFactory.createLineBorder(Color.RED)); // 添加红色线型边框
    62. btn[4].setIcon(icon); // 为按钮设置图标
    63. btn[4].setToolTipText("图片按钮"); // 设置鼠标悬停时提示的文字
    64. btn[5].setText("可点击");
    65. btn[5].addActionListener(new ActionListener() { // 为按钮添加监听事件
    66. public void actionPerformed(ActionEvent e) {
    67. JOptionPane.showMessageDialog(JButtonTest.this, "点击按钮"); // 弹出确认对话框
    68. }
    69. });
    70. setDefaultCloseOperation(EXIT_ON_CLOSE);
    71. setVisible(true);
    72. setTitle("创建不同样式的按钮");
    73. setBounds(100, 100, 400, 200);
    74. }
    75. public static void main(String[] args) {
    76. new JButtonTest();
    77. }
    78. }
    79. //例题18.15

     

    18.6.2:JRadioButton单选按钮

    1. package eightth;
    2. import javax.swing.*;
    3. public class RadioButtonTest extends JFrame {
    4. public RadioButtonTest() {
    5. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    6. setTitle("单选按钮的使用");
    7. setBounds(100, 100, 240, 120);
    8. getContentPane().setLayout(null); // 设置绝对布局
    9. JLabel lblNewLabel = new JLabel("请选择性别:");
    10. lblNewLabel.setBounds(5, 5, 120, 15);
    11. getContentPane().add(lblNewLabel);
    12. JRadioButton rbtnNormal = new JRadioButton("男");
    13. rbtnNormal.setSelected(true);
    14. rbtnNormal.setBounds(40, 30, 75, 22);
    15. getContentPane().add(rbtnNormal);
    16. JRadioButton rbtnPwd = new JRadioButton("女");
    17. rbtnPwd.setBounds(120, 30, 75, 22);
    18. getContentPane().add(rbtnPwd);
    19. // 创建按钮组,把交互面板中的单选按钮添加到按钮组中
    20. ButtonGroup group = new ButtonGroup();
    21. group.add(rbtnNormal);
    22. group.add(rbtnPwd);
    23. }
    24. public static void main(String[] args) {
    25. RadioButtonTest frame = new RadioButtonTest(); // 创建窗体对象
    26. frame.setVisible(true); // 使窗体可见
    27. }
    28. }
    29. //例题18.16

     

    18.6.3:JCheckBox

    1. package eightth;
    2. import java.awt.Container;
    3. import java.awt.FlowLayout;
    4. import java.awt.event.ActionEvent;
    5. import java.awt.event.ActionListener;
    6. import javax.swing.JButton;
    7. import javax.swing.JCheckBox;
    8. import javax.swing.JFrame;
    9. public class CheckBoxTest extends JFrame {
    10. public CheckBoxTest() {
    11. setBounds(100, 100, 170, 110); // 窗口坐标和大小
    12. setDefaultCloseOperation(EXIT_ON_CLOSE);
    13. Container c = getContentPane(); // 获取主容器
    14. c.setLayout(new FlowLayout()); // 容器使用流布局
    15. JCheckBox c1 = new JCheckBox("1"); // 创建复选框
    16. JCheckBox c2 = new JCheckBox("2");
    17. JCheckBox c3 = new JCheckBox("3");
    18. c.add(c1); // 容器添加复选框
    19. c.add(c2);
    20. c.add(c3);
    21. JButton btn = new JButton("打印"); // 创建打印按钮
    22. btn.addActionListener(new ActionListener() { // 打印按钮动作事件
    23. public void actionPerformed(ActionEvent e) {
    24. // 在控制台分别输出三个复选框的选中状态
    25. System.out.println(c1.getText() + "按钮选中状态:" + c1.isSelected());
    26. System.out.println(c2.getText() + "按钮选中状态:" + c2.isSelected());
    27. System.out.println(c3.getText() + "按钮选中状态:" + c3.isSelected());
    28. }
    29. });
    30. c.add(btn); // 容器添加打印按钮
    31. setVisible(true);
    32. }
    33. public static void main(String[] args) {
    34. new CheckBoxTest();
    35. }
    36. }
    37. //例题18.17

     

    选中之后控制台会返回真或者假

    18.7:列表组件

    18.7.1:JComboBox下拉列表框

    1. package eightth;
    2. import java.awt.event.ActionEvent;
    3. import java.awt.event.ActionListener;
    4. import javax.swing.JButton;
    5. import javax.swing.JComboBox;
    6. import javax.swing.JFrame;
    7. import javax.swing.JLabel;
    8. public class JComboBoxTest extends JFrame {
    9. public JComboBoxTest() {
    10. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    11. setTitle("下拉列表框的使用");
    12. setBounds(100, 100, 317, 147);
    13. getContentPane().setLayout(null); // 设置绝对布局
    14. JLabel lblNewLabel = new JLabel("请选择证件:");
    15. lblNewLabel.setBounds(28, 14, 80, 15);
    16. getContentPane().add(lblNewLabel);
    17. JComboBox comboBox = new JComboBox(); // 创建一个下拉列表框
    18. comboBox.setBounds(110, 11, 80, 21); // 设置坐标
    19. comboBox.addItem("身份证"); // 为下拉列表中添加项
    20. comboBox.addItem("军人证");
    21. comboBox.addItem("学生证");
    22. comboBox.addItem("工作证");
    23. comboBox.setEditable(true);
    24. getContentPane().add(comboBox); // 将下拉列表添加到容器中
    25. JLabel lblResult = new JLabel("");
    26. lblResult.setBounds(0, 57, 146, 15);
    27. getContentPane().add(lblResult);
    28. JButton btnNewButton = new JButton("确定");
    29. btnNewButton.setBounds(200, 10, 67, 23);
    30. getContentPane().add(btnNewButton);
    31. btnNewButton.addActionListener(new ActionListener() { // 为按钮添加监听事件
    32. @Override
    33. public void actionPerformed(ActionEvent arg0) {
    34. // 获取下拉列表中的选中项
    35. lblResult.setText("您选择的是:" + comboBox.getSelectedItem());
    36. }
    37. });
    38. }
    39. public static void main(String[] args) {
    40. JComboBoxTest frame = new JComboBoxTest(); // 创建窗体对象
    41. frame.setVisible(true); // 使窗体可见
    42. }
    43. }//例题18.18

    18.7.2:JList列表框

    1. package eightth;
    2. import java.awt.Container;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. public class JListTest extends JFrame {
    6. public JListTest() {
    7. Container cp = getContentPane(); // 获取窗体主容器
    8. cp.setLayout(null); // 容器使用绝对布局
    9. // 创建字符串数组,保存列表的中的数据
    10. String[] contents = { "列表1", "列表2", "列表3", "列表4", "列表5", "列表6" };
    11. JList jl = new JList<>(contents); // 创建列表,并将数据作为构造参数
    12. JScrollPane js = new JScrollPane(jl); // 将列表放入滚动面板
    13. js.setBounds(10, 10, 100, 109); // 设定滚动面板的坐标和大小
    14. cp.add(js);
    15. JTextArea area = new JTextArea(); // 创建文本域
    16. JScrollPane scrollPane = new JScrollPane(area); // 将文本域放入滚动面板
    17. scrollPane.setBounds(118, 10, 73, 80); // 设定滚动面板的坐标和大小
    18. cp.add(scrollPane);
    19. JButton btnNewButton = new JButton("确认"); // 创建确认按钮
    20. btnNewButton.setBounds(120, 96, 71, 23); // 设定按钮的坐标和大小
    21. cp.add(btnNewButton);
    22. btnNewButton.addActionListener(new ActionListener() { // 添加按钮事件
    23. public void actionPerformed(ActionEvent e) {
    24. // 获取列表中选中的元素,返回java.util.List类型
    25. java.util.List values = jl.getSelectedValuesList();
    26. area.setText(""); // 清空文本域
    27. for (String value : values) {
    28. area.append(value + "\n"); // 在文本域循环追加List中的元素值
    29. }
    30. }
    31. });
    32. setTitle("在这个窗体中使用了列表框");
    33. setSize(217, 167);
    34. setVisible(true);
    35. setDefaultCloseOperation(EXIT_ON_CLOSE);
    36. }
    37. public static void main(String args[]) {
    38. new JListTest();
    39. }
    40. }
    41. //例题18.19

    18.8:文本组件

    18.8.1:JTextField文本框

    1. package eightth;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. public class JTextFieldTest extends JFrame {
    6. public JTextFieldTest() {
    7. Container c = getContentPane(); // 获取窗体主容器
    8. c.setLayout(new FlowLayout());
    9. JTextField jt = new JTextField("请点击清除按钮"); // 设定文本框初始值
    10. jt.setColumns(20); // 设置文本框长度
    11. jt.setFont(new Font("宋体", Font.PLAIN, 20)); // 设置字体
    12. JButton jb = new JButton("清除");
    13. jt.addActionListener(new ActionListener() { // 为文本框添加回车事件
    14. public void actionPerformed(ActionEvent arg0) {
    15. jt.setText("触发事件"); // 设置文本框中的值
    16. }
    17. });
    18. jb.addActionListener(new ActionListener() { // 为按钮添加事件
    19. public void actionPerformed(ActionEvent arg0) {
    20. System.out.println(jt.getText()); // 输出当前文本框的值
    21. jt.setText(""); // 将文本框置空
    22. jt.requestFocus(); // 焦点回到文本框
    23. }
    24. });
    25. c.add(jt); // 窗体容器添加文本框
    26. c.add(jb); // 窗体添加按钮
    27. setBounds(100, 100, 250, 110);
    28. setVisible(true);
    29. setDefaultCloseOperation(EXIT_ON_CLOSE);
    30. }
    31. public static void main(String[] args) {
    32. new JTextFieldTest();
    33. }
    34. }
    35. //例题18.20

    在窗体输入后控制台会输出内容

    18.8.2:JPasswordField密码框

    密码框组件由JPasswordField对象表示,其作用是吧用户输入的字符串以某种符号进行加密。

    1. package eightth;
    2. import javax.swing.JFrame;
    3. import javax.swing.JPanel;
    4. import javax.swing.border.EmptyBorder;
    5. import javax.swing.JLabel;
    6. import javax.swing.JOptionPane;
    7. import java.awt.Font;
    8. import javax.swing.JTextField;
    9. import javax.swing.SwingConstants;
    10. import javax.swing.JButton;
    11. import javax.swing.JPasswordField;
    12. import java.awt.event.ActionEvent;
    13. import java.awt.event.ActionListener;
    14. import javax.swing.ImageIcon;
    15. import java.awt.Color;
    16. public class mima extends JFrame { // 创建一个“登录”类,并继承JFrame
    17. // 声明窗体中的组件
    18. private JPanel contentPane;
    19. private JPasswordField passwordField;
    20. private JLabel lblBanner;
    21. //主方法
    22. public static void main(String[] args) {
    23. mima frame = new mima(); // 创建密码对象
    24. frame.setVisible(true); // 使frame可视
    25. }
    26. //创建JFrame窗体
    27. public mima() { // Login的构造方法
    28. setResizable(false); // 不可改变窗体大小
    29. setTitle("密码框"); // 设置窗体题目
    30. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭的方式
    31. setBounds(80, 80, 406, 289); // 设置窗体大小
    32. //创建JPanel面板contentPane置于JFrame窗体中,并设置面板的边距和布局
    33. contentPane = new JPanel();
    34. contentPane.setBackground(Color.WHITE);
    35. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//边界
    36. setContentPane(contentPane);
    37. contentPane.setLayout(null);
    38. // 创建标签lblPwd置于面板contentPane中,设置标签的大小以及标签中字体的内容、样式
    39. JLabel lblPwd = new JLabel("密码:");
    40. lblPwd.setHorizontalAlignment(SwingConstants.RIGHT);
    41. lblPwd.setFont(new Font("幼圆", Font.PLAIN, 16));
    42. lblPwd.setBounds(125, 175, 54, 15);
    43. contentPane.add(lblPwd);
    44. //创建密码框置于面板contentPane中,设置密码框的大小
    45. passwordField = new JPasswordField();
    46. passwordField.setBounds(180, 172, 156, 21);
    47. contentPane.add(passwordField);
    48. }
    49. }

    18.8.3:JTextArea文本域

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class JTextAreaTest extends JFrame {
    5. public JTextAreaTest() {
    6. setSize(200, 100);
    7. setTitle("定义自动换行的文本域");
    8. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    9. Container cp = getContentPane(); // 获取窗体主容器
    10. // 创建一个文本内容为“文本域”、行高和列宽均为6的文本域
    11. JTextArea jt = new JTextArea("文本域", 6, 6);
    12. jt.setLineWrap(true); // 可以自动换行
    13. cp.add(jt);
    14. setVisible(true);
    15. }
    16. public static void main(String[] args) {
    17. new JTextAreaTest();
    18. }
    19. }
    20. //例题18.21

     18.9:表格组件

    18.9.1:创建表格

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. public class JTableDemo extends JFrame {
    5. public static void main(String args[]) {
    6. JTableDemo frame = new JTableDemo();
    7. frame.setVisible(true);
    8. }
    9. public JTableDemo() {
    10. setTitle("创建可以滚动的表格");
    11. setBounds(100, 100, 240, 150);
    12. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    13. String[] columnNames = {"A", "B"}; // 定义表格列名数组
    14. // 定义表格数据数组
    15. String[][] tableValues = {{"A1", "B1"}, {"A2", "B2"}, {"A3", "B3"},
    16. {"A4", "B4"}, {"A5", "B5"}};
    17. // 创建指定列名和数据的表格
    18. JTable table = new JTable(tableValues, columnNames);
    19. // 创建显示表格的滚动面板
    20. JScrollPane scrollPane = new JScrollPane(table);
    21. // 将滚动面板添加到边界布局的中间
    22. getContentPane().add(scrollPane, BorderLayout.CENTER);
    23. }
    24. }
    25. //例题18.22

     

    18.9.2:DefaultTableModel表格数据模型

    1. package eightth;
    2. import java.awt.*;
    3. import javax.swing.*;
    4. import javax.swing.table.*;
    5. public class SortingTable extends JFrame {
    6. private static final long serialVersionUID = 1L;
    7. public static void main(String args[]) {
    8. SortingTable frame = new SortingTable();
    9. frame.setVisible(true);
    10. }
    11. public SortingTable() {
    12. setTitle("表格模型与表格");
    13. setBounds(100, 100, 240, 150);
    14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    15. JScrollPane scrollPane = new JScrollPane();
    16. getContentPane().add(scrollPane, BorderLayout.CENTER);
    17. String[] columnNames = {"A", "B"}; // 定义表格列名数组
    18. // 定义表格数据数组
    19. String[][] tableValues = {{"A1", "B1"}, {"A2", "B2"}, {"A3", "B3"}};
    20. // 创建指定表格列名和表格数据的表格模型
    21. DefaultTableModel tableModel = new DefaultTableModel(tableValues, columnNames);
    22. JTable table = new JTable(tableModel); // 创建指定表格模型的表格
    23. table.setRowSorter(new TableRowSorter<>(tableModel));
    24. scrollPane.setViewportView(table);
    25. }
    26. }
    27. //例题18.23

    可以修改

    18.9.3:维护表格模型

    1. package eightth;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. import javax.swing.table.*;
    6. public class AddAndDeleteDemo extends JFrame {
    7. private DefaultTableModel tableModel; // 定义表格模型对象
    8. private JTable table; // 定义表格对象
    9. private JTextField aTextField;
    10. private JTextField bTextField;
    11. public static void main(String args[]) {
    12. AddAndDeleteDemo frame = new AddAndDeleteDemo();
    13. frame.setVisible(true);
    14. }
    15. public AddAndDeleteDemo() {
    16. setTitle("维护表格模型");
    17. setBounds(100, 100, 530, 200);
    18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    19. final JScrollPane scrollPane = new JScrollPane();
    20. getContentPane().add(scrollPane, BorderLayout.CENTER);
    21. String[] columnNames = {"A", "B"}; // 定义表格列名数组
    22. // 定义表格数据数组
    23. String[][] tableValues = {{"A1", "B1"}, {"A2", "B2"}, {"A3", "B3"}};
    24. // 创建指定表格列名和表格数据的表格模型
    25. tableModel = new DefaultTableModel(tableValues, columnNames);
    26. table = new JTable(tableModel); // 创建指定表格模型的表格
    27. table.setRowSorter(new TableRowSorter<>(tableModel)); // 设置表格的排序器
    28. // 设置表格的选择模式为单选
    29. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    30. // 为表格添加鼠标事件监听器
    31. table.addMouseListener(new MouseAdapter() {
    32. public void mouseClicked(MouseEvent e) { // 发生了单击事件
    33. int selectedRow = table.getSelectedRow(); // 获得被选中行的索引
    34. // 从表格模型中获得指定单元格的值
    35. Object oa = tableModel.getValueAt(selectedRow, 0);
    36. // 从表格模型中获得指定单元格的值
    37. Object ob = tableModel.getValueAt(selectedRow, 1);
    38. aTextField.setText(oa.toString()); // 将值赋值给文本框
    39. bTextField.setText(ob.toString()); // 将值赋值给文本框
    40. }
    41. });
    42. scrollPane.setViewportView(table);
    43. JPanel panel = new JPanel();
    44. getContentPane().add(panel, BorderLayout.SOUTH);
    45. panel.add(new JLabel("A:"));
    46. aTextField = new JTextField("A4", 10);
    47. panel.add(aTextField);
    48. panel.add(new JLabel("B:"));
    49. bTextField = new JTextField("B4", 10);
    50. panel.add(bTextField);
    51. JButton addButton = new JButton("添加");
    52. addButton.addActionListener(new ActionListener() {
    53. public void actionPerformed(ActionEvent e) {
    54. String[] rowValues = {aTextField.getText(),
    55. bTextField.getText()}; // 创建表格行数组
    56. tableModel.addRow(rowValues); // 向表格模型中添加一行
    57. int rowCount = table.getRowCount() + 1;
    58. aTextField.setText("A" + rowCount);
    59. bTextField.setText("B" + rowCount);
    60. }
    61. });
    62. panel.add(addButton);
    63. JButton updButton = new JButton("修改");
    64. updButton.addActionListener(new ActionListener() {
    65. public void actionPerformed(ActionEvent e) {
    66. int selectedRow = table.getSelectedRow(); // 获得被选中行的索引
    67. if (selectedRow != -1) { // 判断是否存在被选中行
    68. // 修改表格模型当中的指定值
    69. tableModel.setValueAt(aTextField.getText(), selectedRow, 0);
    70. // 修改表格模型当中的指定值
    71. tableModel.setValueAt(bTextField.getText(), selectedRow, 1);
    72. }
    73. }
    74. });
    75. panel.add(updButton);
    76. JButton delButton = new JButton("删除");
    77. delButton.addActionListener(new ActionListener() {
    78. public void actionPerformed(ActionEvent e) {
    79. int selectedRow = table.getSelectedRow(); // 获得被选中行的索引
    80. if (selectedRow != -1) // 判断是否存在被选中行
    81. tableModel.removeRow(selectedRow); // 从表格模型当中删除指定行
    82. }
    83. });
    84. panel.add(delButton);
    85. }
    86. }
    87. //例题18.24

    添加

    修改

    删除

    18.10:时间监听器

    18.10.1:ActionEvent动作事件

    1. package eightth;
    2. import java.awt.Container;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. import javax.swing.JButton;
    6. import javax.swing.JFrame;
    7. import javax.swing.WindowConstants;
    8. public class SimpleEvent extends JFrame {
    9. private JButton jb = new JButton("我是按钮,点击我");
    10. public SimpleEvent() {
    11. setLayout(null);
    12. setSize(200, 100);
    13. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    14. Container cp = getContentPane();
    15. cp.add(jb);
    16. jb.setBounds(10, 10, 150, 30);
    17. jb.addActionListener(new jbAction());
    18. setVisible(true);
    19. }
    20. class jbAction implements ActionListener {
    21. public void actionPerformed(ActionEvent arg0) {
    22. jb.setText("我被点击了");
    23. }
    24. }
    25. public static void main(String[] args) {
    26. new SimpleEvent();
    27. }
    28. }
    29. //例题18.25

    18.10.2:KeyEvent键盘事件

    1. package eightth;
    2. import java.awt.BorderLayout;
    3. import java.awt.EventQueue;
    4. import javax.swing.JFrame;
    5. import javax.swing.JPanel;
    6. import javax.swing.border.EmptyBorder;
    7. import java.awt.Color;
    8. import java.awt.Component;
    9. import javax.swing.JButton;
    10. import java.awt.Font;
    11. import javax.swing.SwingConstants;
    12. import javax.swing.border.TitledBorder;
    13. import java.awt.event.KeyAdapter;
    14. import java.awt.event.KeyEvent;
    15. import java.util.ArrayList;
    16. import javax.swing.JTextField;
    17. // 虚拟键盘(键盘的按下与释放)
    18. public class KeyBoard extends JFrame { // 创建“键盘”类继承JFrame
    19. // 声明窗体中的成员组件
    20. private JPanel contentPane;
    21. private JTextField textField;
    22. private JButton btnQ;
    23. private JButton btnW;
    24. private JButton btnE;
    25. private JButton btnR;
    26. private JButton btnT;
    27. private JButton btnY;
    28. private JButton btnU;
    29. private JButton btnI;
    30. private JButton btnO;
    31. private JButton btnP;
    32. private JButton btnA;
    33. private JButton btnS;
    34. private JButton btnD;
    35. private JButton btnF;
    36. private JButton btnG;
    37. private JButton btnH;
    38. private JButton btnJ;
    39. private JButton btnK;
    40. private JButton btnL;
    41. private JButton btnZ;
    42. private JButton btnX;
    43. private JButton btnC;
    44. private JButton btnV;
    45. private JButton btnB;
    46. private JButton btnN;
    47. private JButton btnM;
    48. Color green = Color.GREEN;// 定义Color对象,用来表示按下键的颜色
    49. Color white = Color.WHITE;// 定义Color对象,用来表示释放键的颜色
    50. ArrayList btns = new ArrayList();// 定义一个集合,用来存储所有的按键ID
    51. // 自定义一个方法,用来将容器中的所有JButton组件添加到集合中
    52. private void addButtons() {
    53. for (Component cmp : contentPane.getComponents()) {// 遍历面板中的所有组件
    54. if (cmp instanceof JButton) {// 判断组件的类型是否为JButton类型
    55. btns.add((JButton) cmp);// 将JButton组件添加到集合中
    56. }
    57. }
    58. }
    59. //主方法
    60. public static void main(String[] args) {
    61. EventQueue.invokeLater(new Runnable() { // 使得Runnable中的的run()方法在the system EventQueue的指派线程中被调用
    62. public void run() {
    63. try {
    64. KeyBoard frame = new KeyBoard(); // 创建KeyBoard对象
    65. frame.setVisible(true); // 使frame可视
    66. frame.addButtons();// 初始化存储所有按键的集合
    67. } catch (Exception e) {
    68. e.printStackTrace();
    69. }
    70. }
    71. });
    72. }
    73. // 创建JFrame窗体
    74. public KeyBoard() { // KeyBoard的构造方法
    75. setTitle("\u865A\u62DF\u952E\u76D8\uFF08\u6A21\u62DF\u952E\u76D8\u7684\u6309\u4E0B\u4E0E\u91CA\u653E\uFF09"); // 设置窗体题目
    76. setResizable(false); // 不可改变窗体宽高
    77. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭的方式
    78. setBounds(100, 100, 560, 280); // 设置窗体的位置和宽高
    79. //创建JPanel面板contentPane置于JFrame窗体中,并设置面板的背景色、边距和布局
    80. contentPane = new JPanel();
    81. contentPane.setBackground(Color.WHITE);
    82. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    83. setContentPane(contentPane);
    84. contentPane.setLayout(null);
    85. //创建按钮button置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    86. btnQ = new JButton("Q");
    87. btnQ.setBackground(white);
    88. btnQ.setVerticalAlignment(SwingConstants.TOP);
    89. btnQ.setHorizontalAlignment(SwingConstants.LEADING);
    90. btnQ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    91. btnQ.setBounds(0, 60, 47, 45);
    92. contentPane.add(btnQ);
    93. // 创建按钮button_2置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    94. btnW = new JButton("W");
    95. btnW.setBackground(white);
    96. btnW.setVerticalAlignment(SwingConstants.TOP);
    97. btnW.setHorizontalAlignment(SwingConstants.LEADING);
    98. btnW.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    99. btnW.setBounds(55, 60, 49, 45);
    100. contentPane.add(btnW);
    101. // 创建按钮button_3置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    102. btnE = new JButton("E");
    103. btnE.setBackground(white);
    104. btnE.setVerticalAlignment(SwingConstants.TOP);
    105. btnE.setHorizontalAlignment(SwingConstants.LEADING);
    106. btnE.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    107. btnE.setBounds(110, 60, 45, 45);
    108. contentPane.add(btnE);
    109. // 创建按钮button_4置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    110. btnR = new JButton("R");
    111. btnR.setBackground(white);
    112. btnR.setVerticalAlignment(SwingConstants.TOP);
    113. btnR.setHorizontalAlignment(SwingConstants.LEADING);
    114. btnR.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    115. btnR.setBounds(165, 60, 45, 45);
    116. contentPane.add(btnR);
    117. // 创建按钮button_5置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    118. btnF = new JButton("F");
    119. btnF.setBackground(white);
    120. btnF.setVerticalAlignment(SwingConstants.TOP);
    121. btnF.setHorizontalAlignment(SwingConstants.LEADING);
    122. btnF.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    123. btnF.setBounds(195, 125, 45, 45);
    124. contentPane.add(btnF);
    125. //创建按钮button_6置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
    126. btnD = new JButton("D");
    127. btnD.setBackground(white);
    128. btnD.setVerticalAlignment(SwingConstants.TOP);
    129. btnD.setHorizontalAlignment(SwingConstants.LEADING);
    130. btnD.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    131. btnD.setBounds(137, 125, 45, 45);
    132. contentPane.add(btnD);
    133. btnT = new JButton("T");
    134. btnT.setVerticalAlignment(SwingConstants.TOP);
    135. btnT.setHorizontalAlignment(SwingConstants.LEADING);
    136. btnT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    137. btnT.setBackground(white);
    138. btnT.setBounds(220, 60, 45, 45);
    139. contentPane.add(btnT);
    140. btnY = new JButton("Y");
    141. btnY.setVerticalAlignment(SwingConstants.TOP);
    142. btnY.setHorizontalAlignment(SwingConstants.LEADING);
    143. btnY.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    144. btnY.setBackground(white);
    145. btnY.setBounds(275, 60, 45, 45);
    146. contentPane.add(btnY);
    147. btnU = new JButton("U");
    148. btnU.setVerticalAlignment(SwingConstants.TOP);
    149. btnU.setHorizontalAlignment(SwingConstants.LEADING);
    150. btnU.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    151. btnU.setBackground(white);
    152. btnU.setBounds(330, 60, 45, 45);
    153. contentPane.add(btnU);
    154. btnI = new JButton("I");
    155. btnI.setVerticalAlignment(SwingConstants.TOP);
    156. btnI.setHorizontalAlignment(SwingConstants.LEADING);
    157. btnI.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    158. btnI.setBackground(white);
    159. btnI.setBounds(385, 60, 45, 45);
    160. contentPane.add(btnI);
    161. btnO = new JButton("O");
    162. btnO.setVerticalAlignment(SwingConstants.TOP);
    163. btnO.setHorizontalAlignment(SwingConstants.LEADING);
    164. btnO.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    165. btnO.setBackground(white);
    166. btnO.setBounds(440, 60, 46, 45);
    167. contentPane.add(btnO);
    168. btnP = new JButton("P");
    169. btnP.setVerticalAlignment(SwingConstants.TOP);
    170. btnP.setHorizontalAlignment(SwingConstants.LEADING);
    171. btnP.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    172. btnP.setBackground(white);
    173. btnP.setBounds(495, 60, 45, 45);
    174. contentPane.add(btnP);
    175. btnA = new JButton("A");
    176. btnA.setVerticalAlignment(SwingConstants.TOP);
    177. btnA.setHorizontalAlignment(SwingConstants.LEADING);
    178. btnA.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    179. btnA.setBackground(white);
    180. btnA.setBounds(23, 125, 45, 45);
    181. contentPane.add(btnA);
    182. btnS = new JButton("S");
    183. btnS.setVerticalAlignment(SwingConstants.TOP);
    184. btnS.setHorizontalAlignment(SwingConstants.LEADING);
    185. btnS.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    186. btnS.setBackground(white);
    187. btnS.setBounds(82, 125, 45, 45);
    188. contentPane.add(btnS);
    189. btnG = new JButton("G");
    190. btnG.setVerticalAlignment(SwingConstants.TOP);
    191. btnG.setHorizontalAlignment(SwingConstants.LEADING);
    192. btnG.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    193. btnG.setBackground(white);
    194. btnG.setBounds(251, 125, 45, 45);
    195. contentPane.add(btnG);
    196. btnH = new JButton("H");
    197. btnH.setVerticalAlignment(SwingConstants.TOP);
    198. btnH.setHorizontalAlignment(SwingConstants.LEADING);
    199. btnH.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    200. btnH.setBackground(white);
    201. btnH.setBounds(306, 125, 45, 45);
    202. contentPane.add(btnH);
    203. btnJ = new JButton("J");
    204. btnJ.setVerticalAlignment(SwingConstants.TOP);
    205. btnJ.setHorizontalAlignment(SwingConstants.LEADING);
    206. btnJ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    207. btnJ.setBackground(white);
    208. btnJ.setBounds(361, 125, 45, 45);
    209. contentPane.add(btnJ);
    210. btnK = new JButton("K");
    211. btnK.setVerticalAlignment(SwingConstants.TOP);
    212. btnK.setHorizontalAlignment(SwingConstants.LEADING);
    213. btnK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    214. btnK.setBackground(white);
    215. btnK.setBounds(416, 125, 47, 45);
    216. contentPane.add(btnK);
    217. btnL = new JButton("L");
    218. btnL.setVerticalAlignment(SwingConstants.TOP);
    219. btnL.setHorizontalAlignment(SwingConstants.LEADING);
    220. btnL.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    221. btnL.setBackground(white);
    222. btnL.setBounds(471, 125, 45, 45);
    223. contentPane.add(btnL);
    224. btnZ = new JButton("Z");
    225. btnZ.setVerticalAlignment(SwingConstants.TOP);
    226. btnZ.setHorizontalAlignment(SwingConstants.LEADING);
    227. btnZ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    228. btnZ.setBackground(white);
    229. btnZ.setBounds(39, 190, 45, 45);
    230. contentPane.add(btnZ);
    231. btnX = new JButton("X");
    232. btnX.setVerticalAlignment(SwingConstants.TOP);
    233. btnX.setHorizontalAlignment(SwingConstants.LEADING);
    234. btnX.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    235. btnX.setBackground(white);
    236. btnX.setBounds(107, 190, 45, 45);
    237. contentPane.add(btnX);
    238. btnC = new JButton("C");
    239. btnC.setVerticalAlignment(SwingConstants.TOP);
    240. btnC.setHorizontalAlignment(SwingConstants.LEADING);
    241. btnC.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    242. btnC.setBackground(white);
    243. btnC.setBounds(178, 190, 45, 45);
    244. contentPane.add(btnC);
    245. btnV = new JButton("V");
    246. btnV.setVerticalAlignment(SwingConstants.TOP);
    247. btnV.setHorizontalAlignment(SwingConstants.LEADING);
    248. btnV.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    249. btnV.setBackground(white);
    250. btnV.setBounds(250, 190, 45, 45);
    251. contentPane.add(btnV);
    252. btnB = new JButton("B");
    253. btnB.setVerticalAlignment(SwingConstants.TOP);
    254. btnB.setHorizontalAlignment(SwingConstants.LEADING);
    255. btnB.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    256. btnB.setBackground(white);
    257. btnB.setBounds(315, 190, 45, 45);
    258. contentPane.add(btnB);
    259. btnN = new JButton("N");
    260. btnN.setVerticalAlignment(SwingConstants.TOP);
    261. btnN.setHorizontalAlignment(SwingConstants.LEADING);
    262. btnN.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    263. btnN.setBackground(white);
    264. btnN.setBounds(382, 190, 47, 45);
    265. contentPane.add(btnN);
    266. btnM = new JButton("M");
    267. btnM.setVerticalAlignment(SwingConstants.TOP);
    268. btnM.setHorizontalAlignment(SwingConstants.LEADING);
    269. btnM.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    270. btnM.setBackground(white);
    271. btnM.setBounds(449, 190, 48, 45);
    272. contentPane.add(btnM);
    273. // 创建面板panel置于面板contentPane中,设置面板panel的位置、宽高、TitledBorder、背景色以及布局方式(边界布局)
    274. JPanel panel = new JPanel();
    275. panel.setBorder(new TitledBorder(null, "文本显示区", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    276. panel.setBackground(Color.WHITE);
    277. panel.setBounds(0, 0, 540, 45);
    278. contentPane.add(panel);
    279. panel.setLayout(new BorderLayout(0, 0));
    280. // 创建文本框textField置于面板panel的中间
    281. textField = new JTextField();
    282. textField.addKeyListener(new KeyAdapter() { // 文本框添加键盘事件的监听
    283. char word;
    284. @Override
    285. public void keyPressed(KeyEvent e) { // 按键被按下时被触发
    286. word = e.getKeyChar();// 获取按下键表示的字符
    287. for (int i = 0; i < btns.size(); i++) {// 遍历存储按键ID的ArrayList集合
    288. // 判断按键是否与遍历到的按键的文本相同
    289. if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {
    290. btns.get(i).setBackground(green);// 将指定按键颜色设置为绿色
    291. }
    292. }
    293. }
    294. @Override
    295. public void keyReleased(KeyEvent e) { // 按键被释放时被触发
    296. word = e.getKeyChar();// 获取释放键表示的字符
    297. for (int i = 0; i < btns.size(); i++) {// 遍历存储按键ID的ArrayList集合
    298. // 判断按键是否与遍历到的按键的文本相同
    299. if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {
    300. btns.get(i).setBackground(white);// 将指定按键颜色设置为白色
    301. }
    302. }
    303. }
    304. });
    305. panel.add(textField, BorderLayout.CENTER);
    306. textField.setColumns(10);
    307. }
    308. }
    309. //例题18.26

     

    18.10.3:MouseEvent鼠标事件

    1. package eightth;
    2. import java.awt.BorderLayout;
    3. import java.awt.event.MouseEvent;
    4. import java.awt.event.MouseListener;
    5. import javax.swing.JFrame;
    6. import javax.swing.JLabel;
    7. public class MouseEventDemo extends JFrame { // 继承窗体类JFrame
    8. public static void main(String args[]) {
    9. MouseEventDemo frame = new MouseEventDemo();
    10. frame.setVisible(true); // 设置窗体可见,默认为不可见
    11. }
    12. // 判断按下的鼠标键,并输出相应提示 @param e 鼠标事件
    13. private void mouseOper(MouseEvent e) {
    14. int i = e.getButton(); // 通过该值可以判断按下的是哪个键
    15. if (i == MouseEvent.BUTTON1)
    16. System.out.println("按下的是鼠标左键");
    17. else if (i == MouseEvent.BUTTON2)
    18. System.out.println("按下的是鼠标滚轮");
    19. else if (i == MouseEvent.BUTTON3)
    20. System.out.println("按下的是鼠标右键");
    21. }
    22. public MouseEventDemo() {
    23. super(); // 继承父类的构造方法
    24. setTitle("鼠标事件示例"); // 设置窗体的标题
    25. setBounds(100, 100, 500, 375); // 设置窗体的显示位置及大小
    26. // 设置窗体关闭按钮的动作为退出
    27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    28. final JLabel label = new JLabel();
    29. label.addMouseListener(new MouseListener() {
    30. public void mouseEntered(MouseEvent e) {// 光标移入组件时被触发
    31. System.out.println("光标移入组件");
    32. }
    33. public void mousePressed(MouseEvent e) {// 鼠标按键被按下时被触发
    34. System.out.print("鼠标按键被按下,");
    35. mouseOper(e);
    36. }
    37. public void mouseReleased(MouseEvent e) {// 鼠标按键被释放时被触发
    38. System.out.print("鼠标按键被释放,");
    39. mouseOper(e);
    40. }
    41. public void mouseClicked(MouseEvent e) {// 发生单击事件时被触发
    42. System.out.print("单击了鼠标按键,");
    43. mouseOper(e);
    44. int clickCount = e.getClickCount();// 获取鼠标单击次数
    45. System.out.println("单击次数为" + clickCount + "下");
    46. }
    47. public void mouseExited(MouseEvent e) {// 光标移出组件时被触发
    48. System.out.println("光标移出组件");
    49. }
    50. });
    51. getContentPane().add(label, BorderLayout.CENTER);
    52. //
    53. }
    54. }
    55. //例题18.27

     

  • 相关阅读:
    常用md语法
    DNS压测工具-dnsperf的安装和使用(centos)
    Java函数式编程:三、流与函数式编程
    暑期JAVA学习(38.2)线程的生命周期
    怎么将客户引到私域?
    CDH启用kerberos 高可用运维实战
    Linux下监测网卡状态:exist、down、up link、up unplugg
    Audio2Face的工作原理
    【kubernetes】kubernetes中的Controller
    通过IP地理位置阻止网络攻击
  • 原文地址:https://blog.csdn.net/m0_73576495/article/details/134270358