Java Swing-JLabel位置 [英] Java Swing - JLabel Location

查看:1069
本文介绍了Java Swing-JLabel位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置Jlabel位置时出现问题.
我将内容窗格设置为某些JPanel,创建并尝试添加我的JLabel.

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

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

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

顺便说一句,我试图添加JButton,但是我无法更改JButton的宽度,高度,x,y.

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

推荐答案

Swing使用

Swing uses Layout Managers to place the components.

您必须了解它们如何有效地使用它们.您可以将布局管理器设置为null,然后自己进行布局,但不建议这样做,因为您每次必须跟踪新组件,并在窗口移动缩小等时自行进行布局计算.

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:

使用此代码:

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

    }
}

这篇关于Java Swing-JLabel位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆