此题为判断题(对,错)。
第1题:
下面是一个Applet程序,其功能是接收用户输入的两个整数,比较它们的大小,并在用户按下“比较大小”按钮后,将 Applet中显示的“请先输入两个待比较的整数”,改为“两个整数中最大值是:x”,x是两个数中的最大值。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code=LookForMax width=800 height=400>
</applet>
*/
public class LookForMax extends Applet implements ActionListener{
Label result ;
TextField inl,in2;
Button btn;
int a=0,b=0,max=0;
public void init()
{
result=new Label ("请先输入两个待比较得整数");
in1=new TextField(5);
in2=new TextField(5);
btn=new Button("比较大小");
add(in1);
add(in2);
add(btn);
add(result);
btn.addActionListener(supper);
}
public void actionPerformed(ActionEvent e){
a=Integer.parseInt(in1);
b=Integer.parseInt(in2);
if(a>b)
max=a;
else
max=b;
result, setText ( "两个数中最大值是:"+max);
}
}
LookFormax.html:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code="LookForMax.class" width=800 height=400>
</applet>
</body>
</html>
第2题:
下面是一个Applet程序,其功能是根据公式:y=a*sin(x)绘制正弦曲线。要求窗口中有一个文本区作为曲线峰值a的输入,可以判断输入的数字是否符合要求,一个ok按钮,点击则在画布中绘制曲线,以画布中的横坐标值作为sin()的参数x。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.lang.Math.*;
/*
<applet code="ex18_3.class" width=800 height=400 >
</applet>
*/
public class ex18_3 extends Applet implements ActionListener {
Panel pane=new Panel();
drawWnd dw;
Label 11 = new Label("峰值");
TextField tf1 = new TextField(15);
Button btn = new Button("OK");
private float a=0;
public void init(){
pane.setLayout(new FlowLayout(FlowLayout.CENTER,25,5));
pane.add(11);
pane.add(tf1);
pane.add(btn);
btn.addActionListener(this);
add("North",pane);
dw=new drawWnd();
add("South",dw);
}
class drawWnd extends Canvas {
drawWnd() {
setSize(300,100);
setBackground(Color. GRAY);
}
public void paint(Graphics g) {
g.setColor(Color.GREEN);
for(int i=0;i<getSize().width-1;i++){
int y=0;
int y1=y+(int) (Math.sin(i)*a);
int y2=y1+(int) (a*Math.sin(i+1));
g.drawLine(i,y1,i+1,y2);
}
}
}
public void actionPerformed(ActionEvent ae) {
try{
a=Integer.parseInt(tf1.getText());
dw.repaint();
}
catch(NumberFormatException nfe){
tf1.setText("error!");
}
}
}
ex18_3.html
<HTML>
<HEAD>
<TITLE>ex18_3</TITLE>
</HEAD>
<BODY>
<applet code="ex18_3.class" width=800 height=400 >
</applet>
</BODY>
</HTML>
第3题:
请完成下列Java程序:记录鼠标在窗口中的信息,包括鼠标的移动事件以及鼠标在窗口中的位置坐标,要求在窗口下方显示鼠标的事件和位置信息,在窗口上方有按钮控制程序的正常退出。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.event.*;
public class ex12_2
extends Frame. implements MouseMotionListener, ActionListener
private Label 1;
private String str=" ";
public static void main(String args[])
{
new ex12_2();
}
ex12_2( )
{
______;
setSize(200,200);
Button btn=new Button("exit");
btn.addActionListener (this);
add (btn, "North" );
l=new Label(str);
add (l, "South" );
show ();
}
public void mouseMoved(MouseEvent event)
{
str="Move: "+
l. setText (str);
}
public void mouseDragged(MouseEvent event)
{
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("exit"))
{
System.exit(0);
}
}
}
第4题:
下列程序使用CardLayout管理了2张卡片,每张都是一个Panel,每个Panel有一个Button,单击按钮,显示下一张卡片中的内容。请将程序补充完整。
注意:不改动程序结构,不得增行或删行。
import java.awt.*;
import java.awt.event.*;
public class ex3 implements______
{
private Panel p1,p2;
private Button btn1,btn2;
private Frame. frm;
private CardLayout cl;
public static void main(String[] args)
{
ex3 tt=new ex3();
tt.method();
}
public void method()
{
Frm=new Frame("CardLayout");
Cl=new CardLayout();
btn1=new Button("Card1");
btn2=new Button("Card2");
pl=new Panel();
p2=new Panel();
p1.add(btn1);
btn1.addActionListener(this);
p2.add(btn2);
______
frm.SetLayout(cl);
frm.add(pl,"Layer1");
frm.add(pl,"Layer1");
frm.SetSize{200,200);
frm.SetVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
______
}
}
第5题:
阅读以下说明和Java代码,将解答写入对应栏内。
【说明】
请完成下列Java程序。程序的执行结果是生成一个具有一个TextField类型的对象in、 Button类型的对象btn和Label类型的对象out图形用户界面,程序的功能是计算用户输入数的平方,如图3所示。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
【程序】
import java. awt.*;
import java, awt. event.*;
public class square {
public static void main(String args[ ]){
(1)
}
}
class AppFrame. extends Frame{
TheAdapterTest listener = new TheAdapterTest( );
Text Field in = new TextField (5);
Button btn = new Button("计算");
Label ut = new Label("用于显示计算结果");
public AppFrame( )
{
setLayout( new FlowLayout( ));
add(in);
add(btn)
add(out);
btn. addActionListener( new BtnActionAdapter( ));
addWindowListener (listener);
setSize(400,100);
show( );
}
class BtnActionAdapter implements (2) {
public void actionPerformed((3)) {
String s = in. getText( );
double d =(4)
double sq = d * d;
out. setText(d+"的平方是:" +sq);
}
}
class TheAdapterTest extends WindowAdapter
{
public void windowCIosing((5))
{
System. exit(1)
}
}
}
第6题:
本题中,通过菜单“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;
}
第7题:
●试题六
【说明】
下面是一个Applet程序,其功能是在绘图区域中通过鼠标的移动来绘制直线,并且有清除绘图区域按钮,用来清除已经绘制的图像。
程序运行结果如图5所示。
图5
import javA.awt.*;
import javA.applet.*;
/*
<applet code=ex6_7.class width=800 height=400>
</applet>
*/
public class ex6_7 extends Applet{
private Button btn;
private boolean bDraw, bClear;
private int upX, upY,downX, downY;
public void init(){
setLayout(null);
bClear = false;
bDraw = false;
btn = new Button("clear");
btn.reshape(250, 150, 70, 30);
add(btn);
}
public void paint(Graphics g){
if(bClear){
g.clearRect(0, 0, getSize().width, getSize().height);
(1) ;
}
if(bDraw){
g.drawLine( (2) );
bDraw = false;
}
}
public void update(Graphics g){
(3) ;
}
public boolean mouseDown(Event event, int x, int y){
downX = x;
downY = y;
return true;
}
public boolean mouseUp(Event event, int x, int y){
upX = x;
upY = y;
(4) ;
repaint();
return true;
}
public boolean action(Event event, Object object){
if ( (5) ){
bClear = true;
repaint();
}
return true;
}
}
ex6_7.html
<HTML>
<HEAD>
<TITLE> ex6_7 </TITLE>
</HEAD>
<BODY>
<applet code=" ex6_7.class" width=800 height=400 >
</applet>
</BODY>
</HTML>
第8题:
下面是一个Applet程序,其功能是接收用户输入的两个整数,比较它们的大小,并在用户按下“比较”按钮后,将Applet中的显示的“请先输入两个待比较的整数”,改为“两个整数中最大值:x”,x是两个数中的最大值。请改正程序中的错误(有下划线的语句)并将程序填写完整,使程序能输出正确的结果。
注意:不改动程序结构,不得增行或删行。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class FindMax extends Applet______
{
Label result;
TextField in1,in2;
Button btn;
int a=0,b=0,max;
public void int()
{
result=new Label(“请先输入两个待比较的整数”);
in1=new TextField(5);
in2=new TextField(5);
btn=new Button("比较");
add(in1);
add(in2);
add(btn);
add(result);
btn.addActionListener(this);
}
Public void actionPerformed(ActionEvent e)
{
a=in1;
b=in2;
if(a>b)
max=a;
else
max=b;
btn.setText(“两个数中最大值:”+max);
}
}
Findmax.html:
<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<applet code="FindMax.class"width=800 height=400>
</applet>
</BODY>
</HTML>
第9题:
一个button控件ID为btn_sumbit,双击后得到的处理时间函数名为()
第10题:
< inputid="btnGo"type="button"value="单击我"class="btn"/>以下哪个选项不能够正确地得到这个标签()。
第11题:
在bootstrap中将按钮颜色设置为黄色下列正确的是()。
第12题:
该代码画了一个按钮
Button(“ok”)创建了一个有显示“ok”的按钮
Button()是构造函数
按钮属于容器
第13题:
请完成下列Java程序:用JFrame建立—个窗口,再用网格布局管理器,对窗口布局进行控制,上面有2行2列4个按钮,要求对按钮的大小进行设定,宽度为150,高度为80。
注意:请勿改动main主方法和其他已有语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ex19_2
{
public static void main(String args[])
}
JFrame. frm=new JFrame();
frm.setTitle("ex19_2");
Container cont=frm.getContentPane();
______;
cont.add(new JButton("button1"));
cont.add(new JButton("button2 "));
Dimension dimen=______;
JButton btn1=new JButton("button3");
btn1.setPreferredSize(dimen);
cont.add(btn1);
cont.add(new JButton("button4"));
frm.addWindowListener(new WindowAdapter()
{
public void winowClosing(WindowEvent we)
{
System.exit(0);
}
});
frm.pack();
frm.setVisible(true);
}
}
第14题:
下面是一个Applet程序,其功能是有两个按钮,分别为First和Second,以及一个Label构件。要求单击 First时能在Label中显示出"Command:First",而单击Second时能显示出"Command:Second",要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=ex04_3.class width=800 height=400>
</applet>
*/
Public class ex04_3 extends Applet implements ActionListener
{
private String str="ok";
private Label l;
private Button btn;
public void init()
{
setLayout(null);
l=new Label(str);
l.reshape(10,10,100,30);
add(l);
btn=new Button("First");
btn.reshape(10,50,60,20);
l.addActionListene(this);
add(btn);
btn=new Button("Second");
btn.reshape(10,100,60,20);
btn.setActionCommand("First");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent ae)
{
str="Command:"+ae.getActionCommand();
btn.setText(str);
}
}
ex04_3.html
<HTML>
<HEAD>
<TITLE>ex04_3</TITLE>
</HEAD>
<BODY>
<applet code="ex2_3.class" width=800 height=400>
</applet>
</BODY>
</HTML>
第15题:
下面是一个Applet程序,其功能是打印一个任意进制的乘法表。要求输入乘法表的进制,点击ok则打印出该乘法表。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.applet.*;
import java.lang.*;
public class ex30 3 extends Applet{
private TextField tf;
private Button btn;
public void init(){
tf = new TextField(25);
add(tf);
btn = new Button("OK");
add(btn);
resize(250, 200);
}
public void paint(Graphics g){
try{
int n = tf.getText();
int i,j,x=20,y=60;
for(i=0;i<n;i++){
for (j=1; j<=n; j++) {
g.drawString(Integer.toString(i)+"*"
+Integer.toString(j)+"="
+Integer.toString(i*j),
i*x,j*y);
}
}
}
catch(NumberFormatException nfe){
g.drawString("error number!",20,50);
}
}
public boolean action(Event e, Object o){
if (o == "OK"){
repaint();
return true;
}
else
return false;
}
}
ex30_3.html
<HTML>
<HEAD>
<TITLE>ex30_3</TITLE>
</HEAD>
<BODY>
<applet code="ex30_3.class" width=800 height=400 >
</applet>
</BODY>
</HTML>
第16题:
请在下列程序的横线处填入正确的语句。 import java.awt.*; import java.awt,event.*; public class ex16 { Frame. f; TextArea ta; Button btn; public static void main(String[] args) { ex16 e = new ex16(); e.m(); } public void m() { f = new Frame("ex16"); ta = new TextArea(); btn = new Button("ok"); btn.addActionListener(new MyAction()); f.add(ta, "Center"); f.add(btn, "South"); f.setSize(100, 100); f.setVisible (true); } class MyAction implements ActionListener { ______ { System. out.println (ta. getText ()); } } }
A.static void actionPcrformcd(AcfionEvent e)
B.public void action(ActionEwent e)
C.public actionPerformed(ActionEvent e)
D.public void actionPerformed(ActionEvent e)
第17题:
下列Applet用于显示提供它的主机的IP地址。请选择正确的语句填入横线处。 import java.awt.*; import java. awt. event.*; import java.applet.Applet; import java.net. *; public class ex23 extends Applet { public void init() { setLayout(new GridLayout(2, 1); Button btm = new Button("显示IP"); final Label 1 = new Label(" "); btn. addActionListener (new ActionListener ( ) { public void actionPerformed(ActionEvent ae) { try { URL ur1 = getCodeBase(); String strName = ur1.getHost(); ______ 1.setText (ia.toString()); } catch (Exception e) { e.printStackTrace (); } } }); add (btn); } }
A.InetAddress ia = URL.getByName(strName);
B.InetAddress ia = InetAddress.getByName(strName);
C.InetAddress ia = new InetAddress.getByName(strName);
D.InetAddress ia = InetAddress.getByName(ur1);
第18题:
本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。
import java.awt.event.*;
import java.awt.*;
class MyDialog implements ActionListener
{static final int YES=1,N0=0;
int message=-1;Button yes,no;
MyDialog(Frame. f.String S,boolean b)
{super(f,S,b);
ves=new Button("Yes");yes.addActionListener
(this);
no=new Button("No"); no.addActionListener
(this)o
setLayout(new FlowLayout());
add(yes);add(no);
setBounds(60,60,100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{message=-1;setVisible(false);)
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =yes)
{message=YES;
setVisible(false);
}
else if(e.getSource()= = no)
{message=NO;
setVisible(false);
}
}
public int getMessage()
{return message;
}
}
class Dwindow extends Frame. implements ActionLis-
tener
{TextArea text;Button button;MyDialog dialog;
Dwindow(String s)
{super(s);
text=new TextArea(5,22);button=new Button
("打开对话框");
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);add(text);
dialog=new MyDialog(this,"Dialog",true);
setBounds(60,60,300,300);setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =button)
{
if(dialog.getMessage()= =MyDialog.YES)
{text.append("\n你单击了对话框的yes按
钮");
}
else if(dialog.getMessage()= =MyDialog.NO)
{text.append("\n你单击了对话框的N0按
钮");
}
}
}
}
public class java2
{public static void main(String args[])
{new Dwindow("java2");
}
}
第19题:
●试题六
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。
【说明】
请完成下列Java程序。程序的执行结果是生成一个具有一个TextField类型的对象in、Button类型的对象btn和Label类型的对象out图形用户界面,程序的功能是计算用户输入数的平方,如图3所示。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
【程序】
import javA.awt.*;
import javA.awt.event.*;
public class square {
public static void main(String args[]){
(1)
}
}
class AppFrame. extends Frame{
TheAdapterTest listener=new TheAdapterTest();
TextField in=new TextField (5) ;
Button btn=new Button("计算");
Label ut=new Label("用于显示计算结果");
public AppFrame()
{
setLayout(new FlowLayout());
add(in);
add(btn);
add(out);
btn.addActionListener(new BtnActionAdapter());
addWindowListener(listener);
setSize(400,100);
show();
}
class BtnActionAdapter implements (2) {
public void actionPerformed( (3) ){
String s=in.getText();
double d= (4)
double sq=d*d;
out.setText(d+"的平方是:"+sq);
}
}
class TheAdapterTest extends WindowAdapter
{
public void windowClosing( (5) )
{
System.exit (1) ;
}
}
}
●试题六
【答案】 (1)new AppFrame()
(2)ActionListener
(3)ActionEvent
(4)Integer.parseInt(s);或等价形式
(5)WindowEvent
【解析】生成类AppFrame的对象。实现接口ActionListener。按钮动作事件类名。将字符串s转化为整数并赋给变量d。窗口事件类名 。
第20题:
import java.awt.*; import java.applet.*; public class ButtonDemo extends Applet{ public void init() { Button pushBotton=new Button(“ok”); Button downBotton=new Button(“Yes”); add(pushBotton); add(downBotton); } } 根据以上代码,下列结束正确的是()
第21题:
< inputid="btnGo"type="button"value="单击我"class="btn"/>以下哪个选项能够正确地得到这个标签()。
第22题:
在bootstrap中用于设置按钮的尺寸和颜色的类名为()。
第23题:
下列关于btn标签的说法正确的是()