Java GUI BorderLayout应用程序4个按钮每个移动位置左,右,上,下

时间:2015-04-28 21:18:30

标签: java events listener jbutton border-layout

我在Java初学者课程中关于GUI BorderLayout Jbutton的一个项目已经让我头疼了几天,我真的希望这里的一些人可以帮助我理解它或者一些启发。我的任务是创建一个BorderLayout窗口,上下左右有4个按钮 每个按钮向左或向右或向上或向下移动窗口/ Borderlayout 20像素。 我已经用按钮创建了一个代码,但我不知道如何使按钮移动,最重要的是我不能让Window从桌面移出/消失。请耐心等待我,我是一个非常新鲜的学生。 到目前为止,这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

我在我的手机上,所以我不能(不会)输入很多代码,但我将其复制出另一个线程

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

您可以在调用动作侦听器时以这种方式手动设置JFrame的位置。

答案 1 :(得分: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);

      }


    }