Java Swing - JLabel 位置

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

Java Swing - JLabel Location

javauser-interfaceswingjpaneljlabel

提问by Yosi

I have problem while setting the Jlabel location.
I set the content pane to some JPanel, I created and tried to add my JLabel.

我在设置 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。

采纳答案by OscarRyz

Swing uses Layout Managersto 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:

你的窗户可能是这样的:

as simple as 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 );

    }
}

回答by Jonathan M Davis

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:

如果您不想使用布局管理器而只想自己放置所有内容(顺便说一句,这通常不是布局的最佳方式),然后添加:

getContentPane().setLayout(null);

回答by ZeBlob

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.htmlhttp://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 /使用.html