2016年二級JAVA考試預測試卷及答案
- 計算機二級
- 關注:2.53W次
一、基本操作題(共18分)
本題分別比較兩個字符串"A"和"a"是否相等,並比較兩個字符"A"和"a"是否相等,並輸出比較結果。
public class javal{
public static void main(String[]args){
;
cl=’A ’;c2=’a’;
String strl=new String("A"),str2=new String
("a"):
if( )
tln("char"+c1+"equals"+"char"
+c2);
else
tln("char"+cl+"doesn’t equal
"+"char"+c2);
if( )
tln("string"+strl+"equals"+
"string"+str2):
else
tln("string"+strl+"doesn’t e-
qual"+"string"+str2); .
}
}
二、簡單應用題(共24分)
本題是一個Applet,頁面上有一個按鈕“請單擊”,單擊該按鈕後彈出一個對話框,對話框上有三個按鈕“橙色”、“藍色”和“紅色”,單擊其中任意一個按鈕,則可以將對話框的背景色設置為按鈕名稱所對應的顏色。
import .*;
import t.*;
import g.*;
public class java2 extends JApplet
{
private JFrame framet;
()
{
frame=new JFrame();
itle("java2");
ize(300,200);
ontentPane()(new ButtonPanel
());
JButton PopButton=new JButton("請單擊");
getContentPane()(PopButton);
PopButtom addActionListener(new ActionListener();
{
public void actionPerformed(ActionEvent evt)
{
if(sible())isible(false);
else ;
}
});
}
}
class ButtonPanel extends JPanel
{
private class ColorAction implements ActionLis-
tener
{
private Color backgroundColor;
public void actionPerformed(ActionEvent action-
event)
{
setBackground(backgroundColor);
repaint();
}
public ColorAction(Color color)
{
backgroundColor=color;
}
}
public ButtonPanel()
{
JButton jbutton=new JButton("橙色");
JButton jbuttonl=new JButton("藍色");
JButton jbutton2=new JButton("紅色");
add(jbutton);
add(jbuttonl);
add(jbutton2);
ColorAction coloraction=new ColorAction(ge);
ColorAction coloractionl=new ColorAction();
ColorAction coloraetion2=new ColorAction();
ctionListener(coloraction);
ctionListener(coloractionl);
ctionListener(coloraction2);
}
}
三、綜合應用題(共18分)
本題的功能是用按鈕來控制文本框中文本的.顏色。窗口中有兩個帶有文字標題的面板“Sample text”和“Text color control”,窗口的底部還有一個複選按鈕“Disable changes”。在“Sample text”面板中有一個帶有字符串的文本框,而在“Text color control”面板中有三個按鈕:“Black”、“Red”和“Green”,並且每個按鈕上都有一個對應顏色的圓。單擊任意按鈕,文本框中的文本變成對應的顏色,如果選中“Disable changes”複選項,則三個顏色按鈕變為不可用,如果取消選中複選項,則三個按鈕變為可用。
import g.*;
import .*;
import t.*;
public class java3 extends JFrame{
private JPanel upper,middle,lower;
private JTextField text;
private JButton black,red,green;
private JCheckBox disable;
public java3(String titleText){
super(titleText);
addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent e){
(0);
}
}
);
upper=new JPanel();
order(teTitledBor-
der("Sample text"));
ayout(new BorderLayout());
text=new JTextField("Change the color of this
text"):
(text,ER);
- 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-mo/jsj/erji/zzr546.html