java jlabel位置,Java Swing-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);

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.

解决方案

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:

Dlt1j.png

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值