简体   繁体   English

在 JFrame 上居中 JLabel

[英]Center JLabel On JFrame

I've been reading around lately on centering a JLabel in a JPanel.我最近一直在阅读有关在 JPanel 中居中 JLabel 的内容。 I've seen a lot of the following two answers:我已经看到了很多以下两个答案:

label.setHorizontalAlignment(JLabel.CENTER);

and in the creation of the JLabel并在创建 JLabel 时

static JLabel label = new JLabel("Some Text Here", SwingConstants.CENTER);

Neither of these options that I have tried have worked.我尝试过的这些选项都没有奏效。 All the text becomes left alligned with both of these answers (I've even tried using both of them at the same time, and no luck).所有文本都与这两个答案保持一致(我什至尝试过同时使用它们,但没有成功)。 Here is the full code for one of my labels with its instantiation and everything:这是我的一个标签及其实例化和所有内容的完整代码:

//JLabel title = new JLabel("Title", SwingConstants.CENTER);
JLabel title = new JLabel("Title");
title.setVisible(true);
title.setForeground(Color.BLACK);
title.setFont(new Font("Monospaced", Font.BOLD, 32));
//title.setHorizontalAlignment(JLabel.CENTER);
title.setLocation((int)width/2-190,10);
title.setSize(250,100);
frame.add(title);

I commented out the code that just left aligns it and is supposed to work.我注释掉了刚刚对齐它并且应该可以工作的代码。 For the set location, I'm using:对于设置位置,我正在使用:

static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
static double width = screenSize.getWidth();
static double height = screenSize.getHeight();

to define the screen's parameters, and then use that to keep the centering somewhat abstract.定义屏幕的参数,然后使用它来保持居中有点抽象。 However, obviously monitor size changes and 9 times out of 10, the code I have here for setLocation isn't going to center it on your screen.但是,很明显,监视器大小发生变化,10 次中有 9 次,我在此处为 setLocation 编写的代码不会将其置于屏幕中央。

I'm really curious to why this isn't working for me.我真的很好奇为什么这对我不起作用。 Is it because I'm not adding the JLabel to a JPanel?是因为我没有将 JLabel 添加到 JPanel 中吗? I was under the assumption that JLabel could lay over a JFrame in the same way it layed over a JPanel.我假设 JLabel 可以像放置在 JPanel 上一样放置在 JFrame 上。

EDIT编辑

I just also tried我刚刚也试过了

frame.add(title,BorderLayout.CENTER);

and had the same result as the other 2 options above.并且与上面的其他 2 个选项有相同的结果。

This works for me (NOTE: SwingConstants.CENTER, not SwingConstraints.CENTER): 这对我有用(注意:SwingConstants.CENTER,而不是SwingConstraints.CENTER):

   public static void main( String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         @Override
         public void run() {
            JFrame jf = new JFrame();
            jf.addWindowListener( new WindowAdapter() {
               @Override
               public void windowClosing( WindowEvent arg0 ) {
                  System.exit( 0 );
               }
            } );
            JLabel t = new JLabel( "Centered", SwingConstants.CENTER );
            jf.add( t, BorderLayout.CENTER );
            jf.setSize( 300, 300 );
            jf.setVisible( true );
         }
      } );
   }

Also: 也:

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class CenterDemo2 {
  public static void main(final String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         @Override
         public void run() {
            final JFrame frame = new JFrame("Test frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            final JLabel label = new JLabel("Test label");
            label.setBorder(new LineBorder(Color.BLUE, 2)); //Adding a border for clarity.

            //Most significant two lines of code:
            final JPanel containerPanel = new JPanel(new GridBagLayout());
            containerPanel.add(label);

            frame.getContentPane().add(containerPanel); //or: frame.setContentPane(containerPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });
   }
}  

The JLabel is in the (horizontal and vertical) center of the JPanel . JLabel位于JPanel的(水平和垂直)中心。

Screenshot, after dragging with the mouse to resize the frame: 使用鼠标拖动以调整框架大小后的屏幕截图:

截图

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

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