问答题

本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“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");
}
}

【参考答案】

第1处:extends Dialog
第2处:dialog.setVisible(true)
【解析】第......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)

相关考题

问答题 本题中数组arr中存储了学生的成绩,分别为87,45,56,78,67,56,91,62,82,63,程序的功能是计算低于平均分的人数,并打印输出结果。请在程序空缺部分填写适当内容。使程序能正确运行。 public class javal{ public static void main(String[]args){ int arr[]={56,91,78,67,56,87,45,62,82,63}; int num=arr.length int i=0; int sumScore= 0; int sumNum=0; double average; while(i<num){ sumScore=sumScore+arr[i]; ; } average= ; i=0; do{ if(arr[i]<average) sumNum++: i++; }while( ); System.OUt.println("average:"+average+",be- lows average:"+sumNum); } }

判断题 规模较大的高校可实行“统一领导、分级管理”的财务管理体制。( )

问答题 本题的功能是对列表项的操作,包括删除、添加和反选。窗司中有两个列表框和5个按钮,按钮标签代表着移除列表项的方向,“>”代表只移除选中的列表项,“>>”代表移除所有的列表项,“!”代表反向选择列表项。 import java.awt.*; import java.awt.event.*; class java3 extendsFrame 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(); } }

判断题 教育法规是推行教育道德的有效手段,其对教育道德水平的提高有着积极作用。( )

问答题 本题的功能是获得系统剪贴板中的内容。窗口中有一个菜单“Edit”和一个文本域,“Edit”中有菜单项“Cut”、“Copy”和“Paste”,在文本域中输入内容,可以通过菜单进行剪切、复制和粘贴操作,如果系统剪贴板为空,又做粘贴操作的话,则设置文本域中背景颜色为红色,并显示错误信息。 import Java.awt.*; importjava.io.*; import java.awt.datatransfer.*; import java.awt.event.*; class java3 extends Frame implements ActionListener, ClipboardOwner{ TextArea textArea=new TextArea(); java3(){ super("java3"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); MenuBar mb=new MenuBar(); Menu m=new Menu("Edit"); setLayout(new BorderLayout()); add("Center",textArea); m.add("Cut"); m.add("Copy"); m.add("Paste"); mb.add(m); setMenuBar(this); for(int i=0;i<m.gethemCount();i++){ m.itern(i).addActionListener(this); } setSize(300,300); show(); } public void actionPerformed(ActionEvent evt){ if("Paste".equals(evt.getActionCommand())){ boolean error=true; Transferable t= getToolkit().getSystemClipboard().getContents (this); try{ if(t! =null&&t.isDataFlavorSupported(Dat- aFlavor.stringFlavor)){ textArea.setBackground(Color.white); textArea.setForeground(Color.black); textArea.replaceRange( (String)t.getTransferData(DataFlavor.stringFla- vor), textArea.getSelectionStart(), textArea.getSelectionEnd()); error=false; } }catch(UnsupportedFlavorException e){ }catch(IOException e){ } if(error){ textArea.setBackground(Color.red); textArea.setForeground(Color.white); textArea.repaint(); textArea.setText("ERROR:\nEither the clip- board"+"is empty or the contents is not fl string."); } }else if("Copy".equals(evt.getActionCommand ())) { setContents(); }else if("Cut".equals(evt.getActionCommand ())){ setContents(); textArea.replaceRange("",textArea.getSelec- tionStart(),textArea.getSelectionEnd()); } } void setContents(){ S=textArea.getSelectedText(); St ringSelection contents = new StringSelection (s); getToolkit().getSystemClipboard().setContents (contents,this); } public void lostOwnership(Clipboard clipboard, Transferable contents){ System.out.println("lost ownership"); } public static void main(String args[]){ new java3(); } }

判断题 每次放款都需要提交贷款类文件;与项目有关的协议;担保类文件;与登记、批准、备案、印花税有关的文件等。

问答题 本题定义了一个长度为l0的boolean型数组,并给数组元素赋值,要求如果数组元素下标为奇数,则数组元素值 为false,否则为true。 public class javal{ pubhc static void main(String[]args){ boolean b[]=; for(int i=0;i<10;i++){ if( ) b[i]=false; else ; } for(int i=0;i<10;i++) System.Out.print("bE"+i+"]="+b[i]+","); } }

问答题 本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。 import java.awt.*; import java.awt.event*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; public class java3 { public static void main(String[]args) { TextTestFrame frame=new TextTestFrame(): frame.setDefauhCloseOperation(JFrame.EXIT_ 0N_CLOSE); frame.show(); } } class TextTestFrame extends JFrame { public TextTestFrame() { setTitle("java3"): setSize(DEFAULT_WIDTH,DEFAULT_ HElGHT); Container contentPane=getContentPane(); DocumentListener listener=new DoeumentListen- er(); JPanel panel=new JPanel(); hourField=new JTextField("12",3); panel.add(hourField); hourField.getDocument().addDocumentListener (this); minuteField=new JTextField("00",3): panel.add(minuteField); minuteField.getDocument().addDocumentListener (listener);contentPane.add(panel,BorderLayout.S()UTH); clock=new ClockPanel(); contentPane.add(clock,BorderLayout.CEN- TER); } public void setClock() { try { int hours =Integer.parseInt(hourField.getText().trim ()): int minutes =Integer.parseInt(minuteField.getText().trim ()); clock.setTime(hours,minutes); } catch(NumberFormatExcepfion e){} } public static final int DEFAULT_WIDTH=300; public static final int DEFAULT_HEIGHT =300; private J TextField hourField; private JTextField minuteField; private ClockPanel clock; private class clockFieldListener extends Docu- mentListener { public void insertUpdate(DocumentEvent e){ set- Clock();} public void removeUpdate(DocumentEvent e){ setClock();} public void changedUpdate(DocumentEvent e){} } } class ClockPanel extends JPanel { pubhc void paintComponent(Graphics g) { super.paintComponent(g); Graphies2D g2=(Graphics2D)g; Ellipse2D circle =new Ellipse2D.Double(0,0,2* RADIUS,2 *RADIUS); g2.draw(circle); double hourAngle =Math.toRadians(90-360*minutes/(12 *60)); drawHand(92,hourAngle,HOUR_HAND_ LENGTH); double minuteAngle =Math.toRadians(90-360*minutes/60); drawHand(g2,minuteAngle,MINUTE_HAND_ LENGTH): } punic void drawHand(Graphics2D g2, double angle,double handLength) { Point2D end=new Point2D.Double( RADIUS+handLength*Math.cos(angle), RADIUS-handLength*Math.sin(angle)); Point2D center=new Point2D.Double(RADIUS, RADIUS): g2.draw(new Line2D.Double(center,end)); } public void setTime(int h,int m) { minutes=h*60+m; repaint(); } private double minutes=0; private double RADIUS=100; private double MINUTE_HAND_LENGTH= 0.8*RADIUS; private double HOUR_HAND_LENGTH=0.6 *RADIUS: }

问答题 本题利用递归方法求前n个自然数的和(n=lO)。 public class javal{ public static void main(String[]args){ int sum=add(10): System.out.println("1+2+…+9+10="+ sum); } public static int add( ){ if(n= =l){ ; } else ; } }

判断题 商业银行财务管理主要是指商业银行对各职能部门、分支机构的成本费用和利润进行控制考核,其目的降低整体经营成本、提高经济效益。