鱼C论坛

 找回密码
 立即注册
查看: 2537|回复: 2

刚学的事件监视器,不知哪错了=。=

[复制链接]
发表于 2019-4-5 10:51:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. public class tlp {
  2.         public static void main(String args[]) {
  3.                 WindowActionEvent win=new WindowActionEvent();
  4.                 PoliceListen police=new PoliceListen();
  5.                 win.setMyCommandListener(police);
  6.                 win.setTitle("处理ActionEvent事件");
  7.                 win.setBounds(100,100,460,360);
  8.                 }
  9. }
复制代码
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class WindowActionEvent extends JFrame {
  5.         JTextField inputText;
  6.         JTextArea textShow;
  7.         JButton button;
  8.         MyCommandListener listener;
  9.         public WindowActionEvent() {
  10.                 init();
  11.                 setVisible(true);
  12.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         }
  14.         void init() {
  15.                 setLayout(new FlowLayout());
  16.                 inputText=new JTextField(10);
  17.                 button=new JButton("确定");
  18.                 textShow=new JTextArea(9,30);
  19.                 add(inputText);
  20.                 add(button);
  21.                 add(new JScrollPane(textShow));
  22.         }
  23.         void setMyCommandListener(MyCommandListener listener) {
  24.                 this.listener=listener;
  25.                 listener.setJTextField(inputText);
  26.                 listener.setJTextArea(textShow);
  27.                 inputText.addActionListener(listener);
  28.                 button.addActionListener(listener);
  29.         }
  30. }
复制代码

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public interface MyCommandListener extends ActionListener {
  4.         public void setJTextField(JTextField text);
  5.         public void
  6.         setJTextArea(JTextArea area);
  7.        
  8. }
复制代码

  1. import java.awt.event.*;
  2. import javax.swing.*;

  3. public class PoliceListen implements MyCommandListener {
  4.         JTextField textInput;
  5.         JTextArea showText;
  6.         public void setJTextField(JTextField text) {
  7.                 textInput=text;
  8.                
  9.         }
  10.         public void setTextArea(JTextArea area) {
  11.                 showText=area;
  12.         }
  13.         public void actionPerformed(ActionEvent e) {
  14.                 String str=textInput.getText();
  15.                 showText.append(str+"长度"+str.length()+"\n");
  16.         }
  17. }
复制代码


用的是eclipse,PoliceListen类的“public class policeListen”的policeListen有红线》》
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-15 19:10:00 | 显示全部楼层
好像有匿名对象和内部类 以前我对这个很有研究 只是现在弄c++了 java的内部类的东西忘完了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 06:54:43 | 显示全部楼层
在那类中setTextArea,少了个J,导致MyCommandListener接口中的方法未实现。未细看,新手,错了轻喷。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 16:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表