简体   繁体   English

Java Swing - JLabel位置

[英]Java Swing - JLabel Location

I have problem while setting the Jlabel location. 设置Jlabel位置时遇到问题。
I set the content pane to some JPanel, I created and tried to add my JLabel. 我将内容窗格设置为某个JPanel,我创建并尝试添加我的JLabel。

    JLabel mainTitle = new JLabel("SomeApp");
    mainTitle.setFont(new Font("Arial",2 , 28));
    mainTitle.setBounds(0,0, 115, 130);
    getContentPane().add(mainTitle);

I want that my JPanel will be on the top left corner on my application and what I am getting is "SomeApp" on the top center.(and not top left). 我希望我的JPanel将位于我的应用程序的左上角,我得到的是顶部中心的“SomeApp”。(而不是左上角)。

btw I tried to add JButton the and the I can`t change the width,height,x,y of the JButton. 顺便说一句,我试图添加JButton,我不能改变JButton的宽度,高度,x,y。

Swing uses Layout Managers to place the components. Swing使用布局管理器来放置组件。

You have to understand how they work to use them effectively. 您必须了解他们如何有效地使用它们。 You can set the layout manager to null, and do the layout your self, but is not recommendable because you'll have to keep track of new components each time, and perform layout computation your self when the window moves shrink etc. 您可以将布局管理器设置为null,并自行进行布局,但不建议这样做,因为您每次都必须跟踪新组件,并在窗口移动缩小时执行自己的布局计算等。

Layout managers are a bit hard to grasp at first. 布局管理员起初有点难以掌握。

Your windows could be like this: 你的窗户可能是这样的:

就这么简单

Using this code: 使用此代码:

import javax.swing.*;
import java.awt.Font;
import java.awt.FlowLayout;

class JLabelLocation  {

    public static void main( String [] args ) {

        JLabel mainTitle = new JLabel("SomeApp");
        mainTitle.setFont(new Font("Arial",2 , 28));
        //mainTitle.setBounds(0,0, 115, 130); //let the layout do the work

        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));// places at the left
        panel.add( mainTitle );

        frame.add( panel );// no need to call getContentPane
        frame.pack();
        frame.setVisible( true );

    }
}

Where a particular widget ends up in its container depends on the layout manager that it's using. 特定小部件在其容器中结束的位置取决于它正在使用的布局管理器。 The layout manager determines how to resize and arrange the widgets to make them fit appropriately. 布局管理器确定如何调整窗口小部件的大小和排列窗口以使它们适当地匹配。 Obviously, the default layout for the content pane decided that the top center was the best place to put the JLabel. 显然,内容窗格的默认布局决定了顶部中心是放置JLabel的最佳位置。

If you want to get to not use a layout manager and just place everything yourself (which generally isn't the best way to lay things out btw), then add: 如果你想不使用布局管理器而只是自己放置一切(这通常不是btw布局的最好方法),那么添加:

getContentPane().setLayout(null);

Using layouts is usually a better idea since they allow for dynamic resizing of components. 使用布局通常是一个更好的主意,因为它们允许动态调整组件大小。 Here's how you'd do it with a BorderLayout: 以下是使用BorderLayout的方法:

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add (new JLabel ("Main title"), BorderLayout.NORTH);

If you want to add something to the right of the label you could create an additionnal panel with it's own layout : 如果您想在标签的右侧添加一些内容,您可以使用它自己的布局创建一个额外的面板:

// Create a panel at the top for the title and anything else you might need   
JPanel titlePanel = new JPanel (new BorderLayout());
titlePanel.add(new JLabel ("Main title"), BorderLayout.WEST);

// Add the title panel to the frame
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(titlePanel, BorderLayout.CENTER);

Here are some usefull links to get started with layouts: 以下是一些有用的布局入门链接:

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/visual.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/visual.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout /using.html

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

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