有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在尝试定位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);
}
}

共 (1) 个答案

  1. # 1 楼答案

    首先,不要在从左到右的流程中使用FlowLayout()类FlowLayout组件,使布局为空

    最后,对于按钮,您可以使用

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