如何使用 Java 在 JFrame 中的所需位置放置 JButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3195666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to place a JButton at a desired location in a JFrame using Java
提问by Haxed
I want to put a Jbutton on a particular coordinatein a JFrame. I put setBounds for the JPanel (which I placed on the JFrame) and also setBounds for the JButton. However, they dont seem to function as expected.
我想在 JFrame 中的特定坐标上放置一个 Jbutton 。我为 JPanel(我放置在 JFrame 上)设置了 setBounds,并为 JButton 设置了 setBounds。但是,它们似乎没有按预期运行。
My Output:
我的输出:
This is my code:
这是我的代码:
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();
}
}
How can place the JButton
at coordinate (0, 0)?
如何放置JButton
at 坐标 (0, 0)?
采纳答案by Eugene Ryzhikov
Following line should be called before you add your component
在添加组件之前应调用以下行
pnlButton.setLayout(null);
Above will set your content panel to use absolute layout. This means you'd always have to set your component's bounds explicitly by using setBounds
method.
以上将设置您的内容面板使用绝对布局。这意味着您始终必须使用setBounds
方法显式设置组件的边界。
In general I wouldn't recommend using absolute layout.
一般来说,我不建议使用绝对布局。
回答by Chadwick
Use child.setLocation(0, 0)
on the button, and parent.setLayout(null)
. Instead of using setBounds(...) on the JFrame to size it, consider using just setSize(...)
and letting the OS position the frame.
child.setLocation(0, 0)
在按钮上使用,和parent.setLayout(null)
。与其在 JFrame 上使用 setBounds(...) 来调整它的大小,不如考虑使用setSize(...)
并让操作系统定位框架。
//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);
回答by Conor Watt
I have figured it out lol. for the button do .setBounds(0, 0, 220, 30) The .setBounds layout is like this (int x, int y, int width, int height)
我已经想通了,哈哈。对于按钮 do .setBounds(0, 0, 220, 30) .setBounds 布局是这样的 (int x, int y, int width, int height)
回答by JAN
Define somewhere the consts :
在某处定义 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
and then below :
然后在下面:
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);
where contentPane
is the Container
object that holds the entire frame :
包含整个框架contentPane
的Container
对象在哪里:
JFrame frame = new JFrame("Some name goes here");
Container contentPane = frame.getContentPane();
I hope this helps , works great for me ...
我希望这有帮助,对我很有用......
回答by Carlos Xavier
First, remember your JPanel size height and size width, then observe: JButton coordinates is (xo, yo, x length , y length). If your window is 800x600, you just need to write:
首先,记住你的 JPanel 尺寸高度和尺寸宽度,然后观察:JButton 坐标是 (xo, yo, x length , y length)。如果你的窗口是 800x600,你只需要写:
JButton.setBounds(0, 500, 100, 100);
You just need to use a coordinate gap to represent the button, and know where the window ends and where the window begins.
您只需要使用坐标间隙来表示按钮,并知道窗口在哪里结束,窗口从哪里开始。
回答by Daniel Tremblay
You should set layout first by syntax pnlButton.setLayout()
, and then choose the most suitable layout which u want. Ex: pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
. And then, take that JButton into JPanel.
您应该首先通过语法设置布局pnlButton.setLayout()
,然后选择您想要的最合适的布局。例如:pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
。然后,将该 JButton 放入 JPanel。