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

标签 java events listener jbutton border-layout

几天来,我一直在为一个关于 GUI BorderLayout Jbutton 的 Java 初学者类(class)中必须做的项目而伤透脑筋,我真的希望这里有人可以帮助我理解它或提供一些启发。我的任务是创建一个 BorderLayout 窗口,其中左右上下有 4 个按钮 每个按钮都会将窗口/Borderlayout 向左或向右或向上或向下移动 20 像素。 我已经创建了带有按钮的代码,但我不知道如何使按钮移动,最重要的是我不能允许窗口从桌面移出/消失。请耐心等待我,我是一名新生。 这是到目前为止我的代码:

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

最佳答案

我在手机上,所以我不能(不会)输入太多代码,但我从另一个线程中复制了这段代码

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

在调用 Action 监听器时,可以通过这种方式手动设置JFrame的位置。

关于Java GUI BorderLayout 应用程序 4 个按钮 左、右、上、下每个移动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930116/

相关文章:

java - cublasSgemm与jcuda批量使用

java - 限制对java中类接口(interface)的访问

java - 在属性文件中写入属性而不破坏文件中的窗口路径

windows - 使用事件在 Windows 中观看、设置和获取事件语言

VBA静态类 "WithEvents"?

javascript - Colorthief.js 与 Polymer.js - 查找导致 onload 事件的图像的原色

java - 编辑 JTextPane 时按钮状态的更改

java - JSF 部署后立即开始类(class),而不是在 session 开始时开始

java - 如何防止轻量级组件接收鼠标事件?

java - 带有最终变量的 Android 内联监听器