1、计算机二级 JAVA-110 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、简单应用题(总题数:3,分数:100.00)1.本程序中,主窗口包括一个文本框和一个文本域,在文本框中输入字符串后回车,则弹出一个带有“是”和“否”按钮的确认对话框,单击“是”按钮,则将文本框中的字符串添加到文本域中,否则不添加,并且将文本框中的内容清空,如图所示。 import java.awt.event.*; import java.awt.*; import javax.swing. JOptionPane; class Dwindow extends Frame implements Act
2、ionListener TextField inputName; TextArea save; Dwindow(String s) super (s); inputName=new TextField(22); inputName.addActionListener(this); save=new TextArea(); add(inputName,BorderLayout.NORTH); add(save,BorderLayout.CENTER); setBounds(60,60,300,300); setVisible(true); validate(); addWindowListene
3、r(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) String s=inputName.getText(); int n=JOptionPane._ (this,“确认正确吗? “,“确认对话框“, _); if(n=JOptionPane.YES_OPTION) save.append(“/n“+s); else if(n=JOptionPane.NO_OPTION) inputName.set
4、Text (null); public class exam_90 public static void main(String args) new Dwindow(“exam_90“); (分数:30.00)_2.本程序中,主窗口中有一个“打开颜色对话框”按钮,单击该按钮后,则弹出“调色板”对话框,通过“调色板”对话框可以设定颜色,选中某种颜色单击“确定”按钮后,将该颜色设置为按钮的背景色,如图所示。 import java. awt. event. *; import java. awt. *; import j avax. swing. JColorChooser; class Dwin
5、dow extends Frame implements ActionListener Button button; Dwindow(String s) super(s); button=new Button(“打开颜色对话框“); button.addActionListener(this); setLayout(new FlowLayout(); add(button); setBounds(60,60,300,300); setVisible(true); validate(); addWindowListener(new WindowAdaDter() public void wind
6、owClosing(WindowEvent e) System.exit (0); ); public void actionPerformed (ActionEvent e) Color newColor=_(this,“调色板“,button.getBackground(); _; public class exam_91 public static void main(String args) new Dwindow (“exam_91“); (分数:30.00)_3.本程序中,当鼠标在窗口上单击左键时,则以单击的点为圆心画一个小圆,如果单击右键,则以单击的点为中心画一个正方形,当鼠标离
7、开窗口时,窗口上的所有图形将清除掉,如图所示。 (分数:40.00)_计算机二级 JAVA-110 (1)答案解析(总分:100.00,做题时间:90 分钟)一、简单应用题(总题数:3,分数:100.00)1.本程序中,主窗口包括一个文本框和一个文本域,在文本框中输入字符串后回车,则弹出一个带有“是”和“否”按钮的确认对话框,单击“是”按钮,则将文本框中的字符串添加到文本域中,否则不添加,并且将文本框中的内容清空,如图所示。 import java.awt.event.*; import java.awt.*; import javax.swing. JOptionPane; class Dw
8、indow extends Frame implements ActionListener TextField inputName; TextArea save; Dwindow(String s) super (s); inputName=new TextField(22); inputName.addActionListener(this); save=new TextArea(); add(inputName,BorderLayout.NORTH); add(save,BorderLayout.CENTER); setBounds(60,60,300,300); setVisible(t
9、rue); validate(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) String s=inputName.getText(); int n=JOptionPane._ (this,“确认正确吗? “,“确认对话框“, _); if(n=JOptionPane.YES_OPTION) save.append(“/n“+s); else if(n=JO
10、ptionPane.NO_OPTION) inputName.setText (null); public class exam_90 public static void main(String args) new Dwindow(“exam_90“); (分数:30.00)_正确答案:()解析:第 1 处:showConfirmDialog 第 2 处:JOptionPane.YES_NO_OPTION2.本程序中,主窗口中有一个“打开颜色对话框”按钮,单击该按钮后,则弹出“调色板”对话框,通过“调色板”对话框可以设定颜色,选中某种颜色单击“确定”按钮后,将该颜色设置为按钮的背景色,如图所
11、示。 import java. awt. event. *; import java. awt. *; import j avax. swing. JColorChooser; class Dwindow extends Frame implements ActionListener Button button; Dwindow(String s) super(s); button=new Button(“打开颜色对话框“); button.addActionListener(this); setLayout(new FlowLayout(); add(button); setBounds(6
12、0,60,300,300); setVisible(true); validate(); addWindowListener(new WindowAdaDter() public void windowClosing(WindowEvent e) System.exit (0); ); public void actionPerformed (ActionEvent e) Color newColor=_(this,“调色板“,button.getBackground(); _; public class exam_91 public static void main(String args) new Dwindow (“exam_91“); (分数:30.00)_正确答案:()解析:第 1 处:JColorChooser.showDialog 第 2 处:button.setBackground(newColor)3.本程序中,当鼠标在窗口上单击左键时,则以单击的点为圆心画一个小圆,如果单击右键,则以单击的点为中心画一个正方形,当鼠标离开窗口时,窗口上的所有图形将清除掉,如图所示。 (分数:40.00)_正确答案:()解析:第 1 处:InputEvent.BUTTON1_MASK 第 2 处:InputEvent.BUTTON3_MASK