btn=new Button("ok"); FrameInout() { add(btn); btn.addActionListener(this); } public void ActionPerformed(ActionEvent e){...} } 以上语句中的this是指代btn(按钮)对象。()此题为判断题(对,错)。

题目
btn=new Button("ok"); FrameInout() { add(btn); btn.addActionListener(this); } public void ActionPerformed(ActionEvent e){...} } 以上语句中的this是指代btn(按钮)对象。()

此题为判断题(对,错)。


相似考题
更多“btn=new Button("ok"); FrameInout() { add(btn); btn.addActionListener(this); } public void ActionPerformed(ActionEvent e){...} } 以上语句中的this是指代btn(按钮)对象。() 此题为判断题(对,错)。”相关问题
  • 第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>


    正确答案:this in1.getText() in2.getText()
    this in1.getText() in2.getText() 解析: 本题主要考查JavaApplet程序的编写、java.awt包的基本组件的使用及supper和this关键字的使用。解答本题的关键是熟练掌握Java Applet程序的编写、java.awt包的基本组件的使用及supper和this关键字的使用。Applet(小程序)是一种很重要的Java程序,是工作在Internet的浏览器上或借助JDK中的appletviewer来工作的Java程序。编写Applet小程序必须要用到java.appelt包中的Applet类。java.applet.Applet是java.awt.Panel的子类。
    在本题中,public class LookForMax extends Appkt implements ActionListener语句的功能是声明一个继承Applet类且实现 ActionListener接口的类LookForMax来实现程序功能,btn.addActionListener(this);语句的功能是为按钮btn对象注册一个事件监听器this(this是指当前LookForMax的对象)a=Integer.parseInt(in1.getText());和a=Integer.parseInt(in1.getText());语句的功能是把从文本框in1和in2获得的字符型数据转换成基本整型数据,并把这两个值分别赋给变量a和b。

  • 第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>


    正确答案:y=getSize().height/2 int y2=y+(int)(a*Math.sin(i+1)) a=Float.parseFloat(tf1.getText())
    y=getSize().height/2 int y2=y+(int)(a*Math.sin(i+1)) a=Float.parseFloat(tf1.getText()) 解析:本题主要考查Applet的图形绘制,Applet的生命周期以及其事件处理机制。解题关键是熟悉Applet窗口中坐标的布局,会使用TextField,Canvas,Button等构件,会使用内部类继承Canvas类,实现在画布中绘制图像的方法,并通过在主程序中结合事件处理机制来调用该方法。本题中,第一处,由于画布中的纵坐标是从上向下从0开始的,因此需要有一个相对坐标原点,这里取画布的一半;第二处,计算下一点的坐标,应该是相对于坐标原点y而言的坐标值,不应该是y1;第三处,a为float类型的变量,因此要用Float类的类型转换方法。

  • 第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);

    }

    }

    }


    正确答案:addMouseMotionListener(this) event.getPoint()
    addMouseMotionListener(this) event.getPoint() 解析:本题主要考查鼠标移动事件监听和基本的AWT构件。解题关键是熟悉鼠标移动事件监听器的注册,和对鼠标移动事件的处理。本题中,第1个空,注册鼠标移动事件的监听器,注意参数是this,表示在当前运行的对象中注册该监听器:第2个空,通过调用MouseEvent的getPoint()方法获得鼠标的坐标信息。程序运行结果如下。

  • 第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)

    {

    ______

    }

    }


    正确答案:ActionListener btn2.addActionListener(this); cl.previous(frm);
    ActionListener btn2.addActionListener(this); cl.previous(frm); 解析:本题综合考查了对图形用户界面和事件处理的掌握。按钮可以引发动作事件,当用户单击一个按钮时就引发了一个动作事件,希望相应按钮引发的动作事件的程序必须把按钮注册给实现了ActionListener接口的动作事件监听者。因此,第1空应填入的是ActionListener。第2空的功能是将btn2注册给当前的监听者。因此,第2空应填入的是btn2.addActionListener(this);。第3空需要填入的是响应鼠标事件的处理代码,当单击鼠标时,要显示下一张卡片中的内容,则需要调用next(Container parent)或者previous(Container parent)方法。因此,第3空应填入的是cl.next(frm);或cl.previous(frm);。

  • 第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)

    }

    }

    }


    正确答案:(1)new AppFrame() (2)ActionListener (3)ActionEvent (4)IntegerparseInt(s);或等价形式 (5)WindowEvent
    (1)new AppFrame() (2)ActionListener (3)ActionEvent (4)Integer,parseInt(s);或等价形式 (5)WindowEvent 解析:生成类AppFrame的对象。实现接口ActionListener。按钮动作事件类名。将字符串a转化为整数并赋给变量d。窗口事件类名。

  • 第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;

    }


    正确答案:
    第1处:setJMenuBar(mbar)第2处:publicConnectDialog(JFrameparent)第3处:publicBooleanshowDialog(Connectlnfotransfer)【解析】第1处参数错误,bar未定义;第2处从下一行的super(parent,"Connect",true);可以看出,这里需要的参数为父窗体;第3处从下面的returnok;等可以看出,这是一个有Boolean型返回值的函数,故类型应为Boolean。

  • 第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>


    正确答案:
    ●试题六【答案】(1)bClear=false(2)downX,downY,upX,upY(3)paint(g)(4)bDraw=true(5)event.target==btn【解析】本题主要考查Applet的图形绘制,鼠标事件处理和用户界面的相关知识点以及会使用boolean类型的变量控制程序的流程。解题关键是,熟悉Applet的图形绘制,会用Graphics类的基本绘图方法,例如drawLine()方法;熟悉鼠标事件处理,并能与图形绘制和用户界面设计相结合做简单的综合应用;掌握一定的程序流程的控制思想。本题中,主要的思想是首先判断用户将鼠标按下与放开的坐标位置,然后使用drawLine()方法绘制由鼠标按下点到放开点的直线。注意不要将drawLine()方法的几个参数颠倒,要明白此方法的几个参数的确切含义。将bDraw变量的值设为false,这导致程序在调用paint()方法时不能进入g.drawLine语句,应改为true。最后要明确clear是btn对象显示在用户界面的名称,而不能作为对象来与event.target做比较,因此应改为btn,这在调试时就会出现变量无法解析的错误;符号==这里要确定点击btn事件发生才能做清除,否则不能达到预期效果。

  • 第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>


    正确答案:implement ActionListener 将a=in1;改为a=Integer.parseInt(in1.getText()); 将b=in2;改为b=Integer.parseInt(in2.getText()); 将btn.setText(“两个数中最大值:”+max);改为result.setText("两个数中最大值:"+max);
    implement ActionListener 将a=in1;改为a=Integer.parseInt(in1.getText()); 将b=in2;改为b=Integer.parseInt(in2.getText()); 将btn.setText(“两个数中最大值:”+max);改为result.setText("两个数中最大值:"+max); 解析:本题考查文本框、Applet操作和事件处理。按钮可以引发动作事件,当用户单击一个按钮时就引发了一个动作事件,希望相应按钮引发的动作事件的程序必须把按钮注册给实现了ActionListener接口的动作事件监听者。因此,第1空应该填入的是implement ActionListener。为了接收用户输入的两个整数,需要获得文本框的内容,并将其转化为整数。而a=in1;b=in2;两个赋值语句直接将文本框对象等同于整数,不能得到正确的结果。这两句应该改为a=Integer.parseInt(in1.getText());b=Integer.parseInt(in2.getText());,其中,getText()获得文本框的内容,返回字符串。而Integer.parseInt(String)将字符串对象转换为整数。另外,根据题意,是要在Applet上的“请先输入两个待比较的整数”改为“两个整数中最大值:x”。其中前一个引号中的话是显示在标签中的,因此btn.setText("两个整数中最大值:x"+max);中的操作对象有误,应该将按钮对象btn改为标签对象result。

  • 第9题:

    一个button控件ID为btn_sumbit,双击后得到的处理时间函数名为()

    • A、button_click
    • B、btn_submit_Click
    • C、btn_submit_push
    • D、button_push

    正确答案:B

  • 第10题:

    < inputid="btnGo"type="button"value="单击我"class="btn"/>以下哪个选项不能够正确地得到这个标签()。

    • A、$("input[type=’button’]")
    • B、$("#btnGo")
    • C、$(".btnGo")
    • D、$(".btn")

    正确答案:C

  • 第11题:

    在bootstrap中将按钮颜色设置为黄色下列正确的是()。

    • A、btn-danger
    • B、btn-success
    • C、btn-primary
    • D、btn-warning

    正确答案:D

  • 第12题:

    多选题
    import java.awt.*;    import java.applet.*;    public class ButtonDemo extends Applet{    public void init(){  Button pushButton=new Button(“ok”);  Button downButton=new Button(“Yess”);  add(pushButton);  add(downButton);  }  }  根据以上代码,下列解释正确的是()
    A

    该代码画了一个按钮

    B

    Button(“ok”)创建了一个有显示“ok”的按钮

    C

    Button()是构造函数

    D

    按钮属于容器


    正确答案: C,A
    解析: 暂无解析

  • 第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);

    }

    }


    正确答案:cont.setLayout(new GridLayout(22)) new Dimension(15080)
    cont.setLayout(new GridLayout(2,2)) new Dimension(150,80) 解析:本题主要考查网格布局管理器 (GridLayout)在使用Swing制作图形用户界面时的一些简单应用。解题关键是熟悉 GridLayout的使用方法,初始化的方法是通过容器Container的对象调用setLayout()方法实现的,而它的一些特点,如并不会保持当初组件所定义的大小等,也需要很熟悉。本题中,第 1个空,采用初始化Dimension类的对象来给出Button组件的初始大小,参数分别是宽度和高度;第2个空,将容器通过网格布局管理器设置为2行2列。程序运行结果如下。

  • 第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>


    正确答案:btn.addActionListener(this) btn.setActionCommand("second") l.setText(str)
    btn.addActionListener(this) btn.setActionCommand("second") l.setText(str) 解析:本题主要考查Java语言中高级事件 AcfionEvent和AWT基本构件Label的常用方法的使用。解题关键是熟练掌握动作事件 ActionEvent和Label构件的常用方法。在本题中,第1处,明确注册的事件监听器是监听按钮的,而不是Label;第2处,调用ActionEvent的setActionCommand()方法改变了ActionCommand,使按下Second按钮时显示 "Command:second",而不是"Command:First";第3处,调用Label的setText()方法,而不是 Button的方法。程序运行结果如下图所示。

  • 第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>


    正确答案:Integer.parseInt(tf.getText()) i=1;i=n;i++ i*50+xj*20+y
    Integer.parseInt(tf.getText()) i=1;i=n;i++ i*50+x,j*20+y 解析: 本题主要考查Applet窗口设计,for循环语句。解题关键是掌握Applet窗口的基本事件机制,会基本的窗口布局,会使用for循环进行程序设计。本题中,第1处,tf对象调用getText()方法返回的是String类的对象,所以需要做类型转换;第2处,注意循环变量的上下界应该分别是1和n如果为0乘法表中会出现0项;第3处,坐标计算公式,只要能使结果正常分布在窗口中即可。

  • 第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)


    正确答案:D

  • 第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);


    正确答案:B

  • 第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");

    }

    }


    正确答案:
    第1处:extendsDialog第2处:dialog.setVisible(true)【解析】第1处设定对话框的类应继承Dialog类;第2处显示对话框。

  • 第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);   } }  根据以上代码,下列结束正确的是()

    • A、该代码画了一个按钮
    • B、Button(“ok”)创建一个有显示”ok”的按钮
    • C、Button()是构造函数
    • D、按钮属于容器

    正确答案:B,C

  • 第21题:

    < inputid="btnGo"type="button"value="单击我"class="btn"/>以下哪个选项能够正确地得到这个标签()。

    • A、$("input[type=’button’]")
    • B、$("#btnGo")
    • C、$(".btnGo")
    • D、$(".btn")

    正确答案:A,B,D

  • 第22题:

    在bootstrap中用于设置按钮的尺寸和颜色的类名为()。

    • A、.btn-xs
    • B、.btn-lg
    • C、.btn-danger
    • D、.btn-success

    正确答案:A,B,C,D

  • 第23题:

    下列关于btn标签的说法正确的是()

    • A、btn-group能将按钮组成按钮组
    • B、btn-toolbar能将btn做成复杂组件
    • C、btn-group可以嵌套使用
    • D、可以使用btn-group-lg,btn-group-sm来调整按钮大小

    正确答案:A,B,C,D