非常小的问题,为什么我调用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);
}
}
}
类代码: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);
qingkong.addActionListener(this);
setSize(250, 150);
setVisible(true);
// 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);
}
}
}