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");
}
}