日期:2014-05-20  浏览次数:20746 次

jlabel小问题
为啥子第一个标签和最后一个标签重合了呢?我的坐标设的没错啊?

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

public class Enrol extends JFrame implements ActionListener {

private JLabel title, user_name, user_number, user_academe, user_major, user_class;
private JTextField tname, tnumber, tacademe, tmajor, tclass;
private JButton add, cancel;
private Container cp;
private ResultSet rset;
private JProgressBar progressBar;

public Enrol() {

super("添加信息");

Font font = new Font("TimersRoman", Font.ITALIC, 40);

cp = getContentPane();
cp.setLayout(null);

title = new JLabel("新用户注册");

user_name = new JLabel("姓名:");
user_name.setFont(new Font("幼圆", 2, 18));


user_number = new JLabel("学号:");
user_number.setFont(new Font("幼圆", 2, 18));

user_academe = new JLabel("所属学院:");
user_academe.setFont(new Font("幼圆", 2, 18));

user_major = new JLabel("所修专业:");
user_major.setFont(new Font("幼圆", 2, 16));

user_class = new JLabel("所在年级:");
user_class.setFont(new Font("幼圆", 2, 18));

add = new JButton("添加");
cancel = new JButton("取消");
add.addActionListener(this);
cancel.addActionListener(this);

add.setFont(new Font("幼圆", 2, 18));
cancel.setFont(new Font("幼圆", 2, 18));

tname = new JTextField(15);
tnumber = new JTextField(15);
tacademe = new JTextField(15);
tmajor = new JTextField(15);
tclass = new JTextField(15);

title.setFont(font);
title.setBounds(130, 20, 300, 60);

add.setBounds(150, 400, 75, 25);
cancel.setBounds(250, 400, 75, 25);

user_name.setBounds(50, 100, 75, 25); //此处
user_number.setBounds(50, 150, 75, 25);
user_academe.setBounds(50, 200, 75, 25);
user_major.setBounds(50, 250, 75, 25);
user_class.setBounds(50, 300, 75, 25); //与user_name标签重合,不知道为什么?????

tname.setBounds(150, 100, 250, 25);
tnumber.setBounds(150, 150, 250, 25);
tacademe.setBounds(150, 200, 250, 25);
tmajor.setBounds(150, 250, 250, 25);
tclass.setBounds(150, 300, 250, 25);

cp.add(title);

cp.add(tname);
cp.add(user_name);

cp.add(tnumber);
cp.add(user_number);

cp.add(tacademe);
cp.add(user_academe);

cp.add(tmajor);
cp.add(user_major);


cp.add(tclass);
cp.add(user_class);

cp.add(add);
cp.add(cancel);


ImageIcon img = new ImageIcon("E:\\Test.jpg");

JLabel imgLabel = new JLabel(img);//将背景图放在标签里。

//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));  
imgLabel.setBounds(0,0,600, 450);//设置背景标签的位置  

this.add(imgLabel);

//cp.validate();
add.setBounds(150, 400, 75, 25);
cancel.setBounds(250, 400, 75, 25);

user_class.setBounds(50, 100, 75, 25);

this.setSize(500,500);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

public static void main(String[] args){
new Enrol();
}
}

------解决方案--------------------
user_class.setBounds(50, 100, 75, 25);

这个语句在this。setSize()上面出现了!!去掉就好了。。。。。。。。。。。。