gpt4 book ai didi

java - 是否可以使用坐标来定位 JButton?

转载 作者:行者123 更新时间:2023-12-01 10:24:48 25 4
gpt4 key购买 nike

我一直在尝试定位 JButton,但遇到了很多麻烦。我需要它们处于非常精确的位置,但不知道如何实现这一点。我见过一个叫做 FlowLayout 的东西,但我对它很困惑。几天前我问了另一个关于如何定位 JButton 的问题,他们给了我这个固定代码:

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

public class Frame extends JFrame
{
private JButton button1;
private JButton button2;

public Frame()
{
button1 = new JButton("Hello button1");
button2 = new JButton("Hello button2");
button2.setPreferredSize(new Dimension(100,100));
button1.setPreferredSize(new Dimension(100,100));
add(button2);
add(button1);
}

}

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JPanel
{
public static void main(String args [])
{
Frame frame = new Frame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}

最佳答案

首先,不要在从左到右的流程中使用 FlowLayout() 类 FlowLayout 组件,使 Layout(null) 。

最后对于按钮你可以使用这个

button1.setBounds(XPosition, YPosition, WidthOFButton, HeightOFButton);

关于java - 是否可以使用坐标来定位 JButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400889/

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