6[简答题]
本题是一个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java2 extends JFrame implements ActionL-
istener
{JTable table;Object a[][];
Object name[]={"姓名","英语成绩","数学成 绩","总成绩"};
JButton button;
java2()
{setTitle("java2");
a=new Object[8][4]:
for(int i=0;i<8;i++)
{for(int j=0;j<4;j++)
{if(j!=0)
a[i][j]="";
else
a[i][j]="';
}
}
button=new JButton("计算每人总成绩");
table=
button.addActionListener(this);
getContentPane().add(new JScrollPane(table),
BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.
SOUTH):
setSize(400,200);
setVisible(true):
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(O);
}
});
}
public void actionPerformed(ActionEvent e)
{for(int i=0;i<8;i++)
{double sum=0;
boolean boo=true;
for(int J=1;j<=2;j++)
{try{
sum=sum+Double.parseDouble( );
}
catch(Exception ee)
{
boo=false;
table.repaint();
}
if(boo= =true)
{
a[i][3]=""+sum:
table.repaint();
}
}
}
}
public static void main(String args[-])
{java2 Win=new java2();
}
}
7[简答题]
本题用复选框来控制字体的显示,窗口中有一个标签和两个复选按钮,这两个复选按钮分别对应的字体的两个特点:加粗和倾斜,任意选中一个按钮或两个都选中,标签上的字符串就显示对应的字体。
tmport.java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CheckBoxFrame extends JFrame implements Ac-
tionListener{
public CheckBoxFrame(){
setTitle("CheckBoxTest");
setSize(300,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)(
System.exit(O);
}
});
JPanel P=new JPanel();
bold=addCheckBox(p,"Bold");
italic=addCheckBox(P,"Italic");
getContentPane().add(p,"South");
panel=new CheekBoxTestPanel();
getContentPane().add(panel,"Center");
}
public JCheckBox addCheekBox(JPanel p,String
name){
JCheckBox c=new JCheekBox(name):
c.addActionListener( );
P.add(c);
return C;
}
public void {
int m=(bold.isSelected()?Font.BOLD:O)+
(italic.isSelected()?Font.ITALIC:O);
panel.setFont(m);
}
private CheckBoxTestPanel panel;
private JCheckBox bold;
private JCheckBox italic;
}
class CheckBoxTestPanel extends JPanel{
public CheckBoxTestPanel(){
setFont(Font.PLAIN);
}
public void setFont(int m){
setFont(new Font("SansSerif",m,12));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
9.drawstring("The quick brown fox jumps over
the lazy dog.",0,50);
}
}
public class java2{
public static void main(String[]args){
JFrame frame=new CheckBoxFrame();
frame.show();
}
}
8[简答题]
本题是一个Applet,它显示了一个树型结构。单击树结点的时候,就能将其子结点展开,同时下面的文本框可以显示出所单击的结点的路径,比如单击了根结点下B结点下B2结点,则文本框显示为"[,B,B2]"。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class java2 extends JApplet
{
JTree tree;
JTextField jtf;
public void init()
{
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
top=new DefaultMutableTreeNode("
"):
DefaultMutableTreeNode a=new Default-
MutableTreeNode("A");
DefaultMutableTreeNode al = new Default-
MutableTreeNode("Al");
a.add(a1);
DefatIltMutableTreeNode a2=new Default-
MutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode a3=new Default-
MutableTreeNode("A3");
a.add(a3);
DefaultMutableTreeNode b=new Default-
MutableTreeNode("B");
DefaultMutableTreeNode bl=new Default-
MutableTreeNode("Bl");
b.add(b1);
DefaultMutableTreeNode b2=new Default-
MutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3=new Default-
MutableTreeNode("B3"): b.add(b3); top.add(a); top.add(b); tree=new JTree(top); int v=ScrollPaneConstants.VERTICAL_ SCROLLBAR_AS_NEEDED; int h=ScrollPaneConstants.HORIZONTAL_ SCRoLLBAR_AS_NEEDED; JScrollPane jsp=new JScrollPane(tree,V,h); cp.add(jsp,BorderLayout.CENTER); jtf=new JTextField(20); cp.add(jtf,BorderLayout.SOUTH); tree.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { doMouseClicked(me); } }); } void doMouseClicked(MouseEvent me) { tp=tree.getPathForLocation(me.getX (),me.getY()); if(tp!=null) jtf.setText(tp.toString()); else jtf.setText("");
}
}
9[简答题] 本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击一下,在单击的地方就会出现一个坐标圆点,用鼠标在另外一个地方单击一下又会出现另外一个圆点,并且此时在两个坐标圆点间画出一条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外一条直线。
Import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
class EventQueuePanel extends JPanel implements Ac-
tionListener
{EventQueuePanel()
{JButton button=new JButton("Draw line");
add(button); button.addActionListener(this); } public void actionPerformed(ActionEvent evt) (Graphics g=getGraphics(); P=getClick(); g.drawOval(P.x-2,P.Y-2,4,4); Point q=getClick(); g.drawOval(q.x-2,q.y-2,4,4); g.drawLine(P.X,P.Y,q.x,q.y); g.dispose(); } public Point getClick() {EventQueue eq=Toolkit.getDefaultToolkit(). getSystemEventQueue(); while(true) {try {AWTEvent evt=eq.getNextEvent(); if(evt.getID()= =MouseEvent.MOUSE. PRESSED) {MouseEvent mevt=(MouseEvent)evt; Point P= (); Point top=getRootPane().getLocation(); P.X-=top.x; P.Y-=top.Y; return P; } } catch(InterruptedException e) {} } } private int Y=60; } class EventQueueFrame extends JFrame {public EventQueueFrame() {setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane(); contentPane.add(new EventQueuePanel()); } } public class java2 {public static void main(String[]args) {Frame frame=new EventQueueFrame(); frame.show(); } }
10[简答题]
本题是一个Applet,页面中有10个按钮,名称从“0~ 9”,用鼠标任意单击其中一个按钮后,通过键盘上的上下左右键可以控制按钮在窗口中移动。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class java2 extends Applet
{Button b[]=new Button[10];
int x,Y;
public void init() {for(int i=0;i<=9;i++) {b[i]=new Button(""+i); b[i].addKeyListener(this); addCb[i]); } } public void {Button button=(Button)e.getSource(); x=button.getBounds().x; y—button.getBounds().y; if(e,getKeyCode()= =KeyEvent.VK_UP) {y=y-2; if(y<=O)y=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ DOWN) {y=y+2; if(y>=300)y=300; button,.setLocation(X,y); } else if(e.getKeyCode()= =KeyEvent.VK_ LEFT) {x=x-2; if(x<=0)x=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ RIGHT) (x=X+2; if(x>=300)x=300; button.setLoeation(X,y); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }
2016年全国计算机等级考试《二级JAVA》简答应用试题.doc