JFrame类的小小问题
非常小的问题,为什么我调用JFrame类的时候,这个窗口出来的时候没有任何组件,然后随意调大或者调小窗口,组件就都出来了。很奇怪
类代码:public class Change_password extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
Userinfo uinfo;
JLabel Lpassword = new JLabel("原密码");
JLabel Lnew_password = new JLabel("新密码");
JLabel Lnew_password2 = new JLabel("新密码确认");
JTextField password = new JTextField();
JPasswordField new_password = new JPasswordField();
JPasswordField new_password2 = new JPasswordField();
JButton ok = new JButton("提交");
JButton back = new JButton("返回");
JButton qingkong = new JButton("清空");
Panel p;
int uid2;
public Change_password(int uid){
uid2 = uid;
p = new Panel(new GridLayout(3, 3));
p.add(Lpassword);
p.add(password);
p.add(ok);
p.add(Lnew_password);
p.add(new_password);
p.add(qingkong);
p.add(Lnew_password2);
p.add(new_password2);
p.add(back);
this.getContentPane().add(p,BorderLayout.CENTER);
ok.addActionListener(this);
back.addActionListener(this);
qingkong.addActionListener(this);
setVisible(true);
setSize(250,150);
//setLocationRelativeTo(this);
setDefaultCloseOperation(3);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()== ok){
//调用修改方法
}
if(e.getSource()== back){
setVisible(false);
uinfo = new Userinfo(uid2);
}
if(e.getSource() == qingkong){
password.setText(null);
new_password.setText(null);
new_password2.setText(null);
}
}
}
------解决方案--------------------
------解决方案--------------------可能是组件大小导致的问题,再加入所有组件之后,写一句
pack();
------解决方案--------------------修改后的程序(红色是修改后的地方,这两行颠倒了):
package com.jst;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Change_password extends JFrame implements ActionListener {
/**
*
*/
public static void main(String[] args) {
new Change_password(1);
}
private static final long serialVersionUID = 1L;
// Userinfo uinfo;
JLabel Lpassword = new JLabel("原密码");
JLabel Lnew_password = new JLabel("新密码");
JLabel Lnew_password2 = new JLabel("新密码确认");
JTextField password = new JTextField();
JPasswordField new_password = new JPasswordField();
JPasswordField new_password2 = new JPasswordField();
JButton ok = new JButton("提交");
JButton back = new JButton("返回");
JButton qingkong = new JButton("清空");
Panel p;
int uid2;
public Change_password(int uid) {
uid2 = uid;
p = new Panel(new GridLayout(3, 3));
p.add(Lpassword);
p.add(password);
p.add(ok);
p.add(Lnew_password);
p.add(new_password);
p.add(qingkong);
p.add(Lnew_password2);
p.add(new_password2);
p.add(back);
this.getContentPane().add(p, BorderLayout.CENTER);
ok.addActionListener(this);
back.addActionListener(this);