简体   繁体   English

如何将圆圈移动到“ WEST”边框布局框?

[英]How do I move the circle to the “WEST” border layout frame?

This is file 1 (my instructor prefer's us to use separate files for each extension) 这是文件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);
    }

}

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


}

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


}

the action listener code: 动作侦听器代码:

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

class Lab2MoveBallListener implements ActionListener{


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

I am trying to move the circle to the WEST border layout when a user clicks the "left" button. 当用户单击“左”按钮时,我试图将圆移动到WEST边框布局。 Can someone help me please. 有人能帮助我吗。

I'm not really sure I understand what this GUI should be doing, but this works to move the circle to the left. 我不确定我是否知道此GUI应该做什么,但这可以将圆向左移动。 Points 3) & 4) of my comment still need to be addressed. 我的意见的第3点和第4点仍然需要解决。

Here is the altered code. 这是修改后的代码。

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? ..如何区分所执行动作中的按钮?

There are a number of ways. 有很多方法。

  1. ActionEvent.getActionCommand() / getSource() with if / else statements to select the correct action. ActionEvent.getActionCommand() / getSource()if / else语句一起选择正确的操作。
  2. Add a separate listener to each button. 将单独的侦听器添加到每个按钮。 This is more common in real world code. 这在现实世界的代码中更为常见。 No requirement for getting the source or inspecting the action command. 不需要获取源代码或检查操作命令。

I think you're confused with the "this" keyword. 我认为您对“ this”关键字感到困惑。 The following might get you closer to what you want. 以下内容可能使您更接近所需的内容。

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 is what i suggested, hope it works for you. 这是我建议的,希望它对您有用。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM