几天来,我一直在为一个关于 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/