JButton.setBounds(x,y,w,h)似乎不起作用 [英] JButton.setBounds(x,y,w,h) doesn't seem to work
问题描述
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleExample extends JFrame {
public SimpleExample() {
setTitle("Simple example");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton jb = new JButton("TEST");
jb.setBorderPainted(true);
jb.setBounds(5, 5, 1, 1); ---> This line
add(jb);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SimpleExample ex = new SimpleExample();
ex.setVisible(true);
}
});
}
}
只需创建一个首选大小的简单按钮。 setBounds
方法似乎不起作用。我错在哪里?
Just creating a simple button of a preferred size. The setBounds
method doesn't seem to work. Where am I going wrong?
推荐答案
您的框架在布局管理器的控制之下,正在做出最好的决策布局组件并使用 setBounds
Your frame is under the control of a layout manager, it is making the decisions on how best to layout your components and is overriding the values you have specified using setBounds
覆盖您指定的值。现代GUI需要运行(即使是相同的操作系统)在各种不同的图形环境中,例如不同的DPI,屏幕尺寸和字体设置。
Modern GUIs need to run (even on the same OS) in a variety of different graphical environments, including different DPI, screen sizes and font settings for example.
布局管理器使你可以担心(更少)关于这些问题,强烈建议您使用它们
The layout manager makes it possible for you to worry (less) about these issues and it is highly recommended that you make use of them
看看
- Using Layout Managers
- A Visual Guide to Layout Managers
更多详情
这篇关于JButton.setBounds(x,y,w,h)似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!