是这样的,弄了个登录的窗口,外面有JFrame 其上有个JPanel, JPanel有张图片,有用户名、密码等文本域和两个按钮,
当我把JFrame跟JPanel的大小设成一样后,显示不出panel上的文本域 背景图片等
当我把JFrame的大小改到很大后却可以看到那些组件都在靠近中间的位置,可是,真正能够输入文本信息的却是在左上角...
如图:
虽然显示出来了,但真正的文本域等其实是在左上角画红线处。郁闷...
下面是源代码,哪位大虾能帮帮我吗? 感激不尽!
public class LoginFrame extends JFrame { 
final BackP jBPanel = new BackP(260, 170);
/**构造登录窗体
 * Create the frame.
 */
public LoginFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);                            // 不允许改变大小 
getContentPane().add(jBPanel, java.awt.BorderLayout.CENTER);
pack();
setTitle("登录系统");
setSize(650,670);                                  // 窗体大小
Toolkit toolkit = getToolkit();                    // 获得Toolkit对象
Dimension dimension = toolkit.getScreenSize();     // 获得Dimension对象
int screenHeight = dimension.height;               // 获得屏幕的高度
int screenWidth = dimension.width;                 // 获得屏幕的宽度
int frm_Height = this.getHeight();                 // 获得窗体的高度
int frm_width = this.getWidth();                   // 获得窗体的宽度
this.setLocation((screenWidth - frm_width) / 2,
(screenHeight - frm_Height) / 2);          // 使用窗体居中显示
setAlwaysOnTop(true);
} /**
 * 隐藏系统登录窗体
 */
private void hideLoginFrame(){
this.setVisible(false);
}

/**
 * 背景面板类
 */
private class BackP extends JPanel
{
private JButton btn_login;
private JButton btn_exit;
private JTextField text_user;
private JPasswordField text_pwd;
private JLabel l_user;
private JLabel l_pwd; 
Image image;
int x,y;
/**
 * 面板构造方法
 * @param x 宽度
 * @param y 高度
 */
public BackP(int x,int y)
{
URL url = BackP.class.getResource("/res/login.jpg");            // 获得图片的URL 
image = new ImageIcon(url).getImage();                      // 创建图象对象
            setLayout(null);  
            add(getl_user());
add(getl_pwd());
add(gettext_user());
add(gettext_pwd());
add(getBtn_enter());
add(getBtn_exit()); 
this.x=x;                   
this.y=y;
}

/**
 * 设置图象宽度
 * @param x 宽度
 */
public void setX(int x) {
this.x = x;
}

/**
 * 获得图象宽度
 * @return
 */
public int getX() {
return x;
}

/**
 * 设置图象高度
 * @param y
 */
public void setY(int y) {
this.y=y;
}

/**
 * 获得图象高度
 * @return
 */
public int getY() {
return y;
}

/**
 * 重写paintComponent方法
 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
 */
protected void paintComponent(Graphics g) {
super.paintComponent(g);                 // 调用父类的方法
Graphics2D g2 = (Graphics2D) g;          // 创建Graphics2D对象
g2.drawImage(image, 0, 0, x, y, this);   // 绘制图象
}
 

/**
 * 
 */
/*
protected JComboBox getc_type()
 {
String[] typ={"前台护士","医生","药房人员","管理员"};
if(c_type==null)
{
c_type=new JComboBox();
c_type.setName("c_type");
c_type.setSelectedItem(typ);
c_type.setBounds(74,80,50,25);
}
return c_type;
 }

protected JLabel getl_type()
{
if (l_type==null) 
{
l_type= new JLabel();
l_type.setText("身份:");
l_type.setBounds(24, 80, 50, 20);
}
return l_type;
}
*/
/**
 * @return
 */
protected JLabel getl_user()
{
if (l_user==null) 
{
l_user= new JLabel();
l_user.setText("用户名:");
l_user.setBounds(24, 10, 50, 20);
}
return l_user;
}
/**
 * @return
 */
protected JLabel getl_pwd() 
{
if (l_pwd== null) 
{
l_pwd= new JLabel();
l_pwd.setText("密码:");
l_pwd.setBounds(24, 50, 50, 20);
}
return l_pwd;
}
/**
 * @return
 */
protected JTextField gettext_user()
{
if (text_user==null) 
{
text_user=new JTextField();
text_user.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
text_userActionPerformed(evt);       // 调用方法响应事件操作
}
});
text_user.setName("text_user");
text_user.setBounds(74, 10, 150, 25);
}
return text_user;
}
/**
 * @return
 */
protected JPasswordField gettext_pwd() 
{
if (text_pwd==null) {
text_pwd=new JPasswordField();
text_pwd.addActionListener(new ActionListener() 
{
public void actionPerformed(final ActionEvent evt) 
{
text_pwdActionPerformed(evt);       // 调用方法响应事件操作
}
});
text_pwd.setName("text_pwd");
text_pwd.setBounds(74, 50, 150, 25);
}
return text_pwd;
}
/**
 * @return
 */
protected JButton getBtn_enter() 
{
if (btn_login==null) {
btn_login=new JButton();
btn_login.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
btn_loginActionPerformed(evt);          // 调用方法响应事件操作
}
});
btn_login.setName("btn_login");
btn_login.setText("登  录");
btn_login.setBounds(44, 110, 70, 23);
}
return btn_login;
}
/**
 * @return
 */
protected JButton getBtn_exit() 
{
if (btn_exit == null) {
btn_exit = new JButton();
btn_exit.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
System.exit(0);                     // 退出系统
}
});
btn_exit.setName("btn_exit");
btn_exit.setText("退  出");
btn_exit.setBounds(134, 110, 70, 23);
}
return btn_exit;
}
/**
 * 系统登录
 * @param evt 动作事件
 */
private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) 
{
Connection conn = null;            
Statement st = null;                                                 // Statement对象
ResultSet rs = null;                                                 // 结果集对象
try {
conn = DaoConn.getConnection();                                  // 建立数据连接
st = conn.createStatement();                                     // 创建Statement对象
String user = text_user.getText().trim();                        // 用户名
String pwd = new String(text_pwd.getPassword()).trim();          // 密码
String sql = "select * from tb_doctor where d_user='" + user
+ "' and d_password='" + pwd + "'";                 
rs = st.executeQuery(sql);                                       // 执行查询语句
if (rs.next() && rs.getString("d_user") != null)
{             // 用户名和密码正确                  // 用户权限
// SaveUserInfo.setUserName(user);                              // 保存用户名到公共类
//SaveUserInfo.setPassword(pwd);                               // 保存密码到公共类
//SaveUserInfo.setQuanXian(quanxian);                          // 保存用户权限到公共类
//MainDaohangFrame.launch();                                   // 启动主窗体
hideLoginFrame();                                       // 隐藏登录窗体
}
else 
{
JOptionPane.showMessageDialog(this, "用户名或密码不正确!!!");
text_user.setText("");        // 清空文本框
text_pwd.setText("");         // 清空密码框
text_user.requestFocus();     // 使用文本框获得输入焦点
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "数据库异常",
JOptionPane.INFORMATION_MESSAGE);       // 显示消息框
}
finally {
try {
   conn.close();                             // 关闭数据库连接
     }
catch (Exception e) 
{

}
}
}

/**
 * 用户名文本框的动作事件
 * @param evt 动作事件
 */
private void text_userActionPerformed(java.awt.event.ActionEvent evt) 
{
text_pwd.requestFocus();                            // 密码框获得输入焦点
}

/**
 * 密码框的动作事件
 * @param evt 动作事件
 */
private void text_pwdActionPerformed(java.awt.event.ActionEvent evt) 
{
btn_loginActionPerformed(evt);             // 调用登录按钮的动作事件
} }
 

}有点长...  麻烦各位啦...

解决方案 »

  1.   

    代码太长了,不高兴看了,首先把其他组件例如Button放到Panel里,Panel放入Frame,然后
    Frame的布局用BorderLayout
      

  2.   

    呵呵,大家好!我是猎头cindy,我们公司是一家专门focus在IT行业的猎头公司,目前有高级JAVA开发工程师,软件架构师和系统分析工程师的职位,感兴趣的朋友可以加我的msn:[email protected],我们详细沟通,呵呵,不考虑也可以加我的,我们保持联系:-)
      

  3.   

    有几个问题需要改过来:
    1、应当采用箱式布局
        getContentPane().setLayout(new GridBagLayout());2、控件应当放在面板类中
        final BackP jBPanel = new BackP(260, 170);
        你实际上为这个面板设置了固定大小。3、在将面板放到JFrame的容器当中的时候,需要设置自动填充属性。4、对于图片的处理,你需要重新画出来吗?如果不是,需要用图片加载类加载已有图片。