组建及事件处理 例题2

public class exam {

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

}
import javax.swing.*;
import java.awt.event.*;
public abstract class MyCommandListener extends ActionListener { //子接口多给出2各方法
        public void setJTextField(JTextField text);
        public void setJTextArea(JTextArea area);
		
}
import java.awt.*;
import javax.swing.*;
public class PoliceListen implements MyCommandListener { //负责创建监视器的类
	JTextField textInput;
	JTextArea textShow;
	public void setJTextField(JTextField text){
		textInput=text;
	}
	public void setJTextArea(JTextArea area){
		textShow=area;
	}
	public void actionPerformed(ActiveEvent e){
		String str= textInput.getText();
		textShow.append(str+"的长度:"+str.length()+"\n");
	}
}
import java.awt.*;

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

简单计算器

public class exam {

	public static void main(String[] args) {
           WindowOperation win=new WindowOperation();
           win.setBounds(100,100,390,360);
           win.setTitle("简单计算器");
	}
}
import java.awt.*;
import javax.swing.*;

import java.io.*;
public class WindowOperation extends JFrame {
   JTextField inputNumberOne,inputNumberTwo;
   JComboBox choiceFuhao;
   JTextArea textShow;
   JButton button;
   OperatorListener operator;  //监视ItemEvent事件的监视器
   ComputerListener computer;  //监视ActionEvent事件的监视器
   public void WindowOperation(){
	   init();
	   setVisible(true);
	   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   void init(){
	   setLayout(new FlowLayout());
	   inputNumberOne=new JTextField(5);
	   inputNumberTwo=new JTextField(5);
	   choiceFuhao=new JComboBox();
	   button=new JButton("计算");
	   choiceFuhao.addItem("选择运算符号");
	   String [] a={"+","-","*","/"};
	   for(int i=0;i<a.length;i++){
		   choiceFuhao.addItem(a[i]);
	   }
	   textShow=new JTextArea(9,30);
	   operator=new OperatorListener();
	   computer=new ComputerListener();
	   operator.setJComboBox(choiceFuhao);
	   operator.workTogether(computer);
	   computer.setJTextFieldOne(inputNumberOne);
	   computer.setJTextFieldTwo(inputNumberTwo);
	   computer.setJTextArea(textShow);
	   choiceFuhao.addItemListener(operator);
	   //choiceFuhao是事件源,operator是监视器
	   button.addActionListener(computer);
	  // button是事件源,computer是监视器
	   add(inputNumberOne);
	   add(choiceFuhao);
	   add(inputNumberTwo);
	   add(button);
	   add(new JScrollPane(textShow));
	   
   }
}
import java.awt.event.*;
import javax.swing.*;
public class OperatorListener implements ItemListener{
  JComboBox choice;
  ComputerListener  workTogether;
  public void  setJComboBox(JComboBox box) {
	choice=box;
}
  public void workTogether(ComputerListener computer) {
	workTogether=computer;
}
  public void itemStateChanged(ItemEvent e) {
	String fuhao=choice.getSelectedItem().toString();
	workTogether.setFuhao(fuhao);
}
}
import java.awt.event.*;
import javax.swing.*;
public class ComputerListener implements ActionListener {
  JTextField inputNumberOne,inputNumberTwo;
  JTextArea textShow;
  String fuhao;
  public void setJTextFieldOne(JTextField t) {
	inputNumberOne=t;
}
  public void setJTextFieldTwo(JTextField t) {
	inputNumberTwo=t;
}
  public void  setJTextArea(JTextArea area) {
	textShow=area;
}
  public void setFuhao(String s) {
	fuhao=s;
}
  public void actionPerformed(ActionEvent e) {
	try {
		double number1=Double.parseDouble(inputNumberOne.getText());
		double number2=Double.parseDouble(inputNumberTwo.getText());
		double result=0;
		if(fuhao.equals("+")){
			result=number1+number2;
		}
		else if(fuhao.equals("-")){
			result=number1-number2;
		}
		else if(fuhao.equals("*")){
			result=number1*number2;
		}
		else if(fuhao.equals("/")){
			result=number1/number2;
		}
		textShow.append(number1+" "+fuhao+" "+number2+" = "+result+"\n");
	} 
	catch (Exception exp) {
	  textShow.append("\n请输入数字字符\n"); 
	}
}
}import java.awt.event.*;
import javax.swing.*;
public class HandleListener implements ActionListener{
      JTextArea inpuText,showText;
      public void setInputText(JTextArea text) {
		inpuText=text;
	}
      public void setShowText(JTextArea text) {
		showText=text;
	}
      public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if(str.equals("copy"))
			showText.copy();
		else if(str.equals("cut"))
			showText.cut();
		else if(str.equals("paste"))
			inpuText.paste();
	}
}
import java.awt.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;
public class TextListener implements DocumentListener{
     JTextArea inputText,showText;
     public void setInputText(JTextArea text) {
		inputText=text;
	}
     public void setShowText(JTextArea text) {
		showText=text;
	}
     public void changedUpdate(DocumentEvent e) {
		String string=inputText.getText();
		//空格,数字和符号(!"S%^&*())组成的正则表达式
		String regex="[\\s\\d\\pp{Punct}]+";
		String words[]=string.split(regex);
		Arrays.sort(words);
		showText.setText(null);
		for(int i=0;i<words.length;i++)
			showText.append(words[i]+",");
	}
     public void  removeUpdate(DocumentEvent e) {
		changedUpdate(e);
	}
     public void insertUpdate(DocumentEvent e) {
		changedUpdate(e);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41398448/article/details/80969529
今日推荐