二、基本操作題
本題中定義了一個(gè)長度為20的整數(shù)數(shù)組,然后將1~20分別賦給數(shù)組元素,計(jì)算該數(shù)組中所有下標(biāo)為奇數(shù)的元素的和。
public class javal{
public static void main(String args[]){
int sum;
;
int arrayList[]=new int[20];
for(int i=0;i<=19;i++)
arrayList[i]=i+1;
int pos=0;
while(pos<20){
if( )
sum=sum+arrayList[pos];
;
}
System.out.println("sum="+sum);
}
}
三、簡單應(yīng)用題
本題的功能是通過按鈕來選擇窗口顯示的風(fēng)格。窗口
中有三個(gè)按鈕:“Metal”、“Motif”和“Windows”,單擊任何一
個(gè)按鈕,就能將窗口的風(fēng)格改變?yōu)榘粹o名稱所對(duì)應(yīng)的風(fēng)格。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PlafPanel extends JPanel implements ActionLis-
tener
{public ()
{metaIButton=new JButton("Metal");
motifButtOn=new J Button("Motif");
windowsButton=new JButton("Windows");
add(metalButton);
add(motifButton);
add(windowsButton);
metalButton.addActionListener(this);
motifButton.addActionListener(this);
windowsButton.addActionListener(this);
}
Dublic void actionPerformed(ActionEvent evt)
{Object source=evt.getSource();
String plaf="":
if(source= =metalButton)
plaf="javax.swing.plaf.metal.MetalLookAnd-
Feel";
else if(source= =motifButton)
plaf="com.sun.java.swing.plaf.motif.Moti-
fLookAndFeel";
else if(source= =windowsButton)
Dlaf="com.sun.java.swing.plaf.windows.Win-
dowsLookAndFeel";
try
{UIManager.setLookAndFeel( );
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){)
}
private JButton metalButton;
private JButton motifButton;
private JButton windowsButton;
}
class PlafFrame extends JFrame
{public PlafFrame()
{ setTitle("simple");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(O);
}
});
Container contentPane=getContentPane();
contentPane.add(new PlafPanel());
}
}
public class java2
{public static void main(String[]args)
f JFrame frame=new PlafFrame();
frame.show();
}
二、基本操作題
第1處:sum=0
第2處:pos%2= =1或pos%2 1= =0
第3處:pos++或pos+=1或pos=pos+1
解析:第1處給整型變量sum賦初值;第2處為判斷數(shù)組中元素下標(biāo)為奇數(shù)的條件;第3處是元素下標(biāo)加1繼續(xù)遍歷。
三、簡單應(yīng)用題
第1處:PlafPanel
第2處:plaf
解析:第一處由題意可知,類PlafPanel缺少構(gòu)造函數(shù),所以填PlafPanel。第二處是通過String類型的對(duì)象plaf,和UIManager.setLookAndFeel()方法實(shí)現(xiàn)顯示風(fēng)格的切換。
本題中定義了一個(gè)長度為20的整數(shù)數(shù)組,然后將1~20分別賦給數(shù)組元素,計(jì)算該數(shù)組中所有下標(biāo)為奇數(shù)的元素的和。
public class javal{
public static void main(String args[]){
int sum;
;
int arrayList[]=new int[20];
for(int i=0;i<=19;i++)
arrayList[i]=i+1;
int pos=0;
while(pos<20){
if( )
sum=sum+arrayList[pos];
;
}
System.out.println("sum="+sum);
}
}
三、簡單應(yīng)用題
本題的功能是通過按鈕來選擇窗口顯示的風(fēng)格。窗口
中有三個(gè)按鈕:“Metal”、“Motif”和“Windows”,單擊任何一
個(gè)按鈕,就能將窗口的風(fēng)格改變?yōu)榘粹o名稱所對(duì)應(yīng)的風(fēng)格。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PlafPanel extends JPanel implements ActionLis-
tener
{public ()
{metaIButton=new JButton("Metal");
motifButtOn=new J Button("Motif");
windowsButton=new JButton("Windows");
add(metalButton);
add(motifButton);
add(windowsButton);
metalButton.addActionListener(this);
motifButton.addActionListener(this);
windowsButton.addActionListener(this);
}
Dublic void actionPerformed(ActionEvent evt)
{Object source=evt.getSource();
String plaf="":
if(source= =metalButton)
plaf="javax.swing.plaf.metal.MetalLookAnd-
Feel";
else if(source= =motifButton)
plaf="com.sun.java.swing.plaf.motif.Moti-
fLookAndFeel";
else if(source= =windowsButton)
Dlaf="com.sun.java.swing.plaf.windows.Win-
dowsLookAndFeel";
try
{UIManager.setLookAndFeel( );
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){)
}
private JButton metalButton;
private JButton motifButton;
private JButton windowsButton;
}
class PlafFrame extends JFrame
{public PlafFrame()
{ setTitle("simple");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(O);
}
});
Container contentPane=getContentPane();
contentPane.add(new PlafPanel());
}
}
public class java2
{public static void main(String[]args)
f JFrame frame=new PlafFrame();
frame.show();
}
二、基本操作題
第1處:sum=0
第2處:pos%2= =1或pos%2 1= =0
第3處:pos++或pos+=1或pos=pos+1
解析:第1處給整型變量sum賦初值;第2處為判斷數(shù)組中元素下標(biāo)為奇數(shù)的條件;第3處是元素下標(biāo)加1繼續(xù)遍歷。
三、簡單應(yīng)用題
第1處:PlafPanel
第2處:plaf
解析:第一處由題意可知,類PlafPanel缺少構(gòu)造函數(shù),所以填PlafPanel。第二處是通過String類型的對(duì)象plaf,和UIManager.setLookAndFeel()方法實(shí)現(xiàn)顯示風(fēng)格的切換。