gpt4 book ai didi

java - 使用按钮单击事件来更新java中的变量

转载 作者:行者123 更新时间:2023-12-01 14:40:44 26 4
gpt4 key购买 nike

我画了一个图表,并在其中放置了一个 20*20 像素的框,我试图用一堆按钮来移动该框。问题是,我似乎无法从 Action 监听器中更改框的 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);
}
}

最佳答案

更改 xy 值后,您需要重新绘制,如下所示:

 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();
}
});

关于java - 使用按钮单击事件来更新java中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com