如何将方法移动到第二个文件以从第一个Java Eclipse调用

如何将方法移动到第二个文件以从第一个Java Eclipse调用,java,eclipse,methods,move,Java,Eclipse,Methods,Move,我需要一些移动方法的帮助,以便在“客户机”(第一个文件)上有更少的代码。我希望能够调用这些方法,以便程序能够像现在这样工作。我试过自己做,但没法使它工作。感谢您的帮助 我希望移动的方法是paintComponent、PanelListener和PanelMotionListener 这是第一个文件 /******************/ /* IMPORT SECTION */ /******************/ import javax.swing.*; import java.awt

我需要一些移动方法的帮助,以便在“客户机”(第一个文件)上有更少的代码。我希望能够调用这些方法,以便程序能够像现在这样工作。我试过自己做,但没法使它工作。感谢您的帮助

我希望移动的方法是paintComponent、PanelListener和PanelMotionListener

这是第一个文件

/******************/
/* IMPORT SECTION */
/******************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**************/
/* MAIN CLASS */
/**************/
public class Prog2 extends JPanel
{
    private Prog2_Server c1, c2;
    private Prog2_Server selectedRect;
    private int x,y;

    public static void main(String[] args)
    {
        JFrame theGUI = new JFrame();
        theGUI.setTitle("GUI Example");
        theGUI.setSize(500,500);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Prog2 panel = new Prog2(Color.white);
        Container pane = theGUI.getContentPane();
        pane.add(panel);
        theGUI.setVisible(true);
    }

    public Prog2(Color backColor)
    {
        setBackground(backColor);
        c1 = new Prog2_Server(300,50,100,200, Color.red);
        c2 = new Prog2_Server(50,50,100,200, Color.blue);
        selectedRect = null;
        addMouseListener(new PanelListener());
        addMouseMotionListener(new PanelMotionListener());
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        c1.fill(g);
        c2.draw(g);
    }

    private class PanelListener extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            // Select a circle if it contains the mouse coords
            x= e.getX();
            y= e.getY();

            if(c1.containsPoint(x,y))
            {
                selectedRect = c1;
            }
            else if (c2.containsPoint(x,y))
            {
                selectedRect = c2;
            }
        }

        public void mouseReleased(MouseEvent e)
        {
            // deselect the selected rectangle
            x= e.getX();
            y= e.getY();
            selectedRect = null;
        }
    }

    private class PanelMotionListener extends MouseMotionAdapter
    {
        public void mouseDragged (MouseEvent e)
        {
            // compute the distance and move the selected circle
            int newX = e.getX();
            int newY = e.getY();
            int dx = newX - x;
            int dy = newY -y;

            if(selectedRect != null)
            {
                selectedRect.move(dx,dy);
            }

            x=newX;
            y=newY;
            repaint();
        }
    }
}
这是第二个文件

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

public class Prog2_Server extends JPanel
{
    private int width, length, xCoord, yCoord;
    private Color color;
    private Prog2_Server c1, c2;
    private Prog2_Server selectedRect;
    private int x,y;


    public Prog2_Server(int xC, int yC, int x, int y, Color c)
    {
        width = x;
        length = y;
        xCoord = xC;
        yCoord = yC;
        color = c;
    }

    public void draw(Graphics g)
    {
        Color oldColor = g.getColor();
        g.setColor(color);
        g.drawRect(xCoord,yCoord,width,length);
        g.setColor(oldColor);
    }

    public void fill(Graphics g)
    {
        Color oldColor = g.getColor();
        g.setColor(color);
        g.drawRect(xCoord,yCoord,width,length);
        g.setColor(oldColor);
    }

    public boolean containsPoint(int x, int y)
    {
        return ((xCoord <= x && x <= (xCoord + width)) && (yCoord <= y && y <= (yCoord + length)));
    }

    public void move(int xAmount, int yAmount)
    {
        xCoord = xCoord + xAmount;
        yCoord = yCoord + yAmount;
    }

}
import javax.swing.*;
导入java.awt.*;
公共类Prog2_服务器扩展了JPanel
{
专用整数宽度、长度、xCoord、yCoord;
私人色彩;
专用程序2_服务器c1、c2;
专用程序2_服务器选择删除;
私有整数x,y;
公共程序2_服务器(intxc、intyc、intx、inty、c色)
{
宽度=x;
长度=y;
xCoord=xC;
yCoord=yC;
颜色=c;
}
公共空间绘制(图g)
{
Color oldColor=g.getColor();
g、 设置颜色(颜色);
g、 drawRect(X坐标、Y坐标、宽度、长度);
g、 setColor(oldColor);
}
公共空隙填充(图g)
{
Color oldColor=g.getColor();
g、 设置颜色(颜色);
g、 drawRect(X坐标、Y坐标、宽度、长度);
g、 setColor(oldColor);
}
公共布尔containsPoint(int x,int y)
{

return((xCoord重构您的第一个类,用公共类替换私有类(在不同的文件中,每个类一个)。在“第一个”中创建适当的对象请注意,PanelListener和PanelMotionListener不是方法,而是类。您可以将它们移动到不同的文件中,但它们不再是私有类,您需要对代码进行一些更改。我不建议这样做


如果有太多的代码让您感到不适,Eclipse可能会让您隐藏它(每个方法或类的左边都有一个减号)。

下面是一个将PanelListener类的一部分移动到第二个文件的示例

在服务器中添加一个方法

public static Prog2_Server clientMousePressed (MouseEvent e, Prog2_Server c1, Prog2_Server c2)
{
    int x, y;
    // Select a circle if it contains the mouse coords
    x= e.getX();
    y= e.getY();

    if(c1.containsPoint(x,y))
    {
        return c1;
    }
    else if (c2.containsPoint(x,y))
    {
        return c2;
    }
    return null;
}
然后在客户机文件中,您可以调用服务器中声明的静态方法,如下所示:

private class PanelListener extends MouseAdapter
{
    public void mousePressed(MouseEvent e)
    {  
        selectedRect = Prog2_Server.clientMousePressed(e, c1, c2);
    }
//...   
}

不能将类拆分为多个文件。如果方法在该类中具有逻辑意义,则将其保留在该类中,或者创建一个新的实用程序/帮助程序类。