Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:30:13+00:00 2026-05-28T17:30:13+00:00

This is file 1 (my instructor prefer’s us to use separate files for each

  • 0

This is file 1 (my instructor prefer’s us to use separate files for each extension)

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:

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:

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. Can someone help me please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:30:14+00:00Added an answer on May 28, 2026 at 5:30 pm

    I’m not really sure I understand what this GUI should be doing, but this works to move the circle to the left. Points 3) & 4) of my comment still need to be addressed.

    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.
    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I self-instantiate a Java class? I should not use any file other
Using this file as source, I have a situation where I need to retrieve
Well this file was put in the repo by mistake and was deleted and
How can I create this file in a directory in windows 2003 SP2: .hgignore
I have this file file.txt which I want to split into many smaller ones.
I have this file structure: folderIWantStuffIn/ - old_stuff Now I want to add some
I have some code that effectively does this : File file = new File(C:\\Program
I have student.xml file and am parsing this file using SAX Parser and now
I have been told to edit this file in Sharepoint Designer: /_layouts/KWizCom_WikiPlus/CreateNew.aspx I found
I have a generated txt file. This file has certain lines that are superfluous,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.