21

我想在 JFrame 中的特定坐标上放置一个 Jbutton 。我为 JPanel(我放置在 JFrame 上)设置了 setBounds,还为 JButton 设置了 setBounds。但是,它们似乎没有按预期运行。

我的输出:

替代文字

这是我的代码:

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Control extends JFrame {

    // JPanel
    JPanel pnlButton = new JPanel();
    // Buttons
    JButton btnAddFlight = new JButton("Add Flight");

    public Control() {
        // FlightInfo setbounds
        btnAddFlight.setBounds(60, 400, 220, 30);

        // JPanel bounds
        pnlButton.setBounds(800, 800, 200, 100);

        // Adding to JFrame
        pnlButton.add(btnAddFlight);
        add(pnlButton);

        // JFrame properties
        setSize(400, 400);
        setBackground(Color.BLACK);
        setTitle("Air Traffic Control");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Control();
    }
}

如何放置JButtonat坐标(0, 0)?

4

6 回答 6

23

在添加组件之前应调用以下行

pnlButton.setLayout(null);

上面会将您的内容面板设置为使用绝对布局。这意味着您必须始终使用setBounds方法显式设置组件的边界。

一般来说,我不建议使用绝对布局。

于 2010-07-07T14:29:05.033 回答
4

使用child.setLocation(0, 0)按钮和parent.setLayout(null)。不要在 JFrame 上使用 setBounds(...) 来调整它的大小,而是考虑使用 justsetSize(...)并让操作系统定位框架。

//JPanel
JPanel pnlButton = new JPanel();
//Buttons
JButton btnAddFlight = new JButton("Add Flight");

public Control() {

    //JFrame layout
    this.setLayout(null);

    //JPanel layout
    pnlButton.setLayout(null);

    //Adding to JFrame
    pnlButton.add(btnAddFlight);
    add(pnlButton);

    // postioning
    pnlButton.setLocation(0,0);
于 2010-07-07T14:28:29.623 回答
2

我已经想通了哈哈。对于按钮做 .setBounds(0, 0, 220, 30) .setBounds 布局是这样的 (int x, int y, int width, int height)

于 2013-04-21T22:15:58.227 回答
2

在某处定义 consts :

private static final int BUTTON_LOCATION_X = 300;  // location x 
private static final int BUTTON_LOCATION_Y = 50;   // location y 
private static final int BUTTON_SIZE_X = 140;      // size height
private static final int BUTTON_SIZE_Y = 50;       // size width

然后在下面:

                JButton startButton = new JButton("Click Me To Start!");
                // startButton.setBounds(300, 50,140, 50 );
                startButton.setBounds(BUTTON_LOCATION_X
                                    , BUTTON_LOCATION_Y,
                                      BUTTON_SIZE_X, 
                                      BUTTON_SIZE_Y );
                contentPane.add(startButton);

contentPane保存整个框架的Container 对象在哪里:

 JFrame frame = new JFrame("Some name goes here");
 Container contentPane = frame.getContentPane();

我希望这会有所帮助,对我很有用......

于 2013-08-10T07:34:40.403 回答
2

您应该先通过语法设置布局pnlButton.setLayout(),然后选择您想要的最合适的布局。例如:pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));。然后,将该 JButton 放入 JPanel。

于 2018-07-23T11:10:46.767 回答
0

首先,记住你的 JPanel 尺寸高度和尺寸宽度,然后观察:JButton 坐标为 (xo, yo, x length , y length)。如果你的窗口是 800x600,你只需要写:

JButton.setBounds(0, 500, 100, 100);

您只需要使用坐标间隙来表示按钮,并知道窗口在哪里结束以及窗口从哪里开始。

于 2013-10-26T04:43:55.773 回答