import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class TextArea { /**
 * @param args
 */
public static void main(String[] args) {
// TODO 自动生成方法存根
TextAreaExample Tae = new TextAreaExample(); }}class TextAreaExample extends JFrame implements ActionListener{
JTextArea area1;
    JTextArea area2;
    JScrollPane js1;
    JScrollPane js2;
    JSplitPane jsp;
JButton b1,b2;
JLabel jl1,jl2;
JMenuBar mb;
JMenu mu1,mu2;
JMenuItem itemCopy,itemCut,itemPaste,item4;
JPanel p1;
HelpListen hlisten;
TextAreaExample() { area1 = new JTextArea();
area2 = new JTextArea();
mb=new JMenuBar();
hlisten=new HelpListen();
mu1=new JMenu("编辑");
mu2=new JMenu("帮助");
itemCopy=new JMenuItem("复制");
itemCut=new JMenuItem("剪切");
itemPaste=new JMenuItem("粘贴");
item4=new JMenuItem("关于");
itemCopy.addActionListener(this);
itemCut.addActionListener(this);
itemPaste.addActionListener(this);
item4.addActionListener(hlisten);
mu1.add(itemCopy);
mu1.add(itemCut);
mu1.add(itemPaste);
mu2.add(item4);
mb.add(mu1);
mb.add(mu2);
setJMenuBar(mb);
jl1=new JLabel("明文区");
jl2=new JLabel("密文区");
area1.setLineWrap(true);
area2.setLineWrap(true);
js1 = new JScrollPane(area1);
js2 = new JScrollPane(area2);
jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,js1,js2);
add(jsp,BorderLayout.CENTER);
jsp.setDividerLocation(180);
p1 = new JPanel();
b1 = new JButton("加密");
b2 = new JButton("清空");
p1.add(jl1);
p1.add(b1);
p1.add(b2);
p1.add(jl2);
add(p1,BorderLayout.NORTH);
    b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = area1.getText();
char a[]=str.toCharArray();
for(int i=0;i<a.length;i++)
{
a[i]=(char)(a[i]^'a');
}
String str1=new String(a);
//System.out.println(str);
area2.setText(str1);
} });
    //b2.add(comp);
    
setBounds(100, 100, 400, 400);
setVisible(true);
validate();
    }
public void actionPerformed(ActionEvent e)
    {
     if(e.getSource()==itemCopy)
     area1.copy();
     else if(e.getSource()==itemCut)
     area1.cut();
     else if(e.getSource()==itemPaste)
     area2.paste();
    }

class HelpListen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Introduction in=new Introduction();
}
}
class ClearListen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
area1.setText="";//编译报错 有没有办法在这个类里访问这个临时变量
                   我不想把所有的监听都写成匿名类的形式,就像这样 b1.addActionListener(new ActionListener() 
}
}class  Introduction extends JFrame
{    
 JLabel lab1;
 Introduction()
 {   
 lab1=new JLabel("");
 add(lab1);
 setBounds(200,100,300,240);
 setVisible(true);
 validate();
 }
}

解决方案 »

  1.   


    package CSDNTest;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class TextArea { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    TextAreaExample Tae = new TextAreaExample(); }}class TextAreaExample extends JFrame implements ActionListener {
    private static JTextArea area1;
    JTextArea area2;
    JScrollPane js1;
    JScrollPane js2;
    JSplitPane jsp;
    JButton b1, b2;
    JLabel jl1, jl2;
    JMenuBar mb;
    JMenu mu1, mu2;
    JMenuItem itemCopy, itemCut, itemPaste, item4;
    JPanel p1;
    HelpListen hlisten;

    public static JTextArea getArea1Instance(){
    return area1;
    } TextAreaExample() { area1 = new JTextArea();
    area2 = new JTextArea();
    mb = new JMenuBar();
    hlisten = new HelpListen();
    mu1 = new JMenu("编辑");
    mu2 = new JMenu("帮助");
    itemCopy = new JMenuItem("复制");
    itemCut = new JMenuItem("剪切");
    itemPaste = new JMenuItem("粘贴");
    item4 = new JMenuItem("关于");
    itemCopy.addActionListener(this);
    itemCut.addActionListener(this);
    itemPaste.addActionListener(this);
    item4.addActionListener(hlisten);
    mu1.add(itemCopy);
    mu1.add(itemCut);
    mu1.add(itemPaste);
    mu2.add(item4);
    mb.add(mu1);
    mb.add(mu2);
    setJMenuBar(mb);
    jl1 = new JLabel("明文区");
    jl2 = new JLabel("密文区");
    area1.setLineWrap(true);
    area2.setLineWrap(true);
    js1 = new JScrollPane(area1);
    js2 = new JScrollPane(area2);
    jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, js1, js2);
    add(jsp, BorderLayout.CENTER);
    jsp.setDividerLocation(180);
    p1 = new JPanel();
    b1 = new JButton("加密");
    b2 = new JButton("清空");
    p1.add(jl1);
    p1.add(b1);
    p1.add(b2);
    p1.add(jl2);
    add(p1, BorderLayout.NORTH);
    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String str = area1.getText();
    char a[] = str.toCharArray();
    for (int i = 0; i < a.length; i++) {
    a[i] = (char) (a[i] ^ 'a');
    }
    String str1 = new String(a);
    // System.out.println(str);
    area2.setText(str1);
    } });
    // b2.add(comp); setBounds(100, 100, 400, 400);
    setVisible(true);
    validate();
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == itemCopy)
    area1.copy();
    else if (e.getSource() == itemCut)
    area1.cut();
    else if (e.getSource() == itemPaste)
    area2.paste();
    }
    }class HelpListen implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    Introduction in = new Introduction();
    }
    }class ClearListen implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    TextAreaExample.getArea1Instance().setText("");//编译报错 有没有办法在这个类里访问这个临时变量
    // 我不想把所有的监听都写成匿名类的形式,
    // 就像这样 b1.addActionListener(new ActionListener()
    }
    }class Introduction extends JFrame {
    JLabel lab1; Introduction() {
    lab1 = new JLabel("");
    add(lab1);
    setBounds(200, 100, 300, 240);
    setVisible(true);
    validate();
    }
    }
      

  2.   

    把JTextArea area1; 改成 static JTextArea area1; 
    area1.setText="";这句改成TextAreaExample.area1.setText("");
      

  3.   

    1楼的在前面添加了private不行吧。。两个不同的类这样好像不能访问到是有变量吧
      

  4.   

    还可以:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;class TextArea { /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    // TODO 自动生成方法存根 
    TextAreaExample Tae = new TextAreaExample(); } } class TextAreaExample extends JFrame implements ActionListener{ 
     JTextArea area1; 
        JTextArea area2; 
        JScrollPane js1; 
        JScrollPane js2; 
        JSplitPane jsp; 
    JButton b1,b2; 
    JLabel jl1,jl2; 
    JMenuBar mb; 
    JMenu mu1,mu2; 
    JMenuItem itemCopy,itemCut,itemPaste,item4; 
    JPanel p1; 
    HelpListen hlisten; 
    TextAreaExample() { area1 = new JTextArea(); 
    area2 = new JTextArea(); 
    mb=new JMenuBar(); 
    hlisten=new HelpListen(); 
    mu1=new JMenu("编辑"); 
    mu2=new JMenu("帮助"); 
    itemCopy=new JMenuItem("复制"); 
    itemCut=new JMenuItem("剪切"); 
    itemPaste=new JMenuItem("粘贴"); 
    item4=new JMenuItem("关于"); 
    itemCopy.addActionListener(this); 
    itemCut.addActionListener(this); 
    itemPaste.addActionListener(this); 
    item4.addActionListener(hlisten); 
    mu1.add(itemCopy); 
    mu1.add(itemCut); 
    mu1.add(itemPaste); 
    mu2.add(item4); 
    mb.add(mu1); 
    mb.add(mu2); 
    setJMenuBar(mb); 
    jl1=new JLabel("明文区"); 
    jl2=new JLabel("密文区"); 
    area1.setLineWrap(true); 
    area2.setLineWrap(true); 
    js1 = new JScrollPane(area1); 
    js2 = new JScrollPane(area2); 
    jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,js1,js2); 
    add(jsp,BorderLayout.CENTER); 
    jsp.setDividerLocation(180); 
    p1 = new JPanel(); 
    b1 = new JButton("加密"); 
    b2 = new JButton("清空"); 
    p1.add(jl1); 
    p1.add(b1); 
    p1.add(b2); 
    p1.add(jl2); 
    add(p1,BorderLayout.NORTH); 
        b1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    String str = area1.getText(); 
    char a[]=str.toCharArray(); 
    for(int i=0;i <a.length;i++) 

    a[i]=(char)(a[i]^'a'); 

    String str1=new String(a); 
    //System.out.println(str); 
    area2.setText(str1); 
    } }); 
        //b2.add(comp); 
        
    setBounds(100, 100, 400, 400); 
    setVisible(true); 
    validate(); 
        } 
    public void actionPerformed(ActionEvent e) 
        { 
        if(e.getSource()==itemCopy) 
        area1.copy(); 
        else if(e.getSource()==itemCut) 
        area1.cut(); 
        else if(e.getSource()==itemPaste) 
        area2.paste(); 
        } 
    } class HelpListen implements ActionListener 

    public void actionPerformed(ActionEvent e) 

    Introduction in=new Introduction(); 

    } class ClearListen extends TextAreaExample implements ActionListener

    public void actionPerformed(ActionEvent e) 

    area1.setText("");//编译报错 有没有办法在这个类里访问这个临时变量 

    } class  Introduction extends JFrame 
    {    
    JLabel lab1; 
    Introduction() 
    {  
    lab1=new JLabel(""); 
    add(lab1); 
    setBounds(200,100,300,240); 
    setVisible(true); 
    validate(); 

    }
      

  5.   


    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;class TextArea { /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    // TODO 自动生成方法存根 
    TextAreaExample Tae = new TextAreaExample(); } } class TextAreaExample extends JFrame implements ActionListener{ 
     JTextArea area1; 
        JTextArea area2; 
        JScrollPane js1; 
        JScrollPane js2; 
        JSplitPane jsp; 
    JButton b1,b2; 
    JLabel jl1,jl2; 
    JMenuBar mb; 
    JMenu mu1,mu2; 
    JMenuItem itemCopy,itemCut,itemPaste,item4; 
    JPanel p1; 
    HelpListen hlisten; 
    TextAreaExample() { area1 = new JTextArea(); 
    area2 = new JTextArea(); 
    mb=new JMenuBar(); 
    hlisten=new HelpListen(); 
    mu1=new JMenu("编辑"); 
    mu2=new JMenu("帮助"); 
    itemCopy=new JMenuItem("复制"); 
    itemCut=new JMenuItem("剪切"); 
    itemPaste=new JMenuItem("粘贴"); 
    item4=new JMenuItem("关于"); 
    itemCopy.addActionListener(this); 
    itemCut.addActionListener(this); 
    itemPaste.addActionListener(this); 
    item4.addActionListener(hlisten); 
    mu1.add(itemCopy); 
    mu1.add(itemCut); 
    mu1.add(itemPaste); 
    mu2.add(item4); 
    mb.add(mu1); 
    mb.add(mu2); 
    setJMenuBar(mb); 
    jl1=new JLabel("明文区"); 
    jl2=new JLabel("密文区"); 
    area1.setLineWrap(true); 
    area2.setLineWrap(true); 
    js1 = new JScrollPane(area1); 
    js2 = new JScrollPane(area2); 
    jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,js1,js2); 
    add(jsp,BorderLayout.CENTER); 
    jsp.setDividerLocation(180); 
    p1 = new JPanel(); 
    b1 = new JButton("加密"); 
    b2 = new JButton("清空"); 
    p1.add(jl1); 
    p1.add(b1); 
    p1.add(b2); 
    p1.add(jl2); 
    add(p1,BorderLayout.NORTH); 
        b1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    String str = area1.getText(); 
    char a[]=str.toCharArray(); 
    for(int i=0;i <a.length;i++) 

    a[i]=(char)(a[i]^'a'); 

    String str1=new String(a); 
    //System.out.println(str); 
    area2.setText(str1); 
    } }); 
        //b2.add(comp); 
        
    setBounds(100, 100, 400, 400); 
    setVisible(true); 
    validate(); 
        } 
    public void actionPerformed(ActionEvent e) 
        { 
        if(e.getSource()==itemCopy) 
        area1.copy(); 
        else if(e.getSource()==itemCut) 
        area1.cut(); 
        else if(e.getSource()==itemPaste) 
        area2.paste(); 
        } 
    } class HelpListen implements ActionListener 

    public void actionPerformed(ActionEvent e) 

    Introduction in=new Introduction(); 

    } class ClearListen extends TextAreaExample implements ActionListener

    public void actionPerformed(ActionEvent e) 

    area1.setText("");//编译报错 有没有办法在这个类里访问这个临时变量 

    } class  Introduction extends JFrame 
    {    
    JLabel lab1; 
    Introduction() 
    {  
    lab1=new JLabel(""); 
    add(lab1); 
    setBounds(200,100,300,240); 
    setVisible(true); 
    validate(); 

    }
      

  6.   

    上面的更改了class ClearListen extends TextAreaExample implements ActionListener 
    和area1.setText("");