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

Java 第十一章 GUI编程(2)

目录

GUI

事件处理

基本思路

添加事件监听器

对话框

实例


GUI


事件处理

  • 对于采用了图形用户界面的程序来说,事件控制是非常重要的;
  • 到目前为止, 我们编写的图形用户界面程序都仅仅只是完成了界面,而没有任何实际的功能, 要实现相应的功能,必须进行事件处理;
  •  用户与GUI组件进行交互就会发生事件,如:按下一个按钮、用键盘输入一个字 符、点击鼠标等等;
  •  当前我们要关注的并不是“事件是如何产生的”,而是讨论当发生事件后,我 们应当“如何处理”。

基本思路

 Java中,事件处理的基本思路如下:

● 一个事件源产生一个事件并把它送到监听器那里,监听器一直等待,直 到它收到一个事件,一旦事件被接受,监听器将处理这些事件;

 

  • 由于我们想要处理按钮的点击事件,因此,按钮便是事件源;
  • 监听器类型是ActionListener。

添加事件监听器

形式:

按钮对象.addActionListener(new ActionListener() {

// 事件处理

@Override

public void actionPerformed(ActionEvent e) {

执行操作

}

});

//按钮的事件处理程序 new + 接口,是Java中一种简化的写法,创建了一个接口的匿名内部类对象
        //登录按钮
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                  String account = textField1.getText();//获得文本框账户值
                  String password = textField2.getText();//获得密码框内容
                  try {
                  if (account.length() == 0) {
                      JOptionPane.showMessageDialog(null,"账户不能为空");
                      return;
                  }
                  if (password.length() == 0) {
                      JOptionPane.showMessageDialog(null,"密码不能为空");
                      return;
                  }
                  //预留数据库对接
                  //连接服务器Socket
                  //打开聊天窗口
                      new ChatFrame();
                      dispose();//释放关闭聊天窗口

              }catch (Exception ex){
                  ex.printStackTrace();
                  JOptionPane.showMessageDialog(null, "系统忙","消息",JOptionPane.WARNING_MESSAGE);
              }
            }
        });

对话框

JOptionPane对话框

showMessageDialog():消息对话框

主要有五种消息类型,类型不同,图标不同:

  • ERROR_MESSAGE            //错误消息提示
  • INFORMATION_MESSAGE //信息提示
  • WARNING_MESSAGE         // 警告提示
  • QUESTION_MESSAGE        //问题提示
  • PLAIN_MESSAGE                //简洁提示

showConfirmDialog():确认对话框

主要有四种消息类型,类型不同,图标不同:

  • DEFAULT_OPTION             //默认选项
  • YES_NO_OPTION                //是/否选项
  • YES_NO_CANCEL_OPTION  //是/否/取消选项
  • OK_CANCEL_OPTION             //确定/取消

实例

1.完成十进制整数转其他进制数的小工具

public class numFrame extends JFrame {
    public numFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label5 = new JLabel();
        two = new JLabel();
        eight = new JLabel();
        ten = new JLabel();
        label9 = new JLabel();
        sixteen = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        surebutton = new JButton();
        button = new JButton();
        label10 = new JLabel();

        //======== this ========
        setTitle("\u8fdb\u5236\u8f6c\u6362\u5668");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(label5);
        label5.setBounds(55, 240, 25, label5.getPreferredSize().height);

        //---- two ----
        two.setText("\u4e8c\u8fdb\u5236");
        contentPane.add(two);
        two.setBounds(new Rectangle(new Point(50, 140), two.getPreferredSize()));

        //---- eight ----
        eight.setText("\u516b\u8fdb\u5236");
        contentPane.add(eight);
        eight.setBounds(new Rectangle(new Point(50, 180), eight.getPreferredSize()));

        //---- ten ----
        ten.setText("\u5341\u8fdb\u5236");
        contentPane.add(ten);
        ten.setBounds(new Rectangle(new Point(50, 75), ten.getPreferredSize()));
        contentPane.add(label9);
        label9.setBounds(new Rectangle(new Point(50, 235), label9.getPreferredSize()));

        //---- sixteen ----
        sixteen.setText("\u5341\u516d\u8fdb\u5236");
        contentPane.add(sixteen);
        sixteen.setBounds(new Rectangle(new Point(50, 230), sixteen.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(120, 130, 150, textField1.getPreferredSize().height);
        contentPane.add(textField2);
        textField2.setBounds(120, 175, 150, textField2.getPreferredSize().height);
        contentPane.add(textField3);
        textField3.setBounds(120, 70, 150, textField3.getPreferredSize().height);
        contentPane.add(textField4);
        textField4.setBounds(120, 230, 150, textField4.getPreferredSize().height);

        //---- surebutton ----
        surebutton.setText("\u8f6c\u6362");
        contentPane.add(surebutton);
        surebutton.setBounds(new Rectangle(new Point(80, 350), surebutton.getPreferredSize()));

        //---- button ----
        button.setText("\u8fd4\u56de");
        contentPane.add(button);
        button.setBounds(new Rectangle(new Point(390, 350), button.getPreferredSize()));

        //---- label10 ----
        label10.setText("\u8bf7\u8f93\u5165\u5341\u8fdb\u5236\u6570\uff1a");
        contentPane.add(label10);
        label10.setBounds(20, 25, 175, label10.getPreferredSize().height);

        contentPane.setPreferredSize(new Dimension(580, 675));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //转换进制
        surebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int num = 0;
                try {
                  num = new Integer(textField3.getText());
                }catch (NumberFormatException n){
                    n.printStackTrace();
                    JOptionPane.showMessageDialog(null, "不是有效数字");
                }
                try{
                    textField1.setText(Integer.toBinaryString(num));
                    textField2.setText(Integer.toOctalString(num));
                    textField4.setText(Integer.toHexString(num));
                }catch (Exception ex){
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "系统忙,请稍后再试");
                }
            }
        });
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label5;
    private JLabel two;
    private JLabel eight;
    private JLabel ten;
    private JLabel label9;
    private JLabel sixteen;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton surebutton;
    private JButton button;
    private JLabel label10;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    
	//打开进制转换器
	
	public static void main(String[] args) {
        new numFrame();
    }
}

 

 

 

 

 

 

 

 

相关文章:

  • C与C++中inline关键字的深入解析与使用指南
  • 景联文科技:以专业标注赋能AI未来,驱动智能时代的精准跃迁
  • Java 大视界 -- Java 大数据在智能金融反欺诈中的技术实现与案例分析(114)
  • 靶场(二)---靶场心得小白分享
  • Stable Diffusion模型高清算法模型类详解
  • ifconfig 不显示 Linux 虚拟机常规网卡的 IP 地址
  • # Kouplele (5)
  • docer swarm集群部署springboot项目
  • 详细分析KeepAlive的基本知识 并缓存路由(附Demo)
  • 使用easyocr、PyPDF2对图像及PDF文档进行识别
  • 探秘 Linux 系统编程:进程地址空间的奇妙世界
  • 基于EM期望最大化算法的GMM参数估计与三维数据分类系统python源码
  • SP导入模型设置
  • 解决跨域请求的问题(CORS)
  • iOS UIGestureRecgonizer自动化点击埋点
  • XPath路径表达式
  • 文件上传漏洞详细利用流程
  • BambuStudio学习笔记:format格式化输出
  • Linux中是如何进行日志管理的?
  • C语言--简单排序算法(冒泡、选择、插入)
  • 广州市酒店网站设计/智能建站
  • 上海电子网站建设/网络整合营销4i原则是指
  • wordpress 密码重置/常用seo站长工具
  • 合肥市做网站的公司有哪些/怎么在线上推广自己的产品
  • wordpress做教育网站/seo技术培训教程视频
  • wordpress 恢复备份/搜索引擎排名优化公司