四、綜合應用題(共18分)
本程序的功能是獲取文本框中的文本。窗口中有兩個文本框“用戶名”和“密碼”,以及三個按鈕“登錄”、“其他用戶登錄,,和“關閉”,初始狀態(tài)“用戶名”文本框是只讀的,單擊“其他用戶登錄”按鈕后變成可寫的,“密碼”文本框使用的不是密碼文本框,在用戶鍵入的時候設置顯示為*號。輸入用戶名和密碼后,單擊“登錄”按鈕后,如果輸入的密碼為空,則彈出提示消息框,否則后臺將顯示輸入的用戶名和密碼。比如顯示為“admin用戶的密碼:password”(admi為輸入的用戶名,password為輸入密碼)。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
import javax.swing.JOptionPane;
public class java3
{
public static void main(String args[])
{
final Frame frmFrame=new Frame();
Panel pnlPanel=new Panel();
Label lblUsername=new Label("用戶名");
Label lblPassword=new Label("密碼");
final TextField txtUsername=new TextField("
Student");
final TextField txtPassword=new TextFidd("",
8);
txtUsername.setEditable(false);
txtPassword.setChar(’*’);
Button btnButtonl=new Button("登錄");
ButtOn btnButton2=new Button("其他用戶登
錄");
Button btnButton3=new Button("關閉");
btnButtonl.a(chǎn)ddActionListener(new ActionListen-
er()
{
public void actionPerformed(ActionEvent e)
{
if((txtPassword.getText()).length()= =0)
{
JOptionPane.showMessageDialog(frmFrame,"密
碼不能為空");
return;
}
txtPassword.setColumns(16);
System.out.println(txtUsername.getText()+"
用戶的密碼:"
+txtPassword.getPassword());
}
});
btnButton2.a(chǎn)ddActionListener(new ActionListen-
er()
{
public void actionPerformed(ActionEvent e)
{
txtUsername.setEnable(true);
}
});
btnButton3.a(chǎn)ddActionListener(new ActionListen-
er()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
pnlPanel.a(chǎn)dd(1blUsername);
pnlPanel.a(chǎn)dd(txtUsername);
pnlPanel.a(chǎn)dd(1blPassword);
pnlPanel.a(chǎn)dd(txtPassword);
pnlPanel.a(chǎn)dd(btnButtonl);
pnlPanel.a(chǎn)dd(btnButton2);
pnlPanel.a(chǎn)dd(btnButton3);
frmFrame.a(chǎn)dd(pnlPanel);
frmFrame.setTitle("advance");
frmFrame.pack();
frmFrame.show();
}
}
四、綜合應用題 第1處:txtPassWord.setEchoCharf(’*’) 第2處:txtPassword.getText() 第3處:txtUsername.setEditable(true) 【解析】第一處是通過TextField類的setEchoChar函數(shù)設置用戶輸入時,文本框顯示的文本。第二處是通過TextField類的getText函數(shù)獲取用戶的輸入,即得到密碼值。第三處是使用戶名文本框變?yōu)榭捎?,使用戶可以輸入?
四、綜合應用題 第1處:txtPassWord.setEchoCharf(’*’) 第2處:txtPassword.getText() 第3處:txtUsername.setEditable(true) 【解析】第一處是通過TextField類的setEchoChar函數(shù)設置用戶輸入時,文本框顯示的文本。第二處是通過TextField類的getText函數(shù)獲取用戶的輸入,即得到密碼值。第三處是使用戶名文本框變?yōu)榭捎?,使用戶可以輸入?

