下列【1】、【2】、【3】、【4】注释标注的哪行代码有错误?import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Win extends JFrame implements ItemListener {JTextField text;public Win() {setLayout(new FlowLayout()); //【1】setLocation(500,400);//【2】text = new JTextField(8);add(text);text.addItemListener(this); //【3】setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //【4】}public void itemStateChanged(ItemEvent e){}}
A. 【1】
B. 【2】
C. 【3】
D. 【4】
查看答案
当用户将复选框选中时(如图),给出下列【代码】注释标注的代码的输出结果。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Win extends JFrame {JCheckBox check;public Win() {setLayout(new FlowLayout());check = new JCheckBox("good");check.addItemListener(new Listener());add(check);setBounds(10,10,460,360);setVisible(true);setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);}public static void main(String args[]){new Win();}}class Listener implements ItemListener {public void itemStateChanged(ItemEvent e){JCheckBox box =(JCheckBox)e.getSource();if(box.isSelected())System.out.println(box.getText());//【代码】}}
当用户在文本框中输入6按回车键(如图),给出下列【代码】注释标注的代码的输出结果。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Win extends JFrame {JTextField text;public Win() {setLayout(new FlowLayout());text = new JTextField(8);text.addActionListener(new Listener());add(text);setBounds(10,10,460,360);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}public static void main(String args[]){new Win();}}class Listener implements ActionListener {public void actionPerformed(ActionEvent e){JTextField text =(JTextField)e.getSource();int m = Integer.parseInt(text.getText());System.out.println(m*m);//【代码】}}
创建监视ActionEvent事件的监视器的类必须实现ActionListener接口。
A. 对
B. 错
监视KeyEvent事件的监视器也可以是KeyAdapater类的子类的实例。
A. 对
B. 错