1

I have been breaking my head for days over a project I have to do in my Java beginner class about GUI BorderLayout Jbutton and I really hope some one here can help me out to understand it or shed some light. My task is to create a BorderLayout window with 4 button left right up and down Each button moves the window/ Borderlayout 20 pixel left or right or up or down. I have already created a code with the buttons but I do not to know how to make the buttons move and above all I must not allow the Window to move out/ disappear from the desktop. Please be patient with me I am totally fresh student. Here is my code so far:

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

public class WindowBorder extends JFrame {

    private static final long serialVersionUID = 1L;
    private int x, y; //the coordinates for moving in the screen 

    public WindowBorder (String titel){
        super (titel);
        //create the buttons and the layout and add the buttons 
        JButton right = new JButton ("Right"); 
        JButton left = new JButton ("Left"); 
        JButton up = new JButton ("Up"); 
        JButton down = new JButton ("Down"); 
        //JButton center = new JButton ("Default"); hide the middle button 

        setLayout (new BorderLayout (75,75));
        add(BorderLayout.EAST,right);
        add(BorderLayout.WEST,left);
        add(BorderLayout.NORTH,up);
        add(BorderLayout.SOUTH,down);

        //add(BorderLayout.CENTER,default); hide the middle button 

        //I must create the inner class with the constructors for the task project for school 
        class WindowBorderInner implements ActionListener {
            @Override
            public void actionPerformed (ActionEvent e){

                if(e.getActionCommand().equals("right"))
                //this is the part that I am lost :(
            }
        }

        //configuration the size and the location of the Border layout 
        setSize (400,400);

        setLocationByPlatform (true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String [] arg){ //the test method 
        new WindowBorder("Move Window");
        }
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
Elivan K.
  • 285
  • 1
  • 3
  • 13

2 Answers2

0

I'm on my mobile so I can't(won't) type out much code, but I copied this out of another thread

jFrame.setLocation(jFrame.getX() + 5, jFrame.getY());

You can manually set the location of the JFrame in this way when the action listener is called.

0
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width); int y = (screenSize.height);

class ListenerInnerClass implements ActionListener {
    @Override
    public void actionPerformed (ActionEvent e){
        if (e.getActionCommand ().equals("Right"))
            Toolkit.getDefaultToolkit().getScreenSize();
            setLocation (+20,y);
    }
}



public MoveBorderDemo (String titel){
    super (titel);
    //create the buttons and the layout and add the buttons 
    JButton Right = new JButton ("Right"); 
    JButton Left = new JButton ("Left"); 
    JButton Up = new JButton ("Up"); 
    JButton Down = new JButton ("Down"); 

    Right.addActionListener(new ListenerInnerClass());
    setLayout (new BorderLayout (75,75));

    add(BorderLayout.EAST,Right);
    add(BorderLayout.WEST,Left);
    add(BorderLayout.NORTH,Up);
    add(BorderLayout.SOUTH,Down);

    //configuration the size and the location of the Border layout 
    setSize (400,400);

    //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();


    setLocation(200,200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


public static void main(String [] arg){ //the test method 
    new MoveBorderDemo ("Move Window");
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    System.out.println("Screen width = " + d.width);
    System.out.println("Screen height = " + d.height);

      }


    }
Elivan K.
  • 285
  • 1
  • 3
  • 13