Java:在Eclipse中调用另一个类时出错

时间:2015-04-11 08:59:36

标签: java eclipse

//我设法修复它,有一个完全不相关的错误没有作为错误给出,我删除了代码,现在它可以正常工作。谢谢你的时间。

嗨,
我在Eclipse中创建了一个Java程序,虽然在我的课程中出现了1个错误,但它的工作正常,我稍后会在我的代码中显示它。我有一个Folder类,它使用Document类,并在单击菜单中的按钮时创建一个新类。这曾经工作得很好,但昨天Eclipse和我的电脑发生了一些奇怪的事情,现在它不会工作,尽管代码没有改变。

如果您需要额外的信息,或者我的问题不正确,请说出来。这是我的第一个问题之一。我还在学习。

//编辑 当我将鼠标悬停在Document.java中的错误(公共类Document extends JInternalFrame实现ActionListener)时,它说:

The type Document must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

但是当我把它抽象化时,它只会产生更多的错误。


Folder.java

import javax.swing.*;
import javax.swing.text.*;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font.*;
import java.util.*;

//Main screen

public class Folder extends JFrame implements ActionListener {
    public ImageIcon img = new ImageIcon("C:/users/jeroe_000/pictures/flogo.png");
    private JFrame JFrame = new JFrame();
    private JMenuBar mb = new JMenuBar();
    private JMenu men = new JMenu("Window");
    private JMenuItem newItem = new JMenuItem("New window");
    private JMenuItem closeItem = new JMenuItem("Close all windows");
    private JMenuItem exit = new JMenuItem("Close");
    public JPanel startscreen = new JPanel();

    public boolean opened = false;

    public String programName = "Sitebucket Office";


    private JDesktopPane bord = new JDesktopPane();
    private int n; //number of internal windows 



    private Folder() {      
        setContentPane(bord);
        setJMenuBar(mb);
        mb.add(men);
        men.add(newItem); men.add(closeItem); men.addSeparator(); men.add(exit);
        Container contentPane = getContentPane();

        //connect listener to the menu alternatives
        newItem.addActionListener(this);
        closeItem.addActionListener(this);
        exit.addActionListener(this);
        setTitle("Sitebucket Office");
        setSize(900, 700);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        startscreen.setVisible(true);
        JLabel welcome = new JLabel("Welcome to Sitebucket Office");
        contentPane.add(startscreen);
        startscreen.add(welcome, BorderLayout.SOUTH);


        //Opmaak

        bord.setPreferredSize(new Dimension(100, 25));
        bord.setBackground(new Color(58, 119, 165, 70 ));
        bord.setForeground(new Color(255, 255, 255));

        mb.setBackground(new Color(58, 119, 165));
        mb.setForeground(new Color(255, 255, 255));

        men.setBackground(new Color(58, 119, 165));
        men.setForeground(new Color(255, 255, 255));
        men.setFont(new Font("Oxygen Light", Font.PLAIN,  20));

        newItem.setBackground(new Color(58, 119, 165));
        newItem.setForeground(new Color(255, 255, 255));
        newItem.setFont(new Font("Oxygen Light", Font.PLAIN, 14));

        closeItem.setBackground(new Color(58, 119, 165));
        closeItem.setForeground(new Color(255, 255, 255));
        closeItem.setFont(new Font("Oxygen Light", Font.PLAIN, 14));

        exit.setBackground(new Color(58, 119, 165));
        exit.setForeground(new Color(255, 255, 255));
        exit.setFont(new Font("Oxygen Light", Font.PLAIN, 14));
    }



    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == newItem) {
            Document f = new  Document("Document " + (n+1));
            bord.add(f);
            int x = Math.min(30*n, bord.getWidth() - f.getWidth());
            int y = Math.min(30*n, bord.getHeight() - f.getHeight());
            f.setLocation(x, y);

            f.moveToFront();
            n++;
        }

        else if (e.getSource() == closeItem) {
            JInternalFrame[] a = bord.getAllFrames();
            for (int i=0; i<a.length; i++)
                a[i].dispose();
            n = 0;
        }

        else if (e.getSource() == exit){
            System.exit(0);
        }
    }

    public static void main (String arg[]) {
        new Folder();
    }

}

Document.java

import javax.swing.*;
import javax.swing.text.*;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font.*;
import java.util.*;

public class Document extends JInternalFrame implements ActionListener{    //HERE'S THE ERROR
    public JTextArea area = new JTextArea(10,60);
    public JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
    public String currentFile = "Untitled";
    public boolean changed = false;
    public JTextField fontField;






    public Document(String title){

        //Window
        super(title, true, true, true, true);
        setSize(800,600);
        setVisible(true);


        fontField = new JTextField(5);

        fontField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = fontField.getText();
                try {
                    float fontSize = Float.parseFloat(text);
                    int textSize = Math.round(fontSize);
                    Font textFont = fontField.getFont();
                    textFont = textFont.deriveFont(fontSize);
                    area.setFont(textFont);
                    revalidate();
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });

        //Textarea
        JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//Scrollable textarea
        getContentPane().add(scroll, BorderLayout.CENTER); //Adding the textarea


        //Menubar
        JMenuBar textMb = new JMenuBar(); 
        setJMenuBar(textMb);
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu layoutMenu = new JMenu("Layout");
        JMenu fontMenu = new JMenu("Font");
        JMenu fontSizeMenu = new JMenu("Fontsize");
        JMenu menuSizeMenu = new JMenu("Menu-fontsize");
        JMenu textSizeMenu = new JMenu("Text-fontsize");

        JMenu betaMenu = new JMenu("Bèta functions");

        JLabel fontSizeText = new JLabel("Enter the fontsize down here");
        textMb.add(fileMenu); textMb.add(editMenu); textMb.add(layoutMenu);

        //Adding buttons to filemenu -> menubar
        fileMenu.add(open); fileMenu.add(save); fileMenu.add(saveAs);

        //Adding buttons to editmenu -> menubar
        editMenu.add(cut); editMenu.add(copy); editMenu.add(paste); editMenu.add(selectAll);
        editMenu.getItem(0).setText("Cut");
        editMenu.getItem(1).setText("Copy");
        editMenu.getItem(2).setText("Paste");
        editMenu.getItem(3).setText("Select all");

        //Adding buttons to layoutmenu ->menubar
        layoutMenu.add(fontMenu); layoutMenu.add(fontSizeMenu);

        //Fontmenu
        fontMenu.add(arial); fontMenu.add(opensans); fontMenu.add(oxygenlight); fontMenu.add(segoeuilight); fontMenu.add(timesnewroman);

        //Fontsizemenu
        fontSizeMenu.add(fontSizeText); fontSizeMenu.add(fontField);


        //Layout
        textMb.setBackground(new Color(58, 119, 165));

        fileMenu.setBackground(new Color(58, 119, 165));
        fileMenu.setForeground(new Color(255, 255, 255));
        fileMenu.setFont(new Font("Oxygen Light", Font.PLAIN, 16));

        editMenu.setBackground(new Color(58, 119, 165));
        editMenu.setForeground(new Color(255, 255, 255));
        editMenu.setFont(new Font("Oxygen Light", Font.PLAIN, 16));

        layoutMenu.setBackground(new Color(58, 119, 165));
        layoutMenu.setForeground(new Color(255, 255, 255));
        layoutMenu.setFont(new Font("Oxygen Light", Font.PLAIN, 16));




        //Default stuff
        save.setEnabled(false);
        saveAs.setEnabled(false);
        setVisible(true);

        area.addKeyListener(kl); //To be able to write
        setTitle(currentFile); //Screentitle
    }





        private KeyListener kl = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                changed = true;
                save.setEnabled(true);
                saveAs.setEnabled(true);
            }
        };


        String text2 = fontField.getText();
        float fontSize2 = Float.parseFloat(text2);
        int textSize2 = Math.round(fontSize2);



        Action arial = new AbstractAction("Arial") {
            public void actionPerformed(ActionEvent e) {
                area.setFont(new Font("Arial", Font.PLAIN, textSize2));
                revalidate();
            }
        };

        Action opensans = new AbstractAction("Open Sans") {
            public void actionPerformed(ActionEvent e) {
                area.setFont(new Font("Open Sans", Font.PLAIN, textSize2));
            }
        };

        Action oxygenlight = new AbstractAction("Oxygen Light") {
            public void actionPerformed(ActionEvent e) {
                area.setFont(new Font("Oxygen Light", Font.PLAIN, 24));
            }
        };

        Action segoeuilight = new AbstractAction("Segoe UI Light") {
            public void actionPerformed(ActionEvent e) {
                area.setFont(new Font("Segoe UI Light", Font.PLAIN, 24));
            }
        };

        Action timesnewroman = new AbstractAction("Times New Roman") {
            public void actionPerformed(ActionEvent e) {
                area.setFont(new Font("Roman", Font.PLAIN, 24));
            }
        };

        //Default stuff for new documents
        Action newFile = new AbstractAction("New") {
            public void actionPerformed(ActionEvent e) {
                saveOld();
                area.setText("");
                currentFile = "Untitled";
                setTitle(currentFile);
                changed = false;
                save.setEnabled(false);
                saveAs.setEnabled(false);
            }
        };

        //Default stuff for opening documents
        Action open = new AbstractAction("Open ...") {
            public void actionPerformed(ActionEvent e) {
                saveOld();
                if (dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    readFile(dialog.getSelectedFile().getAbsolutePath());
                }
                saveAs.setEnabled(true);
            }
        };

        //Default stuff for saving documents
        Action save = new AbstractAction("Save") {
            public void actionPerformed(ActionEvent e) {
                if (!currentFile.equals("Naamloos")){
                    saveFile(currentFile);
                }
                else {
                    saveFileAs();
                }

            }
        };

        //Default stuff for saving as documents
        Action saveAs = new AbstractAction("Save as ...") {
            public void actionPerformed(ActionEvent e) {
                saveFileAs();
            }
        };

        //Default stuff for closing documents
        Action exit = new AbstractAction("Close") {
            public void actionPerformed(ActionEvent e) {
                if(changed == true){ 
                    saveFileAs();
                    System.exit(0);
                }

                else{
                    saveOld();
                    System.exit(0);                 
                }
            }
        };

        ActionMap m = area.getActionMap();
        Action cut = m.get(DefaultEditorKit.cutAction);
        Action copy = m.get(DefaultEditorKit.copyAction);
        Action paste = m.get(DefaultEditorKit.pasteAction);
        Action selectAll = m.get(DefaultEditorKit.selectAllAction);

        private void saveFileAs() {
            if (dialog.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
                saveFile(dialog.getSelectedFile().getAbsolutePath());
            }
        }

        private void saveOld() {
            if (changed) {
                if (JOptionPane.showConfirmDialog(this,  currentFile + "save?", "", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                    saveFile(currentFile);
                }
            }
        }

        private void readFile(String fileName) {
            try {
                FileReader r = new FileReader(fileName);
                area.read(r, null);
                r.close();
                currentFile = fileName;
                setTitle(currentFile);
                changed = false;
            }
            catch (IOException e) {
                JOptionPane.showMessageDialog(this, fileName + "can't be found");
            }
        }

        private void saveFile(String fileName) {
            try {
                FileWriter w = new FileWriter(fileName);
                area.write(w);
                w.close();
                currentFile = fileName;
                setTitle(currentFile);
                changed = false;
                save.setEnabled(false);
            }
            catch (IOException e) {}
        }
    }

IntPaintWindow.java

import javax.swing.*;
import java.awt.*;

public class IntPaintWindow extends JInternalFrame{
    public IntPaintWindow(String title){
        super(title, true, true, true, true);
        setSize(400, 300);
        setVisible(true);

    }
}

错误

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type Document

    at Folder.actionPerformed(Folder.java:84)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我知道这是很多信息,但如果你们能帮我修复这个错误,我真的很感激。

亲切的问候,Jeroen

0 个答案:

没有答案