Team sprint DAY5

Team sprint DAY5

Today's content is the component and event handling of this section, that is, should withhold a messaging system graphical interface.
Mention this part, I have to say that Java Swing.

Common components and parts

JTextField: text box
JTextArea: text area
JButton: Button
JLable: Tags
JCheckBox: checkbox
JRadioButton: radio button
JComboBox: drop-down list
JPasswordFiled: Password box

Common layouts

setLayout (layout object);

Handling Events

Event Source:
objects can produce events can be called an event source, text boxes, buttons, drop-down box. In other words, the event source must be an object. And this object must be considered java object events can occur

Listener:
you need an object to be monitored event source, in order to make a deal with the incident, event source by calling the appropriate method, to register an object for his listeners, such as text boxes, this method addActionListener (listeners) ;

Use a combination of

Allows an object to manipulate another object, that object is the current object can be entrusted to a combination of the methods it calls generated behavior.

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

We found a nice interface in the book, is an example 7, intends to enter the dialog box to record server and client input box, the following text to the server and the client.
PoliceListen.java This is the listener class.

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 This is the interface class.

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

WindowActionEvent.java is the main part

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 test class

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事件");
   }
}

Guess you like

Origin www.cnblogs.com/gjy2019/p/10964423.html