JAVA第二次作业展示与学习心得

在这一次作业中,我学习了复选框,密码框两种新的组件,并通过一个邮箱登录界面将两种组件运用了起来。具体的使用方法和其他得组件并没有什么大的不同。
另外我通过查阅资料使用了一种新的布局方式------网格包布局.网格包布局管理是最复杂和灵活的布局管理,与网格布局管理器不同的是,网格包布局管理器允许容器中各个组件的大小各不相同,还允许组件跨越多个网格,也允许组件之间相互部分重叠。网格包布局理解为网格单元布局更合理,因为一个容器被划分为若干个网格单元,而每个组件放置在一个或多个网格单元中。要注意的是,网格包布局不能指定一个容器的网格单元的大小其网格单元的划分是通过weightx和weighty参数来设置的,但也不是直接指定其网格单元的大小。当把一个组件放置在网格单元中时,组件所占据的位置和大小是由一组与他们相关联的约束来决定的。这些约束是由GridBagConstraints类型的对象来设置的。
程序源代码:
import java.awt.;
import javax.swing.
;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class emailx extends JFrame {
private Checkbox rem_pwd;
private Checkbox not_show;
private Container c;
private ActionEvent e;
private ActionListener myActionLis;
private JButton loginBut;
private JButton exitBut;
private JButton register;
private JLabel label;// 标签
private JLabel label2;
private JLabel no_use;
private JPanel rem_not_show;
private JPanel log_cancel;
private JPasswordField password;// 密码框
private JTextField usernametext;// 文本框
// 按钮
public emailx() {
rem_not_show = new JPanel();//用于装记住密码与隐身的两个对象
log_cancel = new JPanel();//用于装登陆和取消的两个对象
rem_pwd = new Checkbox("记住密码");
not_show = new Checkbox("隐身登陆");
/网格包布局是这种,可以达到效果/
GridBagLayout gblayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setTitle("邮箱登陆界面");
// 窗口的主容器
final Container c = getContentPane();
c.setLayout(gblayout); // 让容器采用空布局
c.setBackground(Color.BLUE);
constraints.weightx = 0;
constraints.weighty = 0;
constraints.gridx = 1;

    label = new JLabel("账号:",JLabel.CENTER);
    gblayout.setConstraints(label, constraints);
    c.add(label);
    constraints.gridx = 2;
    usernametext = new JTextField(10);
    gblayout.setConstraints(usernametext, constraints);
    c.add(usernametext);
    constraints.gridx = 3;
    register = new JButton("注册");
    gblayout.setConstraints(register, constraints);
    c.add(register);
    constraints.gridx = 1;
    constraints.gridy = 2;
    label2 = new JLabel("密码:",JLabel.CENTER);
    gblayout.setConstraints(label2, constraints);
    c.add(label2);
    constraints.gridx = 2;
    password = new JPasswordField(10);
    gblayout.setConstraints(password, constraints);
    c.add(password);
    constraints.gridx = 2;
    constraints.gridy = 3;
    rem_not_show.add(rem_pwd);
    rem_not_show.add(not_show);
    rem_not_show.setBackground(c.getBackground());
    gblayout.setConstraints(rem_not_show, constraints);
    c.add(rem_not_show);
    
     exitBut = new JButton();
     exitBut.setText("删除");// 设置按钮值
     loginBut = new JButton("进入"); // 实例化组件
     log_cancel.add(loginBut);
     log_cancel.add(exitBut);
     constraints.gridx = 2;
    constraints.gridy = 4;
     gblayout.setConstraints(log_cancel, constraints);
     log_cancel.setBackground(c.getBackground());
    c.add(log_cancel);

    loginBut.setToolTipText("进入请点击该按钮!");
    // 给按钮注册监听器
    final myActionLis lis = new myActionLis();
    loginBut.addActionListener(lis);
    setSize(400, 300);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
    new emailx();
}

class myActionLis implements ActionListener {

    public void actionPerformed(final ActionEvent e) {
        // 获取文本框或者密码框的值(内容)
        final String name = usernametext.getText();
        final String pwd = password.getText();
        if (name.equals("") || pwd.equals("")) {
            // 弹出提示框
            JOptionPane.showMessageDialog(null, "账号或者密码不能为空!");
        } else {
            if (name.equals("123456789@qq.com") && pwd.equals("123456789")) {
                JOptionPane.showMessageDialog(null, "恭喜您!登录成功!");
            } else {
                JOptionPane.showMessageDialog(null, "账号或者密码错误!请重新输入!");
            }
        }
    }

}

}
程序运行效果:


posted on 2016-03-22 16:53  3班2014330320宋浪峰  阅读(188)  评论(0编辑  收藏  举报