JTextField与JPasswordField控件
import javax.swing.*;import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.GridLayout;
class MyLayout extends JFrame{
private JPanel jp;
private JLabel jl;
private JTextField jt;
private JLabel jl1;
private JTextField jt1;
public static void main(String args[]){
MyLayout jf = new MyLayout();
jf.setVisible(true);
jf.setResizable(false);
}
MyLayout(){
setBounds(100,100,450,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new JPanel();
jl = new JLabel("username:");
jt = new JTextField();
jl1 = new JLabel("password:");
jt1 = new JPasswordField();
jt.setHorizontalAlignment(JTextField.CENTER);
jp.setLayout(new GridLayout(8,2,10,10));
jp.add(jl);
jp.add(jt);
jp.add(jl1);
jp.add(jt1);
setContentPane(jp);
}
}
效果图:
其中第23行代码我用的new 一个JPasswordField赋值给了JTextField,只是因为前者是继承了后者,这又叫向上转型,这是普遍的做法。
页:
[1]