问答题创建线程对象,要传递代码与数据,而传递代码与数据有两种方法,一是通过继承Thread类,二是向Thread类传递一个Runnable对象。请在下面程序的每条横线处填写一个语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class TestThread{
public static void main(String args[ ])
{
MyThread t=new MyThread();
_______________________
}
}
class MyThread_____________Thread{
_____________________
{
for(int i=0;i<10;i++){
System.out.println(" " +i);
}
}
}
问答题下面是一个Applet程序,其功能是进行整数加法运算,要求有3个文本区域,2个作为输入, 1个作为输出,并且由异常处理来判断输入的数字是否为整型,如果是则进行运算,否则抛出异常并显示在第3个文本区域中。请改正程序中的错误(有下画线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import java.awt.event.*; import java.applet.Applet; /* <applet code="ex17_3.class"width=800 height=400> </applet> */ public class ex17_3 extends Applet implements ActionListener TextField tf1=new TextField(5); TextField tf2=new TextField(5); TextField tf3=new TextField(5); Label l1=new Label("+"); Label l2=new Label("="); Button btn=new Button("相加"); public void init() add(l1); add(tf1); add(tf2); add(l2); add(tf3); add(btn); btn.addActionListener(this); public void actionPerformed(ActionEvent ae) try int a=Integer.parseInt(tfi.getText()); int b=Integer.parseInt(tf2.getText()); int x=a+b; tf2.setText(Integer.toString(x)); catch(NumberFormatException nfe) tf3.setText("error!"); ex17_3.html <HTML> <HEAD> <TITLE>ex17_3</TITLE> </HEAD> <BODY> <applet code="ex17_3.class"width=800 height=400> </applet> </BODY> </HTML>
问答题下列程序中,定义了一个3行4列的数组A,并将A的内容转换为ASCII码值,并复制到数组B中,然后打印出来。 注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。 public class Example1_4 private char A[] [] = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'; public int ______; //生成一个空的3行4列的数组B public void copy() for(int i = 0; i < 3; i++) for(int j = 0; j < 4; j++) ______; public static void main(______ argv[]) Example1_4 example = new Example1_4(); example.copy(); for(int i = 0; i < 3; i ++) for(int j = 0; j < 4; j++) System.out.print(example. B[i] [j] *+" "); System.out.println ();
问答题下面是一个Applet程序,其功能是建立一个图形用户界面的窗口,包括一个文本显示区和一个按钮,点击按钮,可以在文本区已有的文本基础上追加显示10条"Welcome to the NCR Examination!"信息,并且文本区由滚动条控制文本的上下滚动。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ex13_3.class" width=800 height=400 >
</applet>
*/
public class ex13_3 extends JApplet
{
JButton jb = new JButton("Add Text");
JTextPane jtp = new JTextPane();
public void init()
{
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i = 1; i < 10; i++)
{{U}}jtp.geText(jtp.setText() +{{/U}}
{{U}}"Welcome to the NCR Examination!/n"){{/U}};
}
});
Container cp = getContentPane();
cp.add(new JScrollPane(jtp));
{{U}}cp.add(BorderLayout. SOUTH, jtp){{/U}};
}
public static void main(String[] args)
{
ex13_3 objl3_3=new ex13_3();
String str = obj13_3.getClass().toString();
if(str.indexOf("class") != -1)
str= str.substring(6);
JFrame frm = new JFrame(str);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(O);
}
});
{{U}}frm.getContentPane().add(ex13_3){{/U}};
frm.setSize(300, 400);
frm.setVisible(true);
}
}
ex13_3.html
<HTML>
<HEAD>
<TITLE>ex13_3</TITLE>
</HEAD>
<BODY>
<applet code="ex13_3.class" width=800 height=400 >
</applet>
</BODY>
</HTML>
问答题本程序的功能是对下拉菜单项的操作,包括添加和删除。页面中包括一个下拉菜单、一个文本框和两个按钮(“删除”和“添加”),选中下拉菜单的一项后,可以通过“删除”按钮从下拉菜单中删除该项,在文本框中填入字符串后,单击“添加”按钮就可以将该项添加到下拉菜单中,所有这些信息都将显示在右侧的文本域中,如图所示。importjava.awt.*;importjava.awt.event.*;publicclassexam_77extendsjava.applet.AppletimplementsItemListener,ActionListener{Choicechoice;TextFieldtext;TextAreaarea;Buttonadd,del;publicvoidinit(){choice=newChoice();text=newTextField(8);area=newTextArea(6,15);choice.add("音乐天地");choice.add("武术天地");choice.add("象棋乐园");choice.add("交友聊天");add=newButton("添加");del=newButton("删除");add.addActionListener(this);del.addActi0nListener(this);choice.addItemListener(this);add(choice);add(del);add(text);add(add);add(area);}publicvoiditemStateChanged(ItemEvente){Stringname=______;intindex=choice.getSelectedIndex();area.setText("/n"+index+":"+name);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==add||e.getSource()==text){Stringname=text.getText();if(name.length()>0){choice.add(name);choice.select(name);area.append("/n添加"+name);}}elseif(e.getSource()==del){choice.remove(______);area.append("/n删除"+choice.getSelectedItem());}exam_77.html:<html><head><title>exam_77</title></head><body><appletcode="exam_77.class"width="400"height="500"></applet></body></html>
问答题下面的程序是从命令行输入3个数传递到public static void main(String args[])方法中(如java Is Triangle 3 4 5),并判断这3个数能否构成三角形的3条边,并显示相应的结果。请在程序的每条横线处填入适当的语句,使程序的功能完整。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class IsTriangle public static void main(String args[ ] ) int a[ ]=new______________ [args.length]; for(int i=0;i<args.length;i++) a[i] = ____________________ if(_________________) System.out.println(a[0]+ ","+a[1]+ ","+a[2] "能构成三角形的3条边"); else System.out.println(a[0]+ ","+a[1]+ ","+a[2] "不能构成三角形的3条边");
问答题本程序的功能是通过菜单打开一个文件,并把文件的字符流显示在文本域中。程序窗口中有菜单和一个文本域,菜单中有“File”和“Edit”,“File”菜单中包括“Fileopen”和“Exit”菜单项,“Edit”菜单中包括“ChangeColor...”菜单项(每个菜单和菜单项都有一个快捷字母,“File”为f、“Fileopen”为0、“Exit”为x、“Edit”为e、“ChangeColor...”为C)。单击“Fileopen”菜单项就会弹出“打开”对话框,选中要打开的文件,单击“打开”按钮后返回主窗口,被选中文件的字符流就被添加到主窗口的文本域中。单击“ChangeColor...”会弹出颜色选择对话框,选中一种颜色后单击“确定”按钮,返回主窗口,文本域中的颜色就变成刚才所选颜色。单击“Exit”菜单项退出程序,如图所示。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;publicclassexam_8extendsJFrame{privateJTextAreatext=newJTextArea();privateJFileChooserfileChoose=newJFileChooser();privateJDialogcolorDlg;privateJColorDialogcolorChoose=newJColorDialog();publicexam_8(StringtitleText){super(titleText);setJMenuBar(buildMenuBar);text.setEditable(false);Containercp=getContentPane();cp.add(newJScrollPane(text),BorderLayout.CENTERsetSize(500,400);setVisible(true);}publicvoidloadFile(){intresult=fileChoose.showOpenDialog(exam_8.this);Filefile=fileChoose.getSelectedFile();if(file!=nulltext.setText("");char[]charBuffer=newchar[4096];intcharsRead=fr.read(charBuffer,0,charBuffer.length);while(charsRead!=-i){text.append(newString(charBuffer,0,charsRead));charsRead=fr.read(charBuffer,0,charBuffer.length);}}catch(IOExceptionioe){ioe.printStackTrace();}}publicJMenuBarbuildMenuBar(){JMenuBarmenuBar=newJMenuBar();JMenufileMenu=newJMenu("File");JMenueditMenu=newJMenu("Edit");JMenuItemexitItem=newJMenuItem("Exit");JMenuItemfileOpenItem=newJMenuItem("Fileopen...");JMenuItemcolorsItem=newJMenuItem("ChangeColor...");fileMenu.setMnemonic(KeyEvent.VK_F);editMenu.setMnemonic(KeyEvent.VK_E);fileOpenItem.setMnemonic(KeyEvent.VK_O);exitItem.setMnemonic(KeyEvent.VK_X);colorsItem.setMnemonic(KeyEvent.VKC);fileOpenItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente{loadFile();}});exitItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente{dispose();System.exit(0);}});colorsItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente{if(colorDlg==null){colorDlg=JColorChooser.createDialog(exam_8.this,"SelectTextColor",true,colorChoose,newColorOKListener(),null);}colorChoose.setColor(text.getForeground());colorDlg.setVisible(true);}});menuBar.add(fileMenu);menuBar.add(editMenu);fileMenu.add(fileOpenItem);fileMenu.add(exitItem);editMenu.add(colorsItem);returnmenuBar;}classColorOKListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){Colorc=colorChoose.getColor();text.setForeground(c);text.repaint();}}publicstaticvoidmain(String[]args){newexam_8("exam_8");}}
问答题本题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一个按钮,就能将窗口的风格改变为按钮名称所对应的风格。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PlafPanel extends JPanel implements ActionLis-
tener
{public ()
{metaIButton=new JButton("Metal");
motifButtOn=new J Button("Motif");
windowsButton=new JButton("Windows");
add(metalButton);
add(motifButton);
add(windowsButton);
metalButton.addActionListener(this);
motifButton.addActionListener(this);
windowsButton.addActionListener(this);
}
Dublic void actionPerformed(ActionEvent evt)
{Object source=evt.getSource();
String plaf="":
if(source= =metalButton)
plaf="javax.swing.plaf.metal.MetalLookAnd-
Feel";
else if(source= =motifButton)
plaf="com.sun.java.swing.plaf.motif.Moti-
fLookAndFeel";
else if(source= =windowsButton)
Dlaf="com.sun.java.swing.plaf.windows.Win-
dowsLookAndFeel";
try
{UIManager.setLookAndFeel( );
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){)
}
private JButton metalButton;
private JButton motifButton;
private JButton windowsButton;
}
class PlafFrame extends JFrame
{public PlafFrame()
{ setTitle("simple");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(O);
}
});
Container contentPane=getContentPane();
contentPane.add(new PlafPanel());
}
}
public class java2
{public static void main(String[]args)
f JFrame frame=new PlafFrame();
frame.show();
}
问答题本程序中,窗口的背景色为系统桌面的颜色,在窗口中分别画了空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭圆形,并且画了白色粗体的“欢迎学习Java!”字符串。importjava.awt.*;importjavax.swing.*;publicclassexam_30{publicstaticvoidmain(String[]args){FillFrameframe=newFillFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classFillFrameextendsJFrame{publicFillFrame(){setTitle("exam_30");setSize(WIDTH,HEIGHT);FillPanelpanel=newFillPanel();panel.setBackground(SystemColor.desktop);ContainercontentPane=getContentPane();contentPane.add(panel);}publicstaticfinalintWIDTH=400;publicstaticfinalintHEIGHT=250;}classFillPanelextendsJPanel{publicvoidpaintComponent(Graphicsg){______;gsetColor(newColor(10,10,10));gdrawRect(10,10,100,30);gsetColor(newColor(100,100,100));gdrawRoundRect(150,10,100,30,15,15);gsetColor(newColor(150,150,150));gdrawOval(280,10,80,30);gsetColor(newColor(10,10,10));g.fillRect(10,110,100,30);g.setColor(newColor(100,100,100));g.fillRoundRect(150,110,100,30,15,15);g.setColor(newColor(150,150,150));g.fillOval(280,110,80,30);g.setColor(Color.white);Fontf=newFont("宋体",______,20);g.setFont(f);g.drawString("欢迎学习Java!",150,200);}}
问答题本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java3 extends JFrame implements ActionListener public java3() setTitle("java3"); setSize(300,300); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); JMenuBar mbar=new JMenuBar(); setJMenuBar(bar); JMenu fileMenu=new JMenu("File"); mbar.add(fileMenu); connectItem=new JMenuItem("Connect"); connectItem.addActionListener(this); fileMenu.add(connectItem); exitItem=new JMenuItem("Exit"); exitItem.addActionListener(this); fileMenu.add(exitItem); public void actionPerformed(ActionEvent evt) Object source=evt.getSource(); if(source==connectItem) ConnectInfo transfer=new ConnectInfo("yourname","pw"); if(dialog==null) dialog=new ConnectDialog(this); if(dialog.showDialog(transfer)) String uname=transfer.username; String pwd=transfer.password; Container contentPane=getContentPane(); contentPane.add(new JLabel("username="+uname+",password="+pwd),"South"); validate(); else if(source==exitItem) System.exit(0); public static void main(String[]args) JFrame f=new java3(); f.show(); private ConnectDialog dialog=null; private JMenuItem connectItem; private JMenuItem exitItem; class ConnectInfo public String username; public String password; public Connectfnfo(String u,String p) username=u;password=p; class ConnectDialog extends JDialog implements ActionListener public ConnectDialog() super(parent,"Connect",true); Container contentPane=getContentPane(); JPanel p1=new JPanel(); p1.setLayout(new GridLayout(2,2)); p1.add(new JLabel("User name:")); p1.add(username=new JTextField("")); p1.add(new JLabel("Password:")); p1.add(password=new JPasswordField("")); contentPane.add("Center",p1); Panel p2=new Panel(); okButton=addButton(p2,"Ok"); cancelButton=addButton(p2,"Cancel"); contentPane.add("South",p2); setSize(240,120); JButton addButton(Container c,String name) JButton button=new JButton(name); button.addActionListener(this); c.add(button); return button; public void actionPerformed(ActionEvent evt) Object source=evt.getSource(); if(source==okButton) ok=true; setVisible(false); else if(source==cancelButton) setVisible(false); public void showDialog(ConnectInfo transfer) username.setText(transfer.username); password.setText(transfer.password); ok=false; show(); if(ok) transfer.username=username.getText(); transfer.password=new String(password.getPassword()); return ok; private JTextField username; private JPasswordField password; private boolean ok; private JButton okButton; private JButton cancelButton;
问答题1.setText("请正确输入!");
问答题本程序是计算自然数1~20的和。
public class exam_22{
public static void main(String[] args) {
int n=20;
______;
do{
sum=sum+n;
______;
}while(______);
System.out.println("sum="+sum);
}
}
问答题本题是一个计时器。窗口中有1个文字标签和3个按钮,名为“复位”、“开始”和“暂停”,初始状态时只有“开始”按钮可用,单击该按钮开始计时,并且“开始”按钮的标签变为“继续”且不可用,“暂停”按钮变为可用,单击“暂停”按钮后“暂停”按钮变为不可用,“复位”和“继续”按钮变为可用,此时如果单击“复位”按钮将恢复到初始状态,如果单击“继续”按钮,则继续进行计数。 import java.awt.*; import java.awt.event.*; public class java3 extends Frame extends Runnable Label timeDisp=new Label("0:0",Label.CENTER); Thread timerThread; int time=0; Button btReset=new Button("复位"); Button btStart=new Button("开始"); Button btStop=new Button("暂停"); java3() super("java3"); Panel p=new Panel(new GridLayout(1,0)); btReset.setEnabled(false); btStop.setEnabled(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); btReset.addActionListener(new ResetListener()); btStop.addActionListener(new StopListener()); btStart.addActionListener(new StartListener()); p.add(btReset); p.add(btStart); p.add(btStop); add(p,BorderLayout.SOUTH); timeDisp.setFont(new Font("Courier",Font.BOLD,60)); add(timeDisp,BorderLayout.CENTER); pack(); show(); void stop() Thread t=timerThread; if(t!=null) timerThread=null; tryt.join();catch(Exception e) public void run() while(timerThread==Thread.currentThread()) timeDisp.setText(""+time/10+":"+time%10+"0"); time++; tryThread.sleep(100);catch(Exception e); class ResetListener implements ActionListener public void actionPerformed(ActionEvent evt) this.stop(); timeDisp.setText("0:0"); time=0; btReset.setEnabled(false); btStart.setEnabled(true); btStop.setEnabled(false); btStart.setLabel("开始"); class StopListener implements ActionListener public void actionPerformed(ActionEvent evt) java3.this.stop(); btReset.setEnabled(true); btStart.setEnabled(true); btStop.setEnabled(false); btStart.setLabel("继续"); class StartListener implements ActionListener public void actionPerforrned(ActionEvent evt) timerThread=new Thread(java3.this); timerThread.start(); btReset.setEnabled(false); btStart.setEnabled(false); btStop.setEnabled(true); btStart.setLabel("继续"); static public void main(String[]args) new java3();
问答题本程序的功能是打印出指定类的所有属性,包括类的构造函数、类的属性变量和类的方法。例如运行程序时指定方法为“java.util.Date”,则打印出类Date的所有信息。
import java.lang.reflect.*;
public class exam_22 {
public static void main(String[] args) throws ClassNotFoundException {
if(args.length<1) {
System.out.println("请输入类的名称!");
return;
}
______c=Class.forName(args[0]);
print_class(c);
}
public static void print_class (Class c)
{
if(c.isInterface()) {
System.out.print(Modifier.toString(c.getModifiers()) + " " +
typename(c));
}
else if (c.getSuperclass() != null) {
System.out.print(Modifier.toString(c.getModifiers()) + " class " +
typename(c) +
" extends " + typename(c.getSuperclass()));
}
else {
System.out.print(Modifier.toString(c.getModifiers()) + " class " +
typename(c));
}
Class[] interfaces=c.getInterfaces();
if ((interfaces != null)
else System.out.print(" implements ");
for(int i=0; i < interfaces.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(typename(interfaces[i]));
}
}
System.out.println(" {");
System.out.prlntln(" // Constructors");
Constructor[] constructors=c.getDeclaredConstructors();
for(int i=0; i < constructors.length; i++)
print_method_or_constructor(constructors[i]);
System.out.println(" // Fields");
Field[] fields=c.getDeclaredFields();
for(int i=0; i < fields.length; i++)
print_field(fields[i]);
System.out.println(" // Methods");
______=c.getDeclaredMethods();
for(int i=0; i < methods.length; i++)
print_method_or_constructor(methods[i]);
System.out.println("}");
}
public static String typename(Class t) {
String brackets=" ";
while(t.isArray()) {
brackets += "[]";
t=t.getComponentType();
}
String name=t.getName();
int pos=name.lastIndexOf(".");
if (pos != -1) name=name.substring(pos+1);
return name + brackets;
}
public static String modifiers(int m) {
if (m == 0) return " ";
else return Modifier.toString(m) + " ";
}
public static void print_field(Field f) {
System.out.println(" " + modifiers(f.getModifiers()) +
typename(f.getType()) + " " + f.getName() + ";");
}
public static void print_method_or_constructor (Member member)
Class returntype=null, parameters[], exceptions[];
if (member instanceof Method) {
Method m=(Method) member;
returntype=m.getReturnType();
parameters=m.getParameterTypes();
exceptions=m.getExceptionTypes();
System.out.print(" " + modifiers(member.getModifiers()) +
typename(returntype) +" "+ member.getName() +
"(");
} else {
Constructor c=(Constructor) member;
parameters=c.getParameterTypes();
exceptions=c.getExceptionTypes();
System.out.print(" " + modifiers(member.getModifiers()) +
typename(c.getDeclaringClass()) + "(");
}
for(int i=0; i < parameters.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(typename(parameters[i]));
}
System.out.print (")");
if (exceptions.length > 0) System.out.print(" throws ");
for(int i=0; i < exceptions.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(typename(exceptions[i]));
}
System.out.println(";");
}
}
问答题___________________
问答题注意:下面出现的“考生文件夹”均为%USER%在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行“//**********Found**********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。本题的要求是:请阅读程序Java_3.java,该程序是比较两个整数的大小,运行程序,完成如下两个整数的输入:运行结果为:给定源程序://if语句实例importjavax.swing.JOptionPane;publicclassJava_3publicstaticvoidmain(Stringargs[])StringfirstNumber;//存储第一个输入数据secondNumber;//存储第二个输入数据result;//字符串输出intnumberl;//用来比较的第一个int型数据number2;//用来比较的第二个int型数据//以字符串格式读输入数据firstNumber=JOptionPane.showInputDialog("请输入第1个整数:");secondNumber=JOptionPane.showInputDialog("请输入第2个整数:");//将字符串转换为int整数//*********Found********number1=Integer.parseInt(______);number2=Integer.parseInt(secondNumber);//用空字符串初始化结果变量result="";if(number1==number2)result=number1+"=="+number2;if(number1!=number2)result=numberl+"!="+number2;if(number1<number2)result=result+"/n"+number1+"<"+number2;if(number1>number2)result=result+"/n"+number1+">"+number2;if(number1<=number2)result=result+"/n"+number1+"<="+number2;if(number1>=number2)result=result+"/n"+number1+">="+number2;//显示结果JOptionPane.showMessageDialog(null,result,"比较结果",//********Found********JOptionPane.______);System.exit(0);
问答题下列程序中,指定变量b=20为字节类型,变量f=0.34F为单精度类型,变量1=0xeefL为64位长整型,并将该三个变量打印输出。请将程序补充完整。 注意:不改动程序结构,不得增行或删行。 import java.io.*; public class Assign public static void main(String args []) ______b=20; ______f=0.34F; ______l=0xeefL; System.out.println("b="+b); System.out.println("f="+f); System.out.println("l="+1);
问答题以下程序是一个简单文本处理器,菜单项可以打开、编辑、保存一个文件。文件内容显示在下面的文本区域中(提示,打开文件通过文件选择器来完成)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。运行结果如下图所示。注意:不改动程序的结构,不得增行或删行。importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;classFileFrameextendsJFrame{Filefile;JTextPanetextpane;FileInputStreamreadStream;JScrollPanescroll;publicFileFrame(){super("文件浏览");JMenufileM=newJMenu("文件");OpenActionopen=newOpenAction();SaveActionclear=newSaveAction();ExitActionexit=newExitAction();JMenuBarmb=newJMenuBar();fileM.add(open);fileM.add(clear);fileM.add(exit);mb.add(fileM);textpane=newJTextPane();scroll=newJScrollPane(textpane);getContentPane().add(scroll);{{U}}getContentPane().addJMenuBar(mb){{/U}};}classOpenActionextendsAbstractAction{publicOpenAction(){super("打开");}publicvoidactionPerformed(ActionEvente){JFileChooserchooser=newJFileChooser();intstate=chooser.showOpenDialog(null);{{U}}file=chooser.selectedFile(){{/U}};if(file!=nulltextpane.read(readStream,this);readStream.close();}catch(IOExceptionioE){}}}}classSaveActionextendsAbstractAction{publicSaveAction(){super("保存");}publicvoidactionPerformed(ActionEvente){if(file==null)return;try{FileWriterout=newFileWriter(file);{{U}}out.read(textpane.getText()){{/U}};out.close();}catch(IOExceptionioE){}}}classExitActionextendsAbstractAction{publicExitAction(){super("退出");}publicvoidactionPerformed(ActionEvente){System.exit(0);}}publicstaticvoidmain(Stringargv[]){FileFramef=newFileFrame();f.setDefaultCloseOperation(JFrame.EXITONCLOSE);f.setSize(300,400);f.show();}}
问答题本程序中,主窗口中有一个“增大”按钮,单击该按钮后,按钮的大小增大,继续单击,按钮将继续增大,如图所示。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassexam_39extendsJFrame{publicexam_39(){super("exam_39");setSize(300,150);setDefaultCloseOperation(EXIT_ON_CLOSE);Fontfont=newFont("Dialog",Font.PLAIN,10);finalJButtonb=newJButton("增大");b.setfont(font);Containerc=getContentPane();c.setLayout(newFlowLayout());c.add(b);addActionListener(newActionListener(){intsize=10;publicvoidactionPerformed(ActionEventev){b.setFont(newFont("Dialog",Font.PLAIN,++size));b.revaliDate();}});}publicstaticvoidmain(String[]args){exam_39re=newexam_39();re.setVisible(true);}}
问答题请完成下列Java程序。程序的功能是对数组进行冒泡排序。冒泡排序算法的基本思路是把当前数据序列中的各相邻数据两两比较,发现任何一对数据间不符合升序或降序关系则立即调换它们的顺序,从而保证相邻数据间符合升序或降序的关系。本题是进行升序排列。例如数组a[]={30,1,-9,70,25}则排序后为{-91253070}。注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:publicclassBubbleSort{publicstaticvoidmain(Stringargs[]){inti,j;inta[]={30,1,-9,70,25};intn=a.length;System.out.print("排序前数组为:");for(i=0;i<n;i++)System.out.print(a[i]+"");System.out.println();for(i=1;i<n;i++)for(j=0;______j++)if(a[j]>a[j+1]){inttemp=a[j];______;a[j+1]=temp;}System.out.print("排序后数组为:");for(i=0;i<n;i++)System.out.print(a[i]+"");}}