填空题

本程序的功能是获取文本框中的文本。窗口中有两个文本框“用户名”和“密码”,以及三个按钮“登录”、“其他用户登录”和“关闭”,初始状态“用户名”文本框是只读的,单击“其他用户登录”按钮后变成可写的,“密码”文本框使用的不是密码文本框,在用户键入的时候没置显示为*号。输入用户名和密码后,单击“登录”按钮后,如果输入的密码为空,则弹出提示消息框,否则后台将显示输入的用户名和密码。比如,显示为“admin用户的密码:password”(admin为输入的用户名,password为输入密码)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class java3

public static void main(String args[])

final Frame frmFrame=new Frame();
Panel pnlPanel=new Panel();
Lrabel lblUsername=new Label("用户名");
Label lblPassword=new Label("密码");
final TextField txtUsername=new TextField("Student")
final TextField txtPassword=new TextField("",8);
txtUsername.setEditable(false);
txtPassWOrd.setChar(’*’)
Button btnButtonl=new Button("登录");
Button btnButton2=new Button("其他用户登录");
Button btnButton3=new Button("关闭");
btnButtonl.addActionListener(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.addActionListener(new ActionListen-er()

public void actionPerformed(ActionEvent e)

txtUsername.setEnable(true);

);
btnButton3.addActionListener(new ActionListen-er()

public void actionPerformed(ActionEvent e)

System.exit(0);

);
pnlPanel.add(1blUsername);
pnlPanel.add(txlUsername);
pnlPanel.add(1blPassword);
pnlPanel.add(txtPassword);
pnlPanel.add(btnButtonl);
pnlPanel.add(btnButton2);
pnlPanel.add(btnButton3);
frmFrame.add(pnlPanel);
frmFrame.setTitle("advance");
frmFrame.pack();
frmFrame.show();

【参考答案】

第1处:txlPassword.setEchoChat"(’*’)
第2处:txtPassword.getTe......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

填空题
本题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif和“WindoWS“,单击任何一个按钮,就能将窗口的风格改变为按钮名称所对应的风格。 import java.awt*; import java.awt.event.*; import javax.swing.*; class PlafPanel extends JPanel implements Actionlis-tener public______() metalButton=new JButton( Metal ); motifButton=new JButton( Motif ); windowsButton=new JButton( Windows ); add(metalButton); add(motifButton); add(windowsButton); metalButlon.addActionListener(this); motifButton.addActionListener(this); windowsButton.addActionListener(this); public void actionPerformed(ActionEvent evt) Object source=evt.getSource(); string plaf= ; if(source==metaIButton) pIaF= javax.swing.plaf.metal.MetalLookAncl-Feel ; else if(source==motifButton) plaf= com.sun.java.swing.plaf.motif.Moti-fLookAndFeel ; else if(source==windowsButton) plaf= com.sun.java.swing.plaf.windows.windowsLookAndFeel ; try UIManager.setLookAndFeel(______); SwingUtilities.updateComponentTreeUI(ttlis); catch(Exception e) private JButton metalButton; private JButton motifButton; private JButton windowsButton; class PlafFrame extends JFrame public PlafFrame() setTitle( simple ); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(new PlafPanel()); public class java2 public static Void main(String[]args) JFrame frame=new PIafFrame(); frame.show();