gpt4 book ai didi

java - 如何将圆圈移动到 "WEST"边框布局框?

转载 作者:行者123 更新时间:2023-11-29 06:43:15 28 4
gpt4 key购买 nike

这是文件 1(我的导师希望我们为每个扩展使用单独的文件)

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

public class Lab2 extends JFrame {

Lab2(){
setTitle("Lab 1b - Application #2");
Lab2Panel p = new Lab2Panel();
add(p);
}

public static void main(String[] args){

Lab2 frame = new Lab2();
frame.setTitle("Lab2 Application # 1");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 400);
frame.setVisible(true);
}

}

文件 2:

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

public class Lab2Panel extends JPanel{
Lab2Button canvas = new Lab2Button();
JPanel panel = new JPanel();

Lab2Panel () {

setLayout(new BorderLayout());

JButton leftButton = new JButton("left");
JButton rightButton = new JButton("right");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");

panel.add(leftButton);
panel.add(rightButton);
panel.add(upButton);
panel.add(downButton);

this.add(canvas, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);

leftButton.addActionListener(new Lab2MoveBallListener());
}


}

文件 3:

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

public class Lab2Button extends JPanel {
int radius = 5;

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius, 2 * radius, 2 * radius);

}

public void moveLeft(){

this.add(this, BorderLayout.WEST);
this.repaint();
}


}

Action 监听器代码:

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

class Lab2MoveBallListener implements ActionListener{


public void actionPerformed(ActionEvent e){
this.moveLeft();
}
}

当用户单击“左”按钮时,我试图将圆移到西边界布局。有人可以帮帮我吗。

最佳答案

我不太确定我理解这个 GUI 应该做什么,但这可以将圆圈向左移动。我的评论中的第 3) 点和第 4) 点仍然需要解决。

这是修改后的代码。

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

public class Lab2 extends JFrame {

Lab2(){
setTitle("Lab 1b - Application #2");
Lab2Panel p = new Lab2Panel();
add(p);
}

public static void main(String[] args){

Lab2 frame = new Lab2();
frame.setTitle("Lab2 Application # 1");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 400);
frame.setVisible(true);
}

}

class Lab2Panel extends JPanel{
Lab2Button canvas = new Lab2Button();
JPanel panel = new JPanel();

Lab2Panel () {

setLayout(new BorderLayout());

JButton leftButton = new JButton("left");
JButton rightButton = new JButton("right");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");

panel.add(leftButton);
panel.add(rightButton);
panel.add(upButton);
panel.add(downButton);

this.add(canvas, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);

leftButton.addActionListener(new Lab2MoveBallListener(canvas));
}


}

class Lab2Button extends JPanel {
int radius = 5;
int x = -1;
int y = -1;

protected void paintComponent(Graphics g){
if (x<0 || y<0) {
x = getWidth() / 2 - radius;
y = getHeight() / 2 - radius;
}
super.paintComponent(g);
g.drawOval(x,y, 2 * radius, 2 * radius);

}

public void moveLeft(){

//this.add(this, BorderLayout.WEST);
x -= 5;
this.repaint();
}


}

class Lab2MoveBallListener implements ActionListener{
private Lab2Button canvas;

Lab2MoveBallListener(Lab2Button canvas) {
this.canvas = canvas;
}

public void actionPerformed(ActionEvent e){
canvas.moveLeft();
}
}

..how do I differentiate between buttons in the action performed?

有很多方法。

  1. ActionEvent.getActionCommand()/getSource()使用 if/else 语句来选择正确的操作。
  2. 为每个按钮添加一个单独的监听器。这在现实世界的代码中更为常见。无需获取源代码或检查操作命令。

关于java - 如何将圆圈移动到 "WEST"边框布局框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073491/

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