Java 如何将圆移动到“点”;“西方”;边框布局框架?

Java 如何将圆移动到“点”;“西方”;边框布局框架?,java,swing,layout,layout-manager,Java,Swing,Layout,Layout Manager,这是文件1(我的讲师希望我们对每个扩展名使用单独的文件) 文件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 BorderLa

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

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

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

我不确定我是否理解这个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();
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类Lab2扩展了JFrame{
Lab2(){
setTitle(“实验室1b-应用程序2”);
Lab2Panel p=新的Lab2Panel();
加(p);
}
公共静态void main(字符串[]args){
Lab2帧=新的Lab2();
frame.setTitle(“Lab2应用程序#1”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(200400);
frame.setVisible(true);
}
}
类Lab2Panel扩展了JPanel{
Lab2Button画布=新的Lab2Button();
JPanel面板=新的JPanel();
Lab2Panel(){
setLayout(新的BorderLayout());
JButton leftButton=新JButton(“左”);
JButton rightButton=新JButton(“右”);
JButton upButton=新JButton(“up”);
JButton downButton=新JButton(“down”);
panel.add(左键);
面板。添加(右键);
面板。添加(向上按钮);
面板。添加(向下按钮);
添加(画布、边框布局、中心);
此.add(面板,BorderLayout.SOUTH);
addActionListener(新的Lab2MoveBallListener(画布));
}
}
类Lab2Button扩展了JPanel{
int半径=5;
int x=-1;
int y=-1;
受保护组件(图形g){

如果(x我不确定我是否理解这个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();
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类Lab2扩展了JFrame{
Lab2(){
setTitle(“实验室1b-应用程序2”);
Lab2Panel p=新的Lab2Panel();
加(p);
}
公共静态void main(字符串[]args){
Lab2帧=新的Lab2();
frame.setTitle(“Lab2应用程序#1”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(200400);
frame.setVisible(true);
}
}
类Lab2Panel扩展了JPanel{
Lab2Button画布=新的Lab2Button();
JPanel面板=新的JPanel();
Lab2Panel(){
setLayout(新的BorderLayout());
JButton leftButton=新JButton(“左”);
JButton rightButton=新JButton(“右”);
JButton upButton=新JButton(“up”);
JButton downButton=新JButton(“down”);
panel.add(左键);
面板。添加(右键);
面板。添加(向上按钮);
面板。添加(向下按钮);
添加(画布、边框布局、中心);
此.add(面板,BorderLayout.SOUTH);
addActionListener(新的Lab2MoveBallListener(画布));
}
}
类Lab2Button扩展了JPanel{
int半径=5;
int x=-1;
int y=-1;
受保护组件(图形g){

如果(x我认为你被“this”关键字弄糊涂了。下面可能会让你更接近你想要的

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 ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
             Lab2Panel.this.remove(canvas);
             Lab2Panel.this.add(canvas, BorderLayout.WEST);
             Lab2Panel.this.repaint();
         }
      });
   }
}

我认为你对“this”关键字感到困惑。下面的内容可能会让你更接近你想要的

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 ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
             Lab2Panel.this.remove(canvas);
             Lab2Panel.this.add(canvas, BorderLayout.WEST);
             Lab2Panel.this.repaint();
         }
      });
   }
}

这是我的建议,希望对你有用

Lab2.java

import javax.swing.JFrame;


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(300, 400);
        frame.setVisible(true);
    }

}
Lab2Panel.java

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JPanel;


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(canvas));
        rightButton.addActionListener(new Lab2MoveBallListener(canvas));
        upButton.addActionListener(new Lab2MoveBallListener(canvas));
        downButton.addActionListener(new Lab2MoveBallListener(canvas));

    }

}
Lab2MoveBallListener.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Lab2MoveBallListener implements ActionListener
{
    private Lab2Button canvas;

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

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("left"))
        {
            canvas.moveLeft();
        }

        if (e.getActionCommand().equals("right"))
        {
            canvas.moveRight();
        }

        if (e.getActionCommand().equals("up"))
        {
            canvas.moveTop();           
        }

        if (e.getActionCommand().equals("down")) 
        {
            canvas.moveDown();
        }

    }

}
Lab2Button.java

import java.awt.Graphics;

import javax.swing.JPanel;


public 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()
    {
        x -= 5;
        this.repaint();
    }

    public void moveRight()
    {
        x += 5;
        this.repaint();
    }

    public void moveTop()
    {
        y -= 5;
        this.repaint();
    }

    public void moveDown()
    {
        y += 5;
        this.repaint();
    }

}
导入java.awt.Graphics;
导入javax.swing.JPanel;
公共类Lab2Button扩展了JPanel
{
int半径=5;
int x=-1;
int y=-1;
受保护组件(图形g)
{
if(x<0 | | y<0)
{
x=getWidth()/2-半径;
y=getHeight()/2-半径;
}
超级组件(g);
g、 绘图椭圆(x,y,2*半径,2*半径);
}
公共空间左移()
{
x-=5;
这个。重新绘制();
}
公权
{
x+=5;
这个。重新绘制();
}
公共void moveTop()
{
y-=5;
这个。重新绘制();
}
公共无效向下移动()
{
y+=5;
这个。重新绘制();
}
}

这是我的建议,希望对您有用

Lab2.java

import javax.swing.JFrame;


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(300, 400);
        frame.setVisible(true);
    }

}
Lab2Panel.java

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JPanel;


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(canvas));
        rightButton.addActionListener(new Lab2MoveBallListener(canvas));
        upButton.addActionListener(new Lab2MoveBallListener(canvas));
        downButton.addActionListener(new Lab2MoveBallListener(canvas));

    }

}
Lab2MoveBallListener.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Lab2MoveBallListener implements ActionListener
{
    private Lab2Button canvas;

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

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("left"))
        {
            canvas.moveLeft();
        }

        if (e.getActionCommand().equals("right"))
        {
            canvas.moveRight();
        }

        if (e.getActionCommand().equals("up"))
        {
            canvas.moveTop();           
        }

        if (e.getActionCommand().equals("down")) 
        {
            canvas.moveDown();
        }

    }

}
Lab2Button.java

import java.awt.Graphics;

import javax.swing.JPanel;


public 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()
    {
        x -= 5;
        this.repaint();
    }

    public void moveRight()
    {
        x += 5;
        this.repaint();
    }

    public void moveTop()
    {
        y -= 5;
        this.repaint();
    }

    public void moveDown()
    {
        y += 5;
        this.repaint();
    }

}
导入java.awt.Graphics;
导入javax.swing.JPanel;
公共类Lab2Button扩展了JPanel
{
int半径=5;
int x=-1;
int y=-1;
受保护组件(图形g)
{
if(x<0 | | y<0)
{
x=getWidth()/2-半径;
y=getHeight()/2-半径;
}
超级组件(g);
g、 绘图椭圆(x,y,2*半径,2*半径);
}
公共空间左移()
{
x-=5;
这个。重新绘制();
}
公权
{
x+=5;
这个。重新绘制();
}
公共void moveTop()
{
y-=5;
这个。重新绘制();
}
公共无效向下移动()
{
y+=5;
这个。重新绘制();
}
}

不扩展每个类而只扩展actionlistener背后的原因是什么?更喜欢组合而不是继承。有必要
实现
actionlistener
,其余的可以用其他方式完成。我想我不明白你的意思。到目前为止,在课堂上我们只创建了这样的类。当我们需要bu时t我们写一个类扩展JButton并调用该类的一个实例。“当我们需要一个按钮时,我们写一个类扩展JButton并调用该类的一个实例。”帮我一个忙好吗?打老师的头,告诉他们他们不适合教OOD(或任何需要它的代码).这件事值得一篇论文,你可能会在网上找到很多关于它的参考资料。如果你不能从你的