下列【1】、【2】、【3】、【4】注释标注的哪行代码没有编译错误,但触发运行异常?import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Win extends JFrame implements ActionListener{JPanel panel;JButton button;public Win() {panel = new JPanel();panel.setLayout(new FlowLayout()); //【1】add(panel);//【2】panel.add(new JLabel("java"));button.addActionListener(this);//【3】button = new JButton("enter");panel.add(button);//【4】setBounds(10,10,460,360);setVisible(true);setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);}public void actionPerformed(ActionEvent e){}public static void main(String args[]) {Win win=new Win();}}
A. 【1】
B. 【2】
C. 【3】
D. 【4】
查看答案
下列【1】、【2】、【3】、【4】注释标注的哪行代码有错误?import javax.swing.*;import java.awt.*;public class Win extends JFrame {JTextField text;public Win() {setLayout(new FlowLayout()); //【1】setLocation(500,400);//【2】text = new TextField(8);//【3】add(text);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //【4】}}
A. 【1】
B. 【2】
C. 【3】
D. 【4】
下列【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);//【代码】}}