Java Swing - JLabel 位置

发布于 2024-09-10 19:53:18 字数 394 浏览 2 评论 0 原文

我在设置 Jlabel 位置时遇到问题。
我将内容窗格设置为某个 JPanel,我创建并尝试添加我的 JLabel。

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

我希望我的 JPanel 将位于我的应用程序的左上角,而我得到的是顶部中心的“SomeApp”。 (而不是左上角)。

顺便说一句,我尝试添加 JButton,但我无法更改 JButton 的宽度、高度、x、y。

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 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).

btw I tried to add JButton the and the I can`t change the width,height,x,y of the JButton.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-09-17 19:53:26

使用布局通常是一个更好的主意,因为它们允许动态调整组件的大小。以下是使用 BorderLayout 的方法:

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

如果您想在标签右侧添加一些内容,您可以使用它自己的布局创建一个附加面板:

// 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);

以下是一些开始使用布局的有用链接:

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

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:

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

红ご颜醉 2024-09-17 19:53:25

特定小部件在其容器中的最终位置取决于它所使用的布局管理器。布局管理器确定如何调整小部件的大小和排列小部件以使它们适当地适合。显然,内容窗格的默认布局决定顶部中心是放置 JLabel 的最佳位置。

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

getContentPane().setLayout(null);

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.

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);
酷炫老祖宗 2024-09-17 19:53:24

Swing 使用 布局管理器来放置组件。

您必须了解它们的工作原理才能有效地使用它们。您可以将布局管理器设置为 null,并自行进行布局,但不推荐,因为您必须每次都跟踪新组件,并在窗口移动、缩小等时自行执行布局计算。

布局管理器是一开始有点难以理解。

您的窗口可能是这样的:

就这么简单

使用此代码:

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

    }
}

Swing uses Layout Managers to place the components.

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.

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文