【问题标题】:Eclipse vs BlueJ issueEclipse 与 BlueJ 的问题
【发布时间】:2016-05-26 21:01:44
【问题描述】:

我在 BlueJ 中尝试了这段代码,它应该创建一个矩形并移动它,但它不起作用。然后我将完全相同的代码放入 Eclipse 中,它的功能与我想象的一样。为什么这在 Eclipse 中有效但在 BlueJ 中无效?

import javax.swing.*;
import java.awt.*;
public class Shapes
{
    public static void main(String[] args)
    {

         JFrame frame = new JFrame("Test");
         Draw object = new Draw(); 
         frame.add(object); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(400,400);
         frame.setVisible(true);
    }
}



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

public class Draw extends JPanel implements ActionListener, KeyListener
{
    Timer tm = new Timer(5,this);
    int x = 0, y = 0, velX = 0, velY = 0;

    public Draw()
    {
        tm.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false); 

    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(x,y,100,20);
    }

    public void actionPerformed(ActionEvent e)
    {
        x += velX;
        y += velY;
        repaint();
    }

    public void keyPressed(KeyEvent e)
    {
        int c = e.getKeyCode();

        if(c == KeyEvent.VK_LEFT)
        {
             velX = -1;
             velY = 0;
        }
        if(c == KeyEvent.VK_UP)
        {
             velX = 0;
             velY = 1;
        }
        if(c == KeyEvent.VK_RIGHT)
        {
             velX = 1;
             velY = 0;
        }
        if(c == KeyEvent.VK_DOWN)
        {
             velX = 0;
             velY = -1;
        }
    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e)
    {
        velX = 0;
        velY = 0;
    }
}

【问题讨论】:

    标签: java eclipse graphics bluej


    【解决方案1】:

    我明白你的意思,我相信它会将它附加到屏幕的那个角落,因为当我调整屏幕大小时,它会随着那个角落移动。我认为并不是 BlueJ 不能正确运行它,我相信它可以,但它的运行方式与 eclipse 的运行方式不同。 我相信this question 会问同样的问题,它会得到很好的答案,你应该先看看它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2013-09-18
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多