如何从我的其他类调用方法

时间:2014-03-30 04:29:16

标签: java

如何在Houses类中调用方法步骤()。

这是我的主要类Ghetto,我想在Houses类中调用它的方法步骤?

这是整个犹太人区的课程:

我希望actionListener和actionPreforemd让我调用另一个类

   package ghetto;
   import ghetto.House;
   import ghetto.Ghetto;
   import ghetto.Houses;
   import java.awt.*;

   import java.awt.event.*;

   import javax.swing.*;
   import javax.swing.event.DocumentEvent;
   import javax.swing.event.DocumentListener;

   import java.util.*;

public class Ghetto extends JFrame implements MouseListener, ActionListener,
    MouseMotionListener {
protected Grids theGrid;

Panel Panel1 = new Panel(); // A panel where the cells will be laid out
JTextField sizeInput;
JTextField redInput;
JTextField blueInput;
JTextField whiteInput;
//private LinkedList<Triangle> values;
JButton stepButton;
JButton startButton;
double value1, value2;
JPanel wherePanel; //
javax.swing.Timer time; // generates ticks that drive the animation
protected double deltaT = 0.020; // how much time for each tick

public static void main(String[] args) {
    new Ghetto();
}

public Ghetto() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    addMouseListener(this);
    addMouseMotionListener(this);
    setLayout(new FlowLayout());

    theGrid = new Grids(5, 0, 0);
    add(theGrid);

    startButton = new JButton("start");
    add(startButton);
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Create a timer
            time = new javax.swing.Timer((int) (1000 * deltaT), this);
            time.start();
            // Add a listener for the timer - which is the step method
            if (e.getSource() == time) 
            {
            //  Houses h = new Houses();
                //h.step();
                //Houses.step();
            }

        }
    });

    stepButton = new JButton("step");
    add(stepButton);
    stepButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            // step method
            if (e.getSource() == time) 
            {
                //step();
            }
            //repaint
            repaint();

        }
    });

    sizeInput = new JTextField("");
    sizeInput.setColumns(10);
    add(sizeInput, BorderLayout.WEST);
    sizeInput.addActionListener(this);

    redInput = new JTextField("");
    redInput.setColumns(10);
    add(redInput, BorderLayout.WEST);
    // redInput.addActionListener(this);

    blueInput = new JTextField("");
    blueInput.setColumns(10);
    add(blueInput, BorderLayout.WEST);
    // blueInput.addActionListener(this);

    whiteInput = new JTextField("");
    whiteInput.setColumns(10);
    add(whiteInput, BorderLayout.WEST);
    // whiteInput.addActionListener(this);


    setSize(new Dimension(550, 600));
    setVisible(true);
}

@Override
public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub

}


@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("ActionPerformed");

    try {
        // Convert txt into a number
        int size = Integer.parseInt(sizeInput.getText());
        int blue = Integer.parseInt(blueInput.getText()) ;
        int red = Integer.parseInt(redInput.getText()) ;
        // int vacant = Integer.parseInt(whiteInput.getText());
        System.out.println("Size = " + size);
        System.out.println("Red = " + red);
        System.out.println("Blue = " + blue);
        // System.out.println("White = " + vacant);

        // remove the old components
        this.remove(theGrid);
        this.remove(startButton);
        this.remove(stepButton);
        this.remove(sizeInput);
        this.remove(redInput);
        this.remove(blueInput);
        this.remove(whiteInput);

        // create the new grid and replace theGrid
        theGrid = new Grids(size, blue, red);

        // add all of the components
        this.add(theGrid);
        this.add(startButton);
        this.add(stepButton);
        this.add(sizeInput);
        this.add(redInput);
        this.add(blueInput);
        this.add(whiteInput);
        this.revalidate();
        this.repaint();
    } catch (NumberFormatException exc) {
        // sizeInput.setText("");
    }
}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


}

2 个答案:

答案 0 :(得分:0)

您的描述并未显示整个代码,但我相信您错过了在您的Ghetto课程中导入House课程,例如,

import com.yourprojectname.House;

它取决于你的包结构,这个语句通常是包声明后类中的第二个语句。 Java包中的其他示例可能是

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

答案 1 :(得分:0)

我知道这是一个非常古老的问题,但我会给那些在调用侦听器内部时遇到问题的人提供答案。你可能想制作自己的听众。

创建一个类,例如MyListener,并为您的按钮

button.addActionListener(new MyListener());

MyListener类应该实现ActionListener,你的IDE会给你实现的方法,你可以从这里轻松调用其他类。