无法在Java中创建类的多个实例

时间:2015-10-14 22:50:27

标签: java class jframe instances

我正在尝试在屏幕上创建随机移动的多个字符(正方形)。我已经创建了一个CharMove类来创建一个正方形,并在屏幕上随机移动它。但是,我尝试在单独的java文件中创建此类的多个实例,并且只创建了一个实例。有什么问题?

CharMove课程:

public class CharMove extends JPanel {
    public static int x = 250;
    public static int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public static void movement(int x, int y, JFrame frame) { 
        CharMove.x = x; 
                CharMove.y = y;
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                CharMove.x = Getx(CharMove.x,frame); 
                CharMove.y = Gety(CharMove.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

世界级

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(100,100,game); 
    char2.movement(250,250,game);
}

3 个答案:

答案 0 :(得分:2)

  

但是,我尝试在单独的java文件中创建此类的多个实例,并且只创建了一个实例。

不,你正在创建多个实例。但是,这并没有任何区别,因为您没有任何每个实例的状态。您唯一的字段是:

public static int x = 250;
public static int y = 250;

这些是静态字段,这意味着它们与该类的任何特定实例无关。您可能只想从声明中删除static关键字。 (我还会将这些字段设为私有字体,并在必要时提供公共获取者/制定者,但这是另一回事。)

您还需要将静态方法转换为实例方法 - 因为它们意味着要对各个实例执行操作,对吧?基本上,我认为你应该通过你用来学习Java的书/教程来修改static的含义。 (还修改了Java命名约定。)

答案 1 :(得分:0)

你不应该使用# Asks user how much change they are trying to give, returns the coins to make that change coin = int(input("How much change are you trying to give (in cents)? ")) change = "" while coin >= 25: quarters = coin // 25 coin = coin % 25 if quarters > 0: change += str(quarters) + " quarters " if coin >= 10: dimes = coin // 10 coin = coin % 10 change += str(dimes) + " dimes " if coin >= 5: nickels = coin // 5 coin = coin % 5 change += str(nickels) + " nickel " pennies = coin // 1 coin %= 1 if pennies > 0: change += str(pennies) + " pennies " print (change) ,因为它不是实例方法(名字说它,它是静态的)。事实上,您的代码不能在public static void movement()进行编译。 应该将其声明为实例方法char1.movement(100,100,game);。实际上,对于该方法的其余部分,您可能也希望以这种方式执行此操作。静态工作没有类的实例。

答案 2 :(得分:0)

您的x和y不是实例变量,它们是静态变量。所以CharMove的每个实例都共享相同的x和y

试试这个,

public class CharMove extends JPanel {
    private int x = 250;
    private int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public void movement(JFrame frame) { 
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                this.x = CharMove.Getx(this.x,frame); 
                this.y = CharMove.Gety(this.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(game); 
    char2.movement(game)
}