java中的两个未定义的方法

时间:2014-12-17 13:53:35

标签: java

不久前开始,一直致力于这个简单的缓存记忆练习。我已经走得很远(缓慢但肯定),但现在我实际上并不知道如何应对我所遇到的问题,并且无法通过Google搜索等方式为自己寻找任何解决方案。之前。我还需要使用Eclipse。

"方法clearMemory()未定义类型VC"

"方法setText(String)未定义类型VC"

import java.awt.BorderLayout;
import java.awt.Dimension;
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.JPanel;
import javax.swing.JTextField;


public class VC {


    public static void main(String[] args) {
        JFrame window = new JFrame();
        final Model model = new Model();
        window.setTitle("Cache memory - 2^n");
        window.setResizable(false);
        window.setSize(new Dimension(600, 300)); //Bilden som man ska gå efter har 620x320 men sen står det att man ska ha 600x300
        window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        window.setLocationRelativeTo(null);

        JPanel inputPanel = new JPanel();
        inputPanel.setPreferredSize(new Dimension(600, 150));
        final JTextField input1 = new JTextField("0");
        input1.setPreferredSize(new Dimension(100, 35));
        inputPanel.add(input1, BorderLayout.WEST);

        JPanel outputPanel = new JPanel();
        outputPanel.setPreferredSize(new Dimension(600, 100));
        final JLabel output = new JLabel("Result: ");
        output.setPreferredSize(new Dimension(350, 10));
        outputPanel.add(output, BorderLayout.CENTER);


        JPanel buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(600, 50));
        JButton computeButton = new JButton("Compute 2^n");
        computeButton.setPreferredSize(new Dimension(125, 40));
        JButton clearButton = new JButton("Clear memory");
        clearButton.setPreferredSize(new Dimension(125, 40));
        JButton quitButton = new JButton("Quit");
        quitButton.setPreferredSize(new Dimension(125, 40));

        buttonPanel.add(computeButton, BorderLayout.WEST);
        buttonPanel.add(clearButton, BorderLayout.CENTER);
        buttonPanel.add(quitButton, BorderLayout.EAST);

        window.add(inputPanel, BorderLayout.NORTH);
        window.add(outputPanel, BorderLayout.CENTER);
        window.add(buttonPanel, BorderLayout.SOUTH);

        quitButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                model.clearMemory();
                System.exit(0);

            }
        });

        clearButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                model.clearMemory();
                output.setText("Memory cleared.");

            }
        });

        computeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                try {
                    output.setText("Result:  " + model.compute2Power(Integer.valueOf(input1.getText()).intValue()));
                }
                catch (IllegalArgumentException e) {
                    output.setText("Input a positive number please!");
                    return;
                }
            }
        });



        window.setVisible(true);



    }
}

我可能在搜索时想出来但我的知识非常有限,所以如果你能用教学法向我解释,我会非常感激。

import java.util.HashMap;


public class Model {

    HashMap<Integer, Long> memory;


    public Model() {
        this.memory = new HashMap<Integer, Long>();
    }



    public void clearMemory() {
        this.memory.clear();
    }



    private long computePower (int power) {
        if (power == 0) {
            return 1L;          
        }       
        if (power == 1) {
            return 2L;
    }
        return 2L * computePower (power - 1);
}


    public long compute2Power(int power) {
        if (power < 0) {
            throw new IllegalArgumentException();
        }
        Long result = getValueFromMemory(Integer.valueOf(power));
        if (result != null) {
            return result.longValue();
        }
        result = Long.valueOf(computePower(power));
        addValueToMemory(Integer.valueOf(power), result);
        return result.longValue();
    }


    private void addValueToMemory(Integer value, Long result) {
        this.memory.put(value, result);
    }


    private Long getValueFromMemory(Integer value) {
        return (Long)this.memory.get(value);
    }






}

2 个答案:

答案 0 :(得分:4)

你可能想打电话:

model.clearMemory();

而不是:

VC.this.clearMemory();

由于clearMemoryModel类的方法。

VC.this.setText应为output.setText

答案 1 :(得分:0)

VC类中没有setText(String s)方法。但是,JLabel类中有一个。与clearMemory()相同。它是类Model的实例方法,因此您应该创建一个Model实例,然后在该实例上调用它。