这是文件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();
        }


}


动作侦听器代码:

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

class Lab2MoveBallListener implements ActionListener{


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


当用户单击“左”按钮时,我试图将圆移动到WEST边框布局。有谁可以帮助我吗。

最佳答案

我不确定我是否知道此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();
}
}





  ..如何区分所执行动作中的按钮?


有很多方法。


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

07-24 21:42