简体   繁体   English

忽略的界限

[英]ignored setbounds

I simply would like to place my JButton by the setBounds method. 我只是想通过setBounds方法放置我的JButton。 But whatever the parameters of this method, the button is on the middle at the top. 但是无论此方法的参数如何,按钮都位于顶部的中间。

This is my code : 这是我的代码:

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Panneau_fenetre1A extends JPanel                      
{ 
    JButton boutonOK = new JButton ("OK");


    public Panneau_fenetre1A(int Na)
    {
        boutonOK.setBounds(300,300,30,30);
        add(boutonOK);

    }

}

The placement of the button would also depend on the layout that you are currently using. 按钮的位置还取决于您当前使用的布局。 Here's a Vusual Guide to Layout to help you out decide on the best layout for your requirement. 这是版式实用指南,可帮助您确定最适合您需求的版式。

使用setLayout(null) ,它可以工作!

You are extending JPanel so the default constructor of JPanel is used and the default LayoutManager is, for whatever reason, the FlowLayout ! 您正在扩展JPanel,因此将使用JPanel的默认构造函数,并且无论出于何种原因,默认的LayoutManager都是FlowLayout! So if you really want to use NullLayout you could add this to your class: 因此,如果您真的想使用NullLayout,可以将其添加到您的类中:

  public Panneau_fenetre1A()
  {
    super(null);
  }

But seriously consider other Layout managers, for example the BorderLayoutManager (super(new BorderLayout())) is a good start. 但是请认真考虑其他布局管理器,例如BorderLayoutManager(super(new BorderLayout()))是一个好的开始。

When designing your dialog, remember that you can and should nest your layouts: one JPanel inside another JPanel (eg a GridLayout inside a BorderLayout). 设计对话框时,请记住您可以并且应该嵌套布局:一个JPanel位于另一个JPanel中(例如BorderLayout内的GridLayout)。 Please note: a 'good' dialog should resize properly, so that if the user resizes your Frame, you want to automatically extend your information objects such as your table, and not show large areas of JPanel background. 请注意:“好”对话框应该正确调整大小,以便用户调整框架的大小时,您想要自动扩展信息对象(例如表格),并且不显示JPanel背景的大区域。 That's something you cannot achieve with a NullLayout. 那是NullLayout无法实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM