| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 838 人关注过本帖
标题:TextListener类为什么不能实例化??求急
只看楼主 加入收藏
编程要有思想
Rank: 1
等 级:新手上路
威 望:1
帖 子:28
专家分:1
注 册:2013-10-10
结帖率:0
收藏
已结贴  问题点数:10 回复次数:4 
TextListener类为什么不能实例化??求急
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.TextListener;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;


public class WindowDocument2 extends JFrame {

    private JPanel contentPane;
    private JTextArea inputText;
    private JTextArea showText;
    private JMenuItem menuItem;
    private JMenuItem menuItem_1;
    private JMenuItem menuItem_2;
    private JMenuItem copy;
    private JMenuItem paste;
    private JMenuItem cut;
    private TextListener textChangeListener;
    private HandleListener handlelistener;
    private TextListener textListener;
    private WindowDocument2 textchangelisener;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WindowDocument2 frame = new WindowDocument2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public WindowDocument2() {
        setTitle("单词排序");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        getContentPane().add(new JScrollPane(inputText));
        getContentPane().add(new JScrollPane(showText));
        textChangeListener = new TextListener();
        handlelistener = new HandleListener();
        ((WindowDocument2) textChangeListener).setInputText(inputText);
        ((WindowDocument2) textChangeListener).setShowText(showText);  
        handlelistener.setInputText(inputText);
        handlelistener.setShowText(showText);  
        (inputText.getDocument()).addDocumentListener(textChangeListener);//向文档注册监视器
        copy.addActionListener(handlelistener);                       //向菜单项注册监视器
        cut.addActionListener(handlelistener);
        paste.addActionListener(handlelistener);
        
        JMenu menu = new JMenu("编辑");
        menuBar.add(menu);
        
        copy = new JMenuItem("复制");
        menu.add(copy);
        
        paste = new JMenuItem("粘贴");
        menu.add(paste);
        
        cut = new JMenuItem("剪切");
        menu.add(cut);
        copy.setAccelerator(KeyStroke.getKeyStroke('C'));
        cut.setAccelerator(KeyStroke.getKeyStroke('x'));
        paste.setAccelerator(KeyStroke.getKeyStroke('p'));
        copy.setActionCommand("copy");
        cut.setActionCommand("cut");
        paste.setActionCommand("paste");
        
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        inputText = new JTextArea();
        inputText.setWrapStyleWord(true);
        inputText.setLineWrap(true);
        inputText.setBounds(56, 31, 142, 194);
        
        inputText.setLineWrap(true);
        inputText.setWrapStyleWord(true);
        handlelistener=new HandleListener();
        contentPane.add(inputText);
        inputText.setColumns(10);
        
        showText = new JTextArea();
        showText.setEditable(false);
        showText.setBounds(244, 31, 142, 194);
        textchangelisener.setInputText(inputText);
        ((WindowDocument2) textChangeListener).setShowText(showText);
        contentPane.add(showText);
        showText.setColumns(10);
    }



public void setInputText(JTextArea text){
    inputText=text;
   
}
public void setShowText(JTextArea text){
    showText=text;
}
public void changedUpdate(DocumentEvent e){
    String str=inputText.getText();
    String regex="[\\s\\d\\p{Punct}]+";
    String words[]=str.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);
}

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"))
    showText.paste();
}

}

搜索更多相关主题的帖子: import 
2013-11-03 22:12
ghjsmzy
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:187
专家分:573
注 册:2009-6-17
收藏
得分:4 
你查一下api,TextListener是个接口对象,java.awt.event Interface TextListener,所以不可以
2013-11-04 14:06
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
收藏
得分:4 
实例化接口是没有意义的,接口本身就是抽象得来,你现在要实例化,就是相反的一个过程,有悖面向对象的原则,如果你想实例化它,可以考虑实例化实现了这个接口的一个类。
2013-11-04 15:40
xstar海绵
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:26
专家分:160
注 册:2013-11-3
收藏
得分:4 
可以写个内部类 TextListener tl=new TextListener(){/*这里实现接口方法*/};就可以用了
2013-11-04 22:53
编程要有思想
Rank: 1
等 级:新手上路
威 望:1
帖 子:28
专家分:1
注 册:2013-10-10
收藏
得分:0 
回复 4楼 xstar海绵
这方法可行,就是想用内部类来写,thankyou
2013-11-11 21:51
快速回复:TextListener类为什么不能实例化??求急
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015491 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved