填空题
本题中定义了长度为20的一维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。
public class java1
public static void main(String[] args)
int a[]=______;
int i;
for(______; i++)
a[i]=i;
for(i=0; i<20; i++)
if(______)
System. out. print("a["+i+"]="+a[i]+",");
【参考答案】
第1处:new int[20]
第2处:i=0; i<20
第3处:i%2!=0
点击查看答案&解析
<上一题
目录
下一题>
热门
试题
填空题
本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java. awt. * ; import java. awt. event. * ; import javax. swing. * ; class MousePanel extends JPanel extends MouseMotionListener public MousePanel() addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent evt) int x=evt. getX(); int y=evt. getY(); current=find(x, y); if(current<0) add(x, y); public void mouseClicked(MouseEvent evt) int x=evt. getX(); int y=evt. getY(); if(evt. getClickCount()>=2) remove(current); ); addMouseMotionListener(this); public void paintComponent(Graphics g) (super. paintComponent(); for (int i=0; i<nsquares; i++) draw(g, i); public int find(int x, int y) for(int i=0; i<nsquares; i++) if (squares[i]. x-SQUARELENGTH 2<=x&& x<=squares[i], x+SQUARELENGTH 2 &&squares[i], y-SQUARELENGTH 2<=y &&y<=squares[i]. y+SQUARELENGTH 2) return i; return-1; public void draw(Graphics g, int i) g. drawRect(squares[i]. x-SQUARELENGTH 2, squares[i]. y-SQUARELENGTH 2, SQUARELENGTH, SQUARELENGTH); public void add(int x, int y) if(nsquares<MAXNSQUARES) squares[nsquares]=new Point(x, y); current=nsquares; nsquares++; repaint(); public void remove(int n) if(n<0||n>=nsquares) return; nsquares--; squares[n]=squares[nsquares]; if(current==n) current=-1; repaint(); public void mouseMoved(MouseEvent evt) public void mouseDragged(MouseEvent evt) private static final int SQUARELENGTH=10; private static final int MAXNSQUARES=100; private Point[] squares=new Point [MAXNSQUARES]; private int nsquares=0; private int current=-1; class MouseFrame extends JFrame public MouseFrame() setTitle( java3 ); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System. exit(0); ); Container contentPane=getContentPane(); contentPane. add (MousePanel()); public class java3 public static void main(String[] args) JFrame frame=new MouseFrame(); frame. show();
点击查看答案&解析
填空题
本题中定义了一个树型的通信录,窗口左侧是一个树,右侧是一个文本域,单击树的结点,则在右侧文本域中显示相关信息,如果单击的是树结点,则显示对应名字的电话信息。 import javax. swing. * ; import javax. swing. tree. * ; import java. awt. * ; import java. awt. event. * ; import javax. swing. event. * ; class Mytree2 extends JFrame______ JTree tree=null; JTextArea text=new JTextArea(20,20); Mytree2() Container con=getContentPane(); DefauhMutableTreeNode root=new DefauhMutableTreeNode ( 同学通信录 ); DefaultMutableTreeNode t1=new DefauhMutableTreeNode( 大学同学 ); DefaultMutableTreeNode t2=new DefaultMutableTreeNode( 研究生同学 ); DefaultMutableTreeNode t1_1=new DefauhMutableTreeNode( 陈艳 ); DefauhMutableTreeNode t1_2=new DefaultMutableTreeNode( 李小永 ); DefauhMutableTreeNode t2_1=new DefaultMutableTreeNode( 王小小 ); DefaultMutableTreeNode t2_2=new DefaultMutableTreeNode( 董小 ); setTitle( java2 ); root. add(t1); root. add(t2); t1. add(t1_1); t1, add(t1_2); t2. add(t2_1); t2. add(t2_2); tree=new JTree(root); JScrollPane scrollpane=new JScrollPane(text); JSplitPane splitpane=new JSplitPane(JSplitPane. HORIZONTAL_SPLIT, true, tree, scrollpane); tree. addTreeSelectionListener(this); con. add(splitpane); addWindowListener( new WindowAdapter() public void windowClosing(WindowEvent e) System. exit(0);); setVisible(true); setBounds(70,80,200,300); public void valueChanged(TreeSelectionEvent e) if(e, getSource()==tree) DefaultMutableTreeNode node= (DefaultMutableTreeNode) tree. getLastSelectedPathComponent(); if(node. isLeaf()) String str=______; if(str. equals( 陈艳 )) text. setText(str+ :联系电话: 0411-4209876 ); else if(str. equals( 李小永 )) text. setText (str+ :联系电话: 010 -62789876 ); else if(str. equals( 王小小 )) text. setText (str+ : 联系电话: 0430-63596677 ); else if(str. equals( 董小 )) text. setText(str + : 联系电话: 020-85192789 ); else text. setText(node. getUserObject(). toString()); public class java2 public static void main(String args[]) Mytree2 win=new Mytree2(); win. pack();
点击查看答案&解析
相关试题
本题中,鼠标在窗口中单击一下,就在单击的...
本题中定义了一个树型的通信录,窗口左侧是...