JAVA新建一个小型的窗体及组件(一)
package Frame; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Endeit extends JFrame { public Endeit() { super(); this.setTitle("文本密码框"); //创建容器 Container cont = getContentPane(); //关闭绝对布局 this.setLayout(null); //创建控件 JLabel jl1 = new JLabel("账号"); JLabel jl2 = new JLabel("密码"); JTextField jt = new JTextField("",20); JPasswordField jpf = new JPasswordField(); jpf.setEchoChar('*'); JButton jb1 = new JButton("登陆"); JButton jb2 = new JButton("重置"); //设置登陆按钮功能 jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub jt.setText(""); jpf.setText(""); JOptionPane.showMessageDialog(null, "登陆成功"); } }); //设置取消按钮功能 jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub jt.setText(""); jpf.setText(""); //设置焦点 jt.requestFocus(); } }); //将控件加入容器中 cont.add(jl1); cont.add(jl2); cont.add(jt); cont.add(jpf); cont.add(jb1); cont.add(jb2); //控件在容器中的位置及大小 jl1.setBounds(40, 20, 50, 20); jl2.setBounds(40, 50, 50, 20); jt.setBounds(90,20,200,20); jpf.setBounds(90,50,200,20); jb1.setBounds(110,90,60,20); jb2.setBounds(180,90,60,20); //窗体在电脑中的位置及大小 this.setBounds(500, 260, 360, 160); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗体不可以拉伸 this.setResizable(false); this.setVisible(true); } public static void main(String[] args) { new Endeit(); } }
刚学java的同学可以分享给你们