• 团队冲刺DAY5


    团队冲刺DAY5

    今天的内容是组件和事件处理这一部分,也就是需要扣一个消息系统的图形界面。
    提到这部分,就不得不说Java Swing。

    常用组件及部件

    JTextField:文本框
    JTextArea:文本区
    JButton:按钮
    JLable:标签
    JCheckBox:复选框
    JRadioButton:单选框
    JComboBox:下拉列表
    JPasswordFiled:密码框

    常用布局

    setLayout(布局对象);

    处理事件

    事件源:
    能够产生事件的对象都可以称为事件源,文本框,按钮,下拉框。也就是说,事件源必须是一个对象。而且这个对象必须是java认为可以发生事件的对象

    监听器:
    需要一个对象对事件源进行监视,以便发生的事件做出处理,事件源通过调用相应的方法,将某个对象注册为自己的监听器,例如文本框,这个方法addActionListener(监听器);

    利用组合

    可以让一个对象来操作另一个对象,即当前对象可以委托它所组合的一个对象调用方法产生行为。

    Receive receive = new Receive(client);
    WindowActionEvent win = new WindowActionEvent();
    PoliceListen police = new PoliceListen();
    

    我们在书上发现一个不错的界面,就是例子7,打算输入框为服务器和客户端的输入框,下面的文本为服务器和客户端的对话记录。
    PoliceListen.java这个是监听类。

    import java.awt.event.*;
    import javax.swing.*;
    public class PoliceListen implements MyCommandListener {
       JTextField textInput;
       JTextArea textShow;
       Socket mysocket;
       public void setJTextField(JTextField text) {
          textInput = text;
       }
       public void setJTextArea(JTextArea area) {
          textShow = area;
       } 
       public void actionPerformed(ActionEvent e) {
          String str=textInput.getText();
          textShow.append("客户端:"+str);
          }
          catch(Exception e){
          }
       }
    }
    
    

    MyCommandListener.java这个是接口类。

    import javax.swing.*;
    import java.awt.event.*;
    interface MyCommandListener extends ActionListener {
         public void setJTextField(JTextField text);
         public void setJTextArea(JTextArea area); 
         
    }
    

    WindowActionEvent.java是主体部分

    import java.awt.*;
    import javax.swing.*;
    public class WindowActionEvent extends JFrame { 
       JTextField inputText;
       JTextArea textShow;
       JButton button;
       MyCommandListener listener;
       public WindowActionEvent() {
          init();
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
       void init() {
          setLayout(new FlowLayout());
          inputText = new JTextField(10); 
          button = new JButton("确定");
          textShow = new JTextArea(9,30);
          add(inputText);
          add(button);
          add(new JScrollPane(textShow));
       }
       void setMyCommandListener(MyCommandListener listener) {
          this.listener = listener;
          listener.setJTextField(inputText);
          listener.setJTextArea(textShow);
          inputText.addActionListener(listener);  //inputText是事件源,listener是监视器
          button.addActionListener(listener);     //button是事件源,listener是监视器
       }
    }
    

    Example9_7测试类

    public class Example9_7 {
       public static void main(String args[]) {
          WindowActionEvent win = new WindowActionEvent();
          PoliceListen police = new PoliceListen();
          win.setMyCommandListener(police);
          win.setBounds(100,100,460,360);
          win.setTitle("处理ActionEvent事件");
       }
    }
    
    
  • 相关阅读:
    MySql多表循环遍历更新
    GridView控件的选择功能,代码实现CheckBox控件的全选、反选以及取消
    使用HTTP POST请求12306网站接口查询火车车次API
    GridView控件的绑定分页功能
    使用HTTP GET请求12306网站接口获取车站名和车站Code
    浅谈从Oracle数据库中取出10条数据的Select语句与Sql Server、MySql的区别
    2022 程序员口语提升指南
    R语言与java整合
    新浪的股票接口 c# (收藏)
    摘记
  • 原文地址:https://www.cnblogs.com/gjy2019/p/10964423.html
Copyright © 2020-2023  润新知