简答题 Java
题号 #2245950

本题的功能是对列表项的操作,包括删除、添加和反选。窗司中有两个列表框和5个按钮,按钮标签代表着移除列表项的方向,“>”代表只移除选中的列表项,“>>”代表移除所有的列表项,“!”代表反向选择列表项。
import java.awt.*;
import java.awt.event.*;
class java3 extends   Frame   implements
ActionListener&ItemListener{
final static int ITEMS=10;
List ltList=new List(ITEMS,true);
List rtList=new List(0,true);
java3(){
super("java3");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(O);
}
}); 
GridBagLayout gbl=new GridBagLayout();
setLayout(gbl);
add(1tList,0,0,1,5,1.0,1.0);
add(rtList。2,O,1,5,1.O,1.O);
ltList.addActionListener(this);
ltList.addhemListener(this);
rtList.addActionListener(this);
rtList.addhemListener(this);
Button b;
add(b=new Button(">"),1,O,1,1,O,1.0);
b.addActionListener(this);
add(b=new Button(">>"),1,1,1,1,0,1.O);
b.addActionListener(this);
add(b=new Button("<"),1,2,1,1,O,1.0);
b.addActionListener(this);
add(b=newButton("<<"),1,3,1,1,O,1.O);
b.addActionListener(this);
add(b=new Button("!"),1,4,1,1,0,1.O);
b.addActionListener(this);
for(int i=0;i<ITEMS;i++){
ltList.add("item"+i);
}
pack();
show();
}
void add(Component comp,
int X,int Y,int W,int h,double weightx,double
weighty){
GridBagLayout gbl=(GridBagLayout)getLayout
(); 
GridBagConstraints c=new GridBagConstraints
();
e.fill=GridBagConstraints.BOTH;
c.gridx=x;
c.gridy=y;
c.gridwidth=W;
c.gridheight=h;
c.weightx=weightx;
c.weighty=weighty;
add(comp);
gbl.setConstraints(comp,c);
}
void reverseSelections(List l){
for(int i=0;i<1.length();i++){
if(1.islndexSelected(i)){
1.deselect(i);
}else{
1.select(i);
}
}
}
void deseleetAll(List l){
for(int i=0;i<1.gethemCount();i++){
1.deseleet(i);
}
}
void replacehem(List l,String item){
for(int i=0;i<1.getltemCount();i++){
if(1.gethem(i).equals(item)){
1.replacehem(item+"*",i);
}
}
}
void move(List ll,List l2,boolean all){
if(a11){
for(int i=0;i<11.getltemCount();i++){
12.add(11.gethem(i));
}
11.removeAll();
}else{
String[]items=11.getSelectedhems();
int[]itemIndexes=11.getSelectedIndexes();
deselectAll(12);
for(int i=0;i<items.length;i++){
12.add(items[i]);
12.select(12.getItemCount()-1);
if(i= =0){
12.makeVisible(12.getltemCount()-1);
}
}
for(int i=itemlndexes.length-1; i>=0;i--){
11.remove(itemlndexes[i]);
}
}
}
public void actionPerformed(AetionEvent evt){
String ar9=evt.getActionCommand();
if(">".equals(arg)){
move(1tList,rtList,false);
}else if(">>".equals(arg)){
move(1tList,rtList,true);
}else if("<".equals(arg)){
move(rtList,ltList,false);
}else if("<<".equals(arg)){
move(rtList,ltList,true);
}else if("!".equals(arg)){
if(ltList.getSelectedhems().length>0){
reverseSelections(ltList);
}else if(rtList.getSelectedhems().length>
0){
reverseSelections(rtList);
}
}else{
Object target=evt.getSouree();
if(target= =rtList‖target= =ltList){
replacehem((List)target,arg);
}
}
}
public void itemStatedChanged(ItemEvent ent){
List target=(List)evt.getSource();
if(target= =hList){
deselectAll(rtList);
} else if(target= =rtList){
deselectAll(ltList);
}
}
public static void main(String[]args){
new java3();
}
}

进入在线模考

参考答案

暂无

答案解析

第1处:extends Frame implements ActionListener,Item-
Listener
第2处:for(int i=0;i<1.getItemCount();i++)
第3处:public void hemStateChanged(ItemEvent evt)
【解析】第1处类可以实现多个接口,接口之间用“,”隔开;第2处reverseSelections方法实现的是反选,遍历列表 获得列表元素数应使用的是getItemCount()方法;第3处
Java是大小写敏感的。

你可能感兴趣的试题

2 本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。
import java.awt.event.*;
import java.awt.*;
class MyDialog        implements ActionListener
{static final int YES=1,N0=0;
int message=-1;Button yes,no;
MyDialog(Frame f.String S,boolean b)
{super(f,S,b);
ves=new Button("Yes");yes.addActionListener
(this);
no=new Button("No"); no.addActionListener
(this)o
setLayout(new FlowLayout());
add(yes);add(no);
setBounds(60,60,100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{message=-1;setVisible(false);)
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =yes)
{message=YES;
setVisible(false);
}
else if(e.getSource()= = no)
{message=NO;
setVisible(false);
}
}
public int getMessage()
{return message;
}
}
class Dwindow extends Frame implements ActionLis-
tener
{TextArea text;Button button;MyDialog dialog;
Dwindow(String s)
{super(s);
text=new TextArea(5,22);button=new Button
("打开对话框");
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);add(text);
dialog=new MyDialog(this,"Dialog",true);
setBounds(60,60,300,300);setVisible(true);
validate(); 
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =button)
{         ;
if(dialog.getMessage()= =MyDialog.YES)
{text.append("\n你单击了对话框的yes按
钮");
}
else if(dialog.getMessage()= =MyDialog.NO)
{text.append("\n你单击了对话框的N0按
钮");
}
}
}
}
public class java2
{public static void main(String args[])
{new Dwindow("java2");
}
}
3 本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆大于画板的大小时,画板就添加滚动条,在画板中单击鼠标右键,则清除画板中的所有图形。
import javax.swing.*;
import javax.swing.event.MouselnputAdapter;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class java3 extends JPanel{
private Dimension size;
private Vector objects;
private final Color colors[]={
Color.red,Color.blue,Color.green,Color.or-
ange,
Color.cyan,Color.magenta,Color.darkGray,
Color.yellow);
private final int color_n=colors.length;
JPanel drawingArea;
public java3(){
setopaque(true);
size=new Dimension(0,0);
objects=new Vector();
JLabel instructionsLeft=new JLabel("单击鼠标
左键画圆.");
JLabel instructionsRight=new JLabel("单击鼠
标右键清空画板.");
JPanel instructionPanel=new JPanel(new Grid-
Layout(0,1));
instructionPanel.add(instructionsLeft);
instructionPanel.add(instructionsRight);
drawingArea=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Rectangle rect;
for(int i=0;i<objects.size();i++){
rect=(Rectangle)objects.elementAt(i):
g.setColor(colors[(i%color_n)]);
g.fillOval(rect.X,rect.Y,rect.width,rect.
height);
}
}
};
drawingArea.setBackground(Color.white);
drawingArea.addMouseListener(new MouseLis-
tener());
JScrollPane scroller=new JScrollPane(drawing-
Area);
scroller.setPreferredSize(new Dimension(200,
200));
setLayout(new BorderLayout());
add(instructionPanel,BorderLayout.NORTH);
add(scroller,BorderLayout.CENTER):
}
class MyMouseListener extends mouseInputAdapt-
er{
final int W=100;
final int H=100;
public void mouseReleased(MouseEvent e){
boolean changed=false;
if(SwingUtilities.isRightMouseButton(e)){
objects.removeAllElements();
size.width=0;
size.height=0;
changed=true;
}else{
int X=e.getX()-W/z;
int Y=e.getY()-H/2 
if(x<0)x=0; 
if(y<O)Y=0;
Rectangle rect=new Rectangle(X,Y,W,H);
objeets.addElement(rect);
drawingArea.scrollRectToVisible(rect);
int this_width=(x+W+2);
if(this width>size.width)
{size.width=this_width;changed=true;}
int this=height=(y+H+2);
if(this_height>size.height)
{size.height=this_height;changed=true;}
}
if(changed){ 
drawingArea.setPreferredSize(size);
drawingArea.revalidateI();
}
drawingArea.paint();
}
}
public static void main(String args[]){
JFrame frame=new JFrame("java3"):
frame.addWindowListener(new WindowAdapter
(){
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
frame.setContentPane(new java3());
frame.pack();
frame.setVisible(true);
}
}
找试题 · 找答案 · 找资料 在线模考