变量值与编程值不同

时间:2018-11-14 18:35:44

标签: java text netbeans jtextfield

如果在这里出现致命的编程错误,请原谅我。我是编码/编程的新手。这是我第一次做OOP。 欢迎所有评论和建议

我想创建一个Java GUI,可以在其中使用键盘箭头移动形状并在JTextField中显示形状的坐标

所以我创建了一个包move,其中包含2个Java类 move.java和second.java

下面是move.java

package move;

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


public class Move{


public static void main(String[] args) {

JFrame f = new JFrame();
second s = new second();
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,600);

    // TODO code application logic here
}

}

这是Second.java

package move;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;


public class second extends JPanel implements ActionListener, KeyListener, MouseListener {

private TextField tfX;
private TextField tfY;
private TextField tfMouseClickX;
private TextField tfMouseClickY;

Timer t = new Timer(10,this);
double x = 50, y = 50, velx = 0, vely = 0;

public second(){
    add(new Label("X Position"));
    tfX = new TextField(10);
    tfX.setEditable(false);
    add(tfX);

    add(new Label("Y Position"));
    tfY = new TextField(10);
    tfY.setEditable(false);
    add(tfY);

    add(new Label("X-Click: "));
    tfMouseClickX = new TextField(10);
    tfMouseClickX.setEditable(false);
    add(tfMouseClickX);
    add(new Label("Y-Click: "));
    tfMouseClickY = new TextField(10);
    tfMouseClickY.setEditable(false);
    add(tfMouseClickY);

    t.start();
    addMouseListener(this);
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.RED);
    g2.fill(new Ellipse2D.Double(x,y,50,50));
}

public void actionPerformed(ActionEvent e){
    repaint();
    x += velx;
    y += vely;
}

    public void keyPressed(KeyEvent e){

    int code = e.getKeyCode();
    if (code == KeyEvent.VK_UP){
        vely = -5;
        velx = 0;
        tfX.setText(Double.toString(x));
        tfY.setText(Double.toString(y));

    }
    else if (code == KeyEvent.VK_DOWN){
        vely = 5;
        velx = 0;
        tfX.setText(Double.toString(x));
        tfY.setText(Double.toString(y));
    }
    else if (code == KeyEvent.VK_LEFT){
        velx = -5;
        vely = 0;
        tfX.setText(Double.toString(x));
        tfY.setText(Double.toString(y));
    }
    else if (code == KeyEvent.VK_RIGHT){
        velx = 5;
        vely = 0;
        tfX.setText(Double.toString(x));
        tfY.setText(Double.toString(y));
    }

}
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){
    velx = 0;
    vely = 0;
    }

@Override
public void mouseClicked(MouseEvent e) {
    tfMouseClickX.setText(e.getX() + "");
    tfMouseClickY.setText(e.getY() + "");
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}
}

我为每个动作的坐标分配了5或-5的值。但是在TextField上,它不会显示我编程的内容,该值会不断变化。

1 个答案:

答案 0 :(得分:0)

您必须按住Tab键,然后使用键盘上的箭头移动形状。