gpt4 book ai didi

java - 有没有更简单的方法在 Eclipse 中定位 JFrame 组件?

转载 作者:行者123 更新时间:2023-12-02 03:42:49 24 4
gpt4 key购买 nike

我正在使用 eclipse
我对 java 有点陌生..我制作了一个按钮、一个文本字段和一个标签,并将它们放入 JPanel 中 - 然后将 JPanel 放在 JFrame 上..

很难正确定位它们,因为我必须更改它们三个的 x,y,宽度,高度,如果它不在应该的位置,我必须更改 x,y 并编译并运行并执行重来一遍,直到到达正确的位置..
有没有更简单的方法来定位这些东西或为我生成坐标的软件?

对不起我的英语

最佳答案

使用布局管理器。

例如:

BorderLayout

enter image description here

A picture of a GUI that uses BorderLayout Every content pane isinitialized to use a BorderLayout. (As Using Top-Level Containersexplains, the content pane is the main container in all frames,applets, and dialogs.) A BorderLayout places components in up to fiveareas: top, bottom, left, right, and center. All extra space is placedin the center area. Tool bars that are created using JToolBar must becreated within a BorderLayout container, if you want to be able todrag and drop the bars away from their starting positions. For furtherdetails, see How to Use BorderLayout.

...//Container pane = aFrame.getContentPane()...
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);

//Make the center component big, since that's the
//typical usage of BorderLayout.
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);

button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);

button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);

button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);

查看完整的文档:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

关于java - 有没有更简单的方法在 Eclipse 中定位 JFrame 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36651327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com