0

我已经绘制了一个图表并在其中放置了一个 20*20 像素的框,我试图用一堆按钮来移动框。问题是,我似乎无法从动作侦听器中更改框的 X 轴和 Y 轴值。这是我的第一个图形应用程序,帮助!

GrapMain.java

import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextField;

public class GraphMain {

    public static void main(String [] args){

        Display nashDisplay = new Display();
        nashDisplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

图.java

import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Graph extends JPanel {
    int x = 5, y = 100;
    Graph (int x, int y){
        this.x = x;
        this.y = y;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        //horizontal lines
        g.setColor(Color.RED);
        for (int height=20; height<200; height+=20){
            g.drawLine(5, height, 480, height);
        }
        //verticle lines
        g.setColor(Color.RED);
        for (int height=5; height<400; height+=20){
            g.drawLine(height,200,height,10);
        }
        //the box
        g.setColor(Color.BLACK);
        g.fillRect(this.x, this.y, 20, 20);
        //g.fillRect((+20 to move right one box),(+20 to move down one box), 20, 20);
        //g.fillRect((-20 to move left one box),(-20 to move up one box), 20, 20);
    }
}

显示.java

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Display extends JFrame {

    int X = 5, Y = 100;
    private JButton left, right, up, down;
    Graph dude = new Graph(X, Y);

    public Display() {
        super("nash's graph(moving the box!)");

        JButton left = new JButton("move left");
        add(left, BorderLayout.WEST);
        left.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                X = X - 20;
            }
        });
        JButton right = new JButton("move right");
        add(right, BorderLayout.EAST);
        right.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                X = X + 20;
            }
        });
        JButton up = new JButton("move up");
        add(up, BorderLayout.NORTH);
        up.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Y = Y - 20;
            }
        });
        JButton down = new JButton("move down");
        add(down, BorderLayout.SOUTH);
        down.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Y = Y + 20;
            }
        });
        setSize(500, 300);
        setVisible(true);
        add(dude, BorderLayout.CENTER);
    }
}
4

1 回答 1

1

你需要 Torepaint在你 cangexyvalue 之后像这样:

 JButton left = new JButton("move left");
        add(left, BorderLayout.WEST);
        left.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        X = X - 20;
                        dude.x = X;
                        dude.y = Y;
                        repaint();
                    }
                });
        JButton right = new JButton("move right");
        add(right, BorderLayout.EAST);
        right.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        X = X + 20;
                        dude.x = X;
                        dude.y = Y;
                        repaint();
                    }
                });
        JButton up = new JButton("move up");
        add(up, BorderLayout.NORTH);
        up.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        Y = Y - 20;
                        dude.x = X;
                        dude.y = Y;
                        repaint();

                    }
                });
        JButton down = new JButton("move down");
        add(down, BorderLayout.SOUTH);
        down.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        Y = Y + 20;
                        dude.x = X;
                        dude.y = Y;
                        repaint();
                    }
                });
于 2013-04-13T12:13:27.210 回答