如何将 JButton 放置在 JFrame 中的某个坐标处

发布于 2024-09-08 05:32:58 字数 754 浏览 2 评论 0原文

我想知道如何将 JButtons 放置在 JFrame 中的特定坐标处。一整天我都在看布局。这不符合我的目的。我更喜欢像 setBounds 这样的东西。有传言说它不起作用,但 setLocation 可以。我尝试过,但是程序忽略了 setLocation 行并将其设置为布局。


代码


import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;

public class SwingUI extends JFrame {

public SwingUI() {
    JFrame frm = new JFrame("OmegaZ");
    JButton btn = new JButton("ClickMe");
    frm.getContentPane().add(btn, BorderLayout.NORTH);
    frm.setSize(400, 400);
    frm.setVisible(true);
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    btn.setLocation(100, 200);
}

public static void main(String[] args) {

    new SwingUI();
}
}

任何帮助表示赞赏。

非常感谢

I want to know how to place JButtons at a particular coordinate in the JFrame. All day I have seen layouts. This does not suit my purpose. I would prefer something like setBounds. Rumour has it that it does not work but setLocation does. I tried it but, the program disregards the setLocation line and sets it to a Layout.


CODE


import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;

public class SwingUI extends JFrame {

public SwingUI() {
    JFrame frm = new JFrame("OmegaZ");
    JButton btn = new JButton("ClickMe");
    frm.getContentPane().add(btn, BorderLayout.NORTH);
    frm.setSize(400, 400);
    frm.setVisible(true);
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    btn.setLocation(100, 200);
}

public static void main(String[] args) {

    new SwingUI();
}
}

Any help is appreciated.

Many Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-09-15 05:32:58

您可以使用空布局进行绝对定位。在这种情况下,您将完成所有工作。

You can do absolute positioning with a null layout. You do all the work in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文