没错误,只是需要添加actionlistener用来实现我想要的功能。我自己不会:(

解决方案 »

  1.   

    不太清楚你的格式要求,参考一下,如果editor1和editor2有多行,它们会被拆分
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.text.*;public class SimpleTextEditor implements ActionListener{ JFrame frame;
    JTextArea editor;
    JTextArea editor1;
    JTextArea editor2;
    Font editorfont = new Font("Monospaced",Font.PLAIN,12);
    int tabsize = 5; 
    /**
    */
    public static void main(String[] args){ SimpleTextEditor simpletexteditor = new SimpleTextEditor();
    simpletexteditor.init(); }
    public void init(){
    /*SecurityManager securitymanager = System.getSecurityManager();
    if (securitymanager != null){
    try{
    securitymanager.checkSystemClipboardAccess();
    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (debug) System.out.println("Using system clipboard");
    }catch (SecurityException se){
    showExceptionErrorMessage("SystemClipboardAccess is denied. Using application clipboard.");
    clipboard = new Clipboard("SimpleTextEditor");
    System.out.println("SystemClipboardAccess is denied.");
    System.out.println("SecurityException: " + se.getMessage());
    if (debug) System.out.println("Using application clipboard"); }
    }else{
    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (debug) System.out.println("Using system clipboard");
    }*/

    frame = new JFrame("SimpleTextEditor");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //button controls
    JPanel controlpanel = new JPanel();
    JButton openbutton = new JButton("Open A File");
    JButton writebutton = new JButton("Save To File");
    JButton exitbutton = new JButton("Exit");
    exitbutton.addActionListener(this);
    openbutton.addActionListener(this);
    writebutton.addActionListener(this);
    controlpanel.add(writebutton);
    controlpanel.add(openbutton);
    controlpanel.add(exitbutton);
    frame.getContentPane().add(controlpanel,"North");
    //insert button
    JPanel insertpanel = new JPanel();

    editor1 = new JTextArea(new PlainDocument(),"",3,10);
    insertpanel.add(editor1);

    editor2 = new JTextArea(new PlainDocument(),"",3,10);
    insertpanel.add(editor2);
    JButton addbutton = new JButton("Add");
    addbutton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    addbutton_actionPerformed(e);
    }
    });

    insertpanel.add(addbutton);
    frame.getContentPane().add(insertpanel,"South");
    //editor
    editor = new JTextArea(new PlainDocument(),"",20,60); //initializes a JTextArea with width 60 and height 30 
    editor.setFont(editorfont);
    editor.setTabSize(tabsize);
    JScrollPane scrollpane = new JScrollPane(editor);
    frame.getContentPane().add(scrollpane,"Center");
    frame.pack();
    frame.show();
    } public File getAFileForSave(){
    File file = null;
    File currentdirectory = new File(".");
    JFileChooser filechooser = new JFileChooser(currentdirectory);
    int replycode = filechooser.showSaveDialog(frame);
    if (replycode == JFileChooser.APPROVE_OPTION){
    file = filechooser.getSelectedFile();
    }
    return file; } /** Brings up as imple file open dialog and returns the file selected by the user.
    */
    public File getAFileToOpen(){
    File file = null;
    File currentdirectory = new File(".");
    JFileChooser filechooser = new JFileChooser(currentdirectory);
    int replycode = filechooser.showOpenDialog(frame);
    if (replycode == JFileChooser.APPROVE_OPTION){
    file = filechooser.getSelectedFile();
    }
    return file;
    } /** Writes the string to the input file.
    */
    public void writeStringToFile(File file, String s){ try{
    FileWriter filewriter = new FileWriter(file);
    StringReader stringreader = new StringReader(s);
    BufferedReader bufferedreader = new BufferedReader(stringreader);
    String lineread = "";
    while ((lineread = bufferedreader.readLine()) != null){
    filewriter.write(lineread + "\r\n");
    }
    filewriter.close();
    }catch (FileNotFoundException fnfe){
    System.err.println("FileNotFoundException: " + fnfe.getMessage());
    showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
    }catch (IOException ioe){
    System.err.println("IOException: " + ioe.getMessage());
    showExceptionErrorMessage("IOException: " + ioe.getMessage());
    }
    } /** Appends the contents of the input file to the text editor.
    */
    public void writeFileToEditor(File file){ try{
    FileReader filereader = new FileReader(file);
    BufferedReader bufferedreader = new BufferedReader(filereader);
    String lineread = "";
    while ((lineread = bufferedreader.readLine()) != null){
    editor.append(lineread + "\n");
    }
    filereader.close();
    }catch (FileNotFoundException fnfe){
    System.err.println("FileNotFoundException: " + fnfe.getMessage());
    showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
    }catch (IOException ioe){
    System.err.println("IOException: " + ioe.getMessage());
    showExceptionErrorMessage("IOException: " + ioe.getMessage());
    } } /** Provides actions to user input.
    */
    public void actionPerformed(ActionEvent actionevent){
    String actioncommand = actionevent.getActionCommand();
    if (actioncommand.compareTo("Exit") == 0) {
    System.exit(0);
    }else if (actioncommand.compareTo("Open A File") == 0) {
    File f = getAFileToOpen();
    writeFileToEditor(f);
    }else if (actioncommand.compareTo("Save To File") == 0) {
    String s = editor.getText();
    File f = getAFileForSave();
    writeStringToFile(f,s);
    showMessage("Contents of the Text Editor saved to file " + f.getName());
    }
    } /** Conveniently displays a message to the user and waits for 
    * confirmation.
    */
    public void showMessage(String s){
    JOptionPane.showMessageDialog(frame,s);
    } /** Conveniently displays an exception error message to the user 
    * and waits for confirmation.
    */
    public void showExceptionErrorMessage(String s){
    JOptionPane.showMessageDialog(frame,s,s,JOptionPane.ERROR_MESSAGE);
    }
    void addbutton_actionPerformed(ActionEvent e) {
    String str1 = editor1.getText();
    String str2 = editor2.getText();
    String line1, line2;
    try {
    StringBuffer line = new StringBuffer();
    line.setLength(60);
    int i;
    int count = editor1.getLineCount();
    if(count < editor2.getLineCount()) count = editor2.getLineCount();
    for(i=0;i<count;i++) {
    if(i < editor1.getLineCount()) {
    line1 = str1.substring(editor1.getLineStartOffset(i), 
       editor1.getLineEndOffset(i)-1);
    for(int j=0;j<line1.length();j++) 
    line.setCharAt(j, line1.charAt(j));
    }
    if(i < editor2.getLineCount()) {
    line2 = str2.substring(editor2.getLineStartOffset(i),
                  editor2.getLineEndOffset(i)-1);
    for(int j=0;j<line2.length();j++) 
    line.setCharAt(j+10, line2.charAt(j));
    }
    editor.append(line+"\n");
    }

    }
    catch(Exception ex) {
    System.out.println(ex.getMessage());
    }

    }
    }
      

  2.   

    如果要是希望editor1是一个list,里面所选的内容可以被放入editor,该怎么实现??