当前位置: 代码迷 >> Java相关 >> TextListener类为什么不能实例化??求急
  详细解决方案

TextListener类为什么不能实例化??求急

热度:804   发布时间:2013-11-03 22:12:08.0
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  

----------------解决方案--------------------------------------------------------
你查一下api,TextListener是个接口对象,java.awt.event Interface TextListener,所以不可以
----------------解决方案--------------------------------------------------------
实例化接口是没有意义的,接口本身就是抽象得来,你现在要实例化,就是相反的一个过程,有悖面向对象的原则,如果你想实例化它,可以考虑实例化实现了这个接口的一个类。
----------------解决方案--------------------------------------------------------
可以写个内部类 TextListener tl=new TextListener(){/*这里实现接口方法*/};就可以用了

----------------解决方案--------------------------------------------------------
回复 4楼 xstar海绵
这方法可行,就是想用内部类来写,thankyou
----------------解决方案--------------------------------------------------------
  相关解决方案