简答题 Java
题号 #2246028

本题中定义了一个树型的通信录,窗El左侧是一个树,
右侧是一个文本域,单击树的结点,则在右侧文本域中显示
相关信息,如果单击的是树结点,则显示对应名字的电话
信息。
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 Default-
MutableTreeNode("同学通信录");
DefaultMutableTreeNode tl=new Default-
MutableTreeNode("大学同学");
DefaultMutableTreeNode t2=new Default-
MutableTreeNode("研究生同学");
DefaultMutableTreeNode tl l=new Default-
MutatleTreeNode("陈艳");
DefaultMutableTreeNode tl 2=new Default-
MutableTreeNode("李小永");
DefaultMutableTreeNode t2 1=new Defauh-
MutableTreeNode("王小小");
DefauhMutableTreeNode t2 2=new Defauh-
MutableTreeNode("董小");
setTitle("java2");
root.add(t1);root.add(t2);
tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2.
add(t2_2);
tree=new JTree(root); 
JSerollPane scrollpane=new JScrollPane(text);
JSplitPane splitpane=new JSplitPane(JSplitPane.
HORIZONTAL SPLIT,
true,tree,scrollpane);
tree.addTreeSeleetionListener(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.getSouree()= =tree)
{DefauhMutableTreeNode node=
(DefaultMutableTreeNode)tree.getLastSelected-
PathComponent();
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();}
}

进入在线模考

参考答案

暂无

答案解析

第1处:implements TreeSelectionListener
第2处:node.toString() 
【解析】第1处实现了一个JTree的监听器接口;第2处将node转换成String型。

你可能感兴趣的试题

1 本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。
import java.awt.*;
import java.awt.event.*;
import javax swing.*;
class MousePanel extends JPanel extends MouseMo-
tionListener
{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-SQUARE-
LENGTH/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= -l;
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[MAX-
NSQUARES];
private int nsquares=0;
private int current=-l;
}
class MouseFrame extends JFramc
{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();
}
}
3 本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“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();
}
}
找试题 · 找答案 · 找资料 在线模考