在這一次做業中,我學習了複選框,密碼框兩種新的組件,並經過一個郵箱登陸界面將兩種組件運用了起來。具體的使用方法和其餘得組件並無什麼大的不一樣。
另外我經過查閱資料使用了一種新的佈局方式------網格包佈局.網格包佈局管理是最複雜和靈活的佈局管理,與網格佈局管理器不一樣的是,網格包佈局管理器容許容器中各個組件的大小各不相同,還容許組件跨越多個網格,也容許組件之間相互部分重疊。網格包佈局理解爲網格單元佈局更合理,由於一個容器被劃分爲若干個網格單元,而每一個組件放置在一個或多個網格單元中。要注意的是,網格包佈局不能指定一個容器的網格單元的大小其網格單元的劃分是經過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;java
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, "帳號或者密碼錯誤!請從新輸入!"); } } } }
}
程序運行效果:
學習