简答题 Java
题号 #2246010

本题的功能是监听键盘键的敲击,并显示在窗口中。
import javax,.swing.*;
importjava.awt.*; ,
import java.awt.event.*;
public class java3 extends JFrame extends KeyListener
{
private String linel=""line2=""
private String line3=""
private JTextArea textArea;
public java3()
{
super("java3");
textArea=new JTextArea(10,15);
textArea.setText("Press any key on the key-
board…");
textArea.setEnabled(false);
addKeyListener(this);
getContentPane().add(textArea);
setSize(350,100);
show();
}
public void keyPressed(KeyEvent e)
{
linel="Key pressed:"+e.getKeyText(e.
getKeyCode());
setLines2and3(e);
}
public void keyReleased(KeyEvent e)
{
linel="Key released:"+e.getKeyText(e.
getKeyCode());
setLines2and3(e):
}
public void keyTyped(KeyEvent e)
{
Linel="Key typed:"+e.getKeychar();
setLines2and3(e);
}
private void setLines2and3(KeyEvent e)
{
line2="This key is"+(e.isActionKey()?""
:"not")+"an action key";
String temp=e.getKeyModifiersText(e.get-
Modifiers());
hne3="Modifier keys pressed:"+(temp.e-
quals("")?"none":temp);
textArea.setText(linel+"\n"+line2+"\n"
+line3+"\n");
}
public static void main(String args[])
{
java3 app=new java3();
addWindowListener(new Windowadapted()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}

进入在线模考

参考答案

暂无

答案解析

第1处:extends JFrame implements KeyListener
第2处:linel="Key typed:"+e.getKeyChar()
第3处:app.addWindowListener(new WindowAdapter())
【解析】第1处实现接口应用implements;第2处Java是大小写敏感的,获得键盘值应使用getKeyChar()方法;第 3处窗体级监听器应注册给接收类。

你可能感兴趣的试题

3 本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。
import java.awt.* ;
import java.awt.event.*;
import javax.swin9.*;
public class java3 extends JFrame implements ActionL-
istener{
public java3(){
setTitle("java3");
setSize(300,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JMenuBar mbar = new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu=new JMenu("File");
mbar.add(fileMenu);
connectltem=new JMenuhem("Connect");
connecthem.addActionListener(this);
fileMenu.add(connecthem);
exithem=new JMenuhem("Exit");
exithem.addActionListener(this);
fileMenu.add(exithem);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if(source= =connecthem){
Connectlnfo transfer=new ConnectInfo ("your-
name","pw");
if(dialog= =null)
dialog=new ConnectDialog(this);
if(dialog.showDialog(transfer)){
String uname=transfer.username;
String pwd=transfer.password;
Container contentPane=getContentPane();
contentPane.add(new JLabel("username="+
uname+",password="+pwd),"South");
validate();
}
}
else if(source= =exitltem)
System.exit(0);
} 
public static void main(String[]args){
JFrame f=new java3();
f.show();
}
private ConnectDialog dialog=null;
private JMenuhem connecthem;
private JMenuhem exithem;
}
class Connectlnfo{
public String username;
public String password;
public Connectlnfo(String U,String p){
username=u;password=P;
}
}
class ConnectDialog extends JDialog implements Ac-
tionListener{
public ConnectDialog(){
super(parent,"Connect",true);
Container contentPane=getContentPane();
JPanel pl=new JPanel();
pl.setLayout(new GridLayout(2,2));
pl.add(newJLabel("User name:"));
pl.add(username=new JTextField(""));
pl.add(new JLabel("Password:"));
pl.add(password=new JPasswordField(""));
contentPane.add("Center",pl);
Panel p2=new Panel();
okButton=addButton(p2,"ok");
cancelButton=addButton(p2。"Cancel");
contentPane.add("South",p2);
setSize(240,120);
}
JButton addButton(Container C,String name){
JButton button=new JButton(name);
button.addActionListener(this);
C.add(button);
return button;
}
public void actionPerformed(ActionEvent evt){
object source=evt.getSource();
if(source= =okButton){
ok=true:
setVisible(false);
}
else if(source= =cancelButton)
setVisible(false);
}
public void showDialog(Connectlnfo transfer){
username.setText(transfer.username);
password.setText(transfer.password);
ok=false;
show();
if(ok){
transfer.username=username.getText();
transfer.password=new String(password.get-
Password());
}
return ok;
}
private JTextField username ;
private JPasswordField password;
private boolean ok;
private JButton okButton; 
private JButton cancelButton;
}
找试题 · 找答案 · 找资料 在线模考