Java 如何对 JLabel 使用 setBounds 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21311617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:59:18  来源:igfitidea点击:

How to use setBounds method for a JLabel?

javaswingjlabellayout-manager

提问by user3227258

I'm making a window application with Swing. I am using the setBounds()method for the JLabelspacing but it's not working.

我正在使用Swing. 我正在使用间距的setBounds()方法,JLabel但它不起作用。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FullScreenJFrame extends JFrame
{
  public FullScreenJFrame( String title )
  {
      super(title);
      //JFrame frame = new JFrame();
      this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      setUndecorated(true);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      setBounds(0,0,screenSize.width, screenSize.height);
      getContentPane()
      .add(new JLabel("    HIGHCOURT OF JUDICATURE AT ALLHAHABAD"), BorderLayout.NORTH);
      JLabel label = new JLabel("JJ");
      label.setBounds(20, 20, 150, 20);

      // label.setText(s);
      add(label);
  }

  public static void main( String[] args )
  {
      FullScreenJFrame frame = new FullScreenJFrame("");
      //JFrame frame1 = new JFrame();
      //JLabel label = new JLabel("dd");
      //label.setBounds(370, 340, 150, 20);
      //frame1.add(label);
      frame.setVisible(true);
  }
}

采纳答案by Paul Samsotha

Are null layouts recommended? As other have suggested, the answer is

是否推荐空布局?正如其他人所建议的,答案是

NO

. 看Laying out Components in a Container在容器中布置组件以使用 LayoutManagers 进行一些练习



Will I still answer your question? Sure. Just so you know.

我还会回答你的问题吗?当然。只要你知道。

"I am using the setBounds() method for the JLabel spacing but it's not working. Please, can any one tell me why it is not working?"

“我正在将 setBounds() 方法用于 JLabel 间距,但它不起作用。请问有人能告诉我为什么它不起作用吗?”

Yes, your bounds aren't working because the JFramehas a default Borderlayout. In order for setBoundsto work, the layout needs to be null.

是的,您的边界不起作用,因为它JFrame有一个默认的Borderlayout. 为了setBounds工作,布局需要是null.

setLayout(null);

Also, keep in mind that when you douse a null layout, any components in which you don'tsetBoundsfor will not appear.

另外,请记住,当你使用空布局,在其中任何部件不会setBounds对将不会出现。



See Laying out Components in a Containerto get some practice in with LayoutManagers

请参阅在容器中布置组件以获得一些使用 LayoutManagers 的练习

回答by Maroun

Don'tuse setBounds()to resize a component. Use a Layout Managerand you won't be worried about manually doing this.

不要使用setBounds()调整大小组件。使用布局管理器,您就不必担心手动执行此操作。

回答by gasparms

One advice, It seems you are going to show your application using all screen so try to avoid the use of absolute positions like setBoundMethod.

一个建议,您似乎要使用所有屏幕显示您的应用程序,因此尽量避免使用绝对位置,如 setBoundMethod。

You should fit your application within a layout that uses other layouts within it. Check this link.

您应该将您的应用程序放入使用其他布局的布局中。检查此链接。

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

回答by ravibagul91

Why?

为什么?

Why are going for absolute positioning, when Layout Managersdo it for you.

为什么要进行绝对定位,当布局管理器为您做的时候。

One more thing, If your JFrame does not contain any title so there is no need to add empty title as it is bad practice.

还有一件事,如果您的 JFrame 不包含任何标题,则无需添加空标题,因为这是不好的做法。

Replace

代替

FullScreenJFrame frame = new FullScreenJFrame("");

By

经过

FullScreenJFrame frame = new FullScreenJFrame();

And

public FullScreenJFrame( String title )

By

经过

public FullScreenJFrame()

No need to call

不需要打电话

super(title);

回答by blob

Or in order to set your layout as null right click on your form and click set layout then click null layout.

或者为了将您的布局设置为空,右键单击您的表单并单击设置布局,然后单击空布局。