第1题
A.TextArea可以显示多行多列的文本
B.TextField可以显示多行多列的文本
C.在单行文本输入区中按下回车键会触发ActionEvent事件
D.单行和多行文本区都可以通过方法setEditable(Boolean)来设定只读属性
第2题
第3题
import java.awt.*;
import java.awt.event.*;
public class java2 extends java.applet.Applet imple-
ments hemListener,ActionListener
{Choice choice;
TextField text;
TextArea area;
Button add,del;
public void init() .
{choice:new Choice();
text=new TextField(8);
area:new TextArea(6,15);
choice.add("音乐天地");
choice.add("武术天地");
choice.add("象棋乐园");
choice.add("交友聊天");
add=new Button("添加");
del=new Button("删除");
add.addActionListener(this);
del.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);add(text);add(add);add(area);
}
public void itemStateChanged(hemEvent e)
{String name= ;
int index=choice.getSelectedIndex();
area.setText("\n"+index+":"+name);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =add||e.getSource()= =
text)
{String name=text.getText();
if(name.length()>;0)
{choice.add(name);
choice.select(name);
area.append("\n添加"+name);
}
}
else if(e.getSource()= =del)
{choice.remove( );
area.append("\n删除"+choice.getSelectedItem
());
}
}
}
第4题
tmport java.awt.event.*:
import java.awt.*;
import javax.swing.JOptionPane;
class Dwindow extends Frame. implements ActionLis-
tener
{TextField inputNumber;
TextArea show;
Dwindow(String s)
{super(s):
inputNumber=new TextField(22);
inputNumber.addActionListener(this);
show=new TextArea();
add(inputNumber,BorderLayout.NORTH);
add(show,BorderLayout.CENTER);
setBounds(60,60,300,300);setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{boolean boo=false;
if(e.getSource()= =inputNumber)
{String s= ;
char a[]=S.toCharArray();
for(int i=0;i<;a.length;i++)
{if(!(Character.isDigit(a[i])))
boo=true;
}
if(boo= =true)
{JOptionPane.showMessageDialog(this,"您输入
了非法字符","警告对话框",
);
inputNumber.setText(null);
}
else if(boo= =false)
{int number=Integer.parseInt(s);
show.append("\n"+number+"平方:"+(num-
bet*number)):
}
}
}
}
public class java2
{public static void main(String args[])
{ new Dwindow("java2");
}
}
第5题
第6题
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
程序运行结果如下:
import java.awt.*;
import java.awt.event.*;
public class ex15_2 extends Frame. implements ActionListener {
private Label 1;
private TextField tf;
public static void main(String[] args) {
ex15_2 obj15_2 = new ex15_2();
}
public ex15_2() {
setBackground(Color.gray);
l = new Label("Welcom to the NCR Examination!");
Font font = new Font("TimesRoman",Font.BOLD,20);
l.setFont(font);
add("Center",l);
Panel p = new Panel();
Button b = new Button("Left");
b.addActionListener(this);
p.add(b);
b = new Button("Center");
b.addActionListener(this);
p.add(b);
b = new Button("Right");
b.addActionListener(this);
p.add(b);
________________;
b = new Button("Exit");
b.addActionListener(this);
p.add(b);
p = new Panel();
tf = new TextField(40);
p.add(tf);
b = new Button("Set");
b.addActionListener(this);
p.add(b);
add("North",p);
setSize(500,300);
show();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Exit"))
System.exit(0);
else if(ae.getActionCommand().equals("Left"))
____________________;
else if(ae.getActionCommand().equals("Center")
l.setAlignment(Label.CENTER);
else if(ae.getActionCommand().equals("Right"))
l.setAlignment(Label.RIGHT);
else if(ae.getActionCommand().equals("Set"))
l.setText(tf.getText());
}
}
第7题
第9题
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
程序运行结果如下:
import java.awt.*;
import java.awt.event.*;
public class ex14_2 extends Frame. implements ItemListener {
private TextField tf;
private List 1;
public static void main(String[] arg) {
ex14_2 obj14_2 = new ex14_2();
}
public ex14_2() {
setBackground(Color. GRAY);
setTitle("ex14_2");
l = new List(5);
l.addItemListener(this);
l.addItem("Item1");
l.addItem("Item2");
l.addItem("Item3");
l.addItem("Item4");
l.addItem("Item5");
add("Center",l);
tf = new TextField(50);
add("South",tf);
______________;
show ( );
}
public void itemStateChanged(ItemEvent event)
tf.setText( );
}
}
第10题
A、源程序必须存放到MyTextArea.java文件中
B、在行2和行3之间应该增加一句:super(nrows,ncols)以至新构件有合适的大小
C、在行6processTextEvent()的返回值应声明为boolean而不是void.
D、在行7和8之间应增加一句:returntrue.
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!