单选题下列关于程序运行结果的描述中,错误的是(  )。import javax.swing.*; import java.awt.*; public class Test{ public static void main(String[]args){ JFrame f = new JFrame(); JPanel P=new JPanel(); f.setLayout(new BorderLayout()); f.getContentPane().add(p,"Center"); P.setBackground(Color.blue); f.setVisible(true); f.setSize(200,200); } }A程序不能运行B显示一个窗口C窗口大小是200×200D窗口背景为蓝色

单选题
下列关于程序运行结果的描述中,错误的是(  )。import javax.swing.*; import java.awt.*; public class Test{ public static void main(String[]args){ JFrame f = new JFrame(); JPanel P=new JPanel(); f.setLayout(new BorderLayout()); f.getContentPane().add(p,"Center"); P.setBackground(Color.blue); f.setVisible(true); f.setSize(200,200); } }
A

程序不能运行

B

显示一个窗口

C

窗口大小是200×200

D

窗口背景为蓝色


参考解析

解析:
该程序没有语法和逻辑错误,所以可以运行。由JFrame f=new JFrame()可以看出,该程序创建了一个窗口,P.setBackground(Color.blue)告诉我们,窗口的背景颜色是蓝色的,而f.setSize(200,200)则设置了窗口的大小,最后窗口是可见的。

相关考题:

下面程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过表格JTable的实例显示。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。import java.awt.*;import javax.swing.*;import java.util. Date;import javax.swing.table.*;import java.applet.*;import java.io.*;public class Example3_10 extends JApplet, JFrame{public void init(){FileModel fm = new FileModel();JTable jt = new JTable();jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);jt.setColumnSelectionAllowed(true);JScrollPane jsp = new JScrollPane(jt);getContentPane().add(jsp, BorderLayout.CENTER);}public static void main(String args[]){Example3_10 ft = new Example3_10();ft.init();JFrame. f = new JFrame();f.setDefaultCloseOperation(JFrame.EXIT ON CLOSE);f.getContentPane() .add(ft.getContentPane());f.setSize (300, 400);f.show();}}class FileModel extends AbstractTableModel{String[] columnName = new String[] {"文件名", "大小", "最后修改时间"}Object[] [] data;public FileModel() { this("."); }public FileModel(String dir){File file = new File(dir);String files[] = file.list();data = new Object [files.length] [columnName.length];for (int i=0; i < files.length; i++){File tmp = new File (files[i]);data[i] [0] = tmp.getName();data[i] [1] = new Long(tmp.length());data[i] [2] = new Date(tmp.lastModified() );}}public int getColumnNumber(){return columnName.length;}public int getRowCount(){return data. length;}public String getColumnName(int col){return columnName[col];}public Object getValueAt(int row, int col){return data[row] [col];}public Class getColumnClass(int c){return getValueAt (0, c) .getClass();}}

本题中,用表格表现某个月的月历,其中标题是从Sunday到Saturday,表格中的各项是可以修改的。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class java2 ( public static void main(String[]args) { try{ UIManager.setLookAndFeel(UIManager.getSys- temLookAndFeelClassName): } catch(Exception e) JFrame. frame=new CalendarTableFrame; frame.setDefaultCloseOperation(JFrame.EXIT_ oN CLOSE); frame.show; } } clasgCalendarTableFrame. extends JFrame { private static final int WIDTH=500; private static final int HEIGHT=150: private cells= { {null,null,null,new Integer(1),new Integer (2),new Integer(3),new Integer(4)), {new Integer(5),new Integer(6),new Integer (7).new Integer(8),new Integer(9),new Integer (10),new Integer(11)), {new Integer(12),new Integer(13),new Integer (14),new Integer(15),new Integer(16),new Integer (17),new Integer(18)), {new Integer(19),new Integer(20),new Integer (21),new Integer(22),new Integer(23),new Integer (24),new Integer(25)), {new Integer(26),new Integer(27),new Integer (28),new Integer(29),new Integer(30),new Integer (31),null} }; private String[]columnNames={ "Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday" }; public CalendarTableFrame{ setTitle("java2"); setSize(WIDTH,HEIGHT); JTable table=new ; getContentPane.add(new JScrollPane(table), BorderLayout.CENTER); } }

本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有4个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class java3 { public static void main(String[]args) { JFrame. frame=new TransformTestFrame; frame.setDefaultCloseOperation(JFrame.EXIT_ 0N_CLoSE); frame.show; } } class TransformTest extends JFrame { public TransformTestFrame { setTitle("java3"); setSize(WIDTH,HEIGHT); Container contentPane=getContentPane; canvas=new TransformPanel; contentPane.add(canvas,BorderLayout.CEN- TER); JPanel buttonPanel=new JPanel: ButtonGroup group=new ButtonGroup; JRadioButton rotateButton =new JRadioButton("旋转",true); buttonPanel.add(rotateButton); group.add(rotateButton); rotateButton.addActionListener(new ActionListener { public void actionPerformed(ActionEvent event) { canvas.setRotate; } }); JRadioButton trans|ateButton =new JRadioButton("移位",false); buttonPanel.add(translateButton); group.add(translateButton); translateButton.addActionListener(new ActionLiatener public void actionPerformed(ActionEvent event) { canvas.setTranslate; } }); JRadioButton scaleButton =new JRadioButton("放大",false); buttonPanel.add(scaleButton): group.add(scaleButton); scaleButton.addActionListener(new ActionListener { public void actionPerformed(ActionEvent event) { canvas.setScale; } }); JRadioButton shearButton =new JRadioButton("剪切",false); buttonPanel.add(shearButton): group.add(shearButton); shearButton.addActionListener(new ActionListener { public void actionPerformed(ActionEvent event) { canvas.setShear; } }); contentPane.add(buttonPanel,BorderLayout. NORTH): } private TransformPanel canvas; private static final int WIDTH=300; private static final int HEIGHT=300; } class TransformPanel extend JPanel { public TransformPanel { square=new Rectangle2D.Double(-50,-50, 100,100); t = new AffineTransform; setRotate; } public void paintComponent { super.paintComponent(g); Graphics2D g2=(Graphies2D)g; g2.translate(getWidth/2,getHeight/2): g2.setPaint(Color.gray); g2.draw(square); g2.transform(t); g2.setPaint(Color.black); g2.draw(square); } public void setRotate { t.setToRotation(Math.toRadians(30)); repaint; } public void setTranslate { t.setToTranslation(20,1 5); repaint; } public void setScale { t.setToScale(2.0,1.5): repaint; } public void setShear { t.setToShear(-0.2,O); repaint; } private Rectangle2D square; private AffineTransform. t; }

请完成下列Java程序:用一个边框布局来安排一个工具栏和一个卡片布局。工具栏可浮动,包括两个按钮,一个向前一个向后控制卡片的显示;卡片布局包括3张卡片,分别标识为card1、card2、card3。注意;请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。源程序代码文件清单如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.LineBorder;public class ex20_2{private static CardLayout cl=new CardLayout();private static JPanel cp=new JPanel();public static void main(String args[]){JFrame. jf=new JFrame();jf.setTitle("ex20_2");Container c=jf.getContentPane();JToolBar jtb=new JToolBar();JButton btnNext=new JButton(new Imagelcon("next.gif"));JButton btnPre=new JButton(new ImageIcon("back.gif"));jtb.add(btnPre);jtb.add(btnNext);cp.setLayout(c1);Dimension dim=new Dimension(150,80);cp.setPreferredSize(dim);cp.setBorder(new LineBorder(Color.BLACK));cp.add(new JLabel("card1",JLabel.CENTER),"card1");cp.add(new JLabel("card2",JLabel.CENTER),"card2");cp.add(new JLabel("card3",JLabel.CENTER),"card3");c.add(jtb,BorderLayout.NORTH);c.add(cp,BorderLayout.CENTER);ActionListener al=new ActionResponse();______;______;if.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}});jf.pack();jf.setVisible(true);}static class ActionResponse implements ActionListener{public void actionPerformed(ActionEvent ae){if(ae.getActionCommand().equals("next"))c1.next(cp);elsec1.previous(cp);}}}

下列代码的功能是把按钮save添加到窗口myFrame的中间,在空白处应填入的代码是______。import Javax.swing.*;public calss MyFrame{public static void main(String args[]){JFrame. myFrame=new JFrame();JButton save=new JButton("Save");myFrame.getContentPane()add(save,"______");myFrame.setSize(200,200);myFrame.setVisible(true);}}

本程序的功能是读取用户输入的整数a、b,单击按钮“计算”,则计算出a和b数的和,并显示计算的结果。请将程序补充完整。注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class simple{public static void main{String[] args){ExampleFrame. frame=new ExampleFrame();frame.______(JFrame.EXIT_ON_CLOSE);frame.show();}}class ExampleFrame. extends JFrame{private JPanel panel;public static final int DEFAULT_WIDTH=250;public static final int DEFAULT_HEIGHT=250;public ExampleFrame(){Init();setTitle("welcome");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);Container contentPane=getContentPane();contentPane.add(panel);}private void init(){JLabel la1=new JLabel("输入整数a:");JLabel la2=new JLabel ("输入整数b:");final JTextField num1=new JTextField(10);final JTextField num2=new JTextField(10);JButton but=new JButton("计算");final JLabel res=new JLabel("结果:");but.addActionListener(______{public void actionPerformed(ActionEvent event){String str=new String();String s1=num1.getText();String s2=num2.getText();try{int a=Integer.parseInt(s1);int b=Integer.parseInt(s2);str=String.valueOf(a +b);}catch(Exception e){str=e.getMessage();}res.setText ("结果:"+str);}});panel=new JPanel();panel.setLayout(new FlowLayout(FlowLayout.LEFT));panel.add(la1);panel.add(num1);panel.add(la2);panel.add(hum2);panel.add(but);panel.add(res);}}

本程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过表格JTable的实例显示。请更正题中带下划线的部分,使程序能输出正确的结果。注意:不改变程序的结构,不得增行或删行。import java.awt.*;import javax.swing.*;import java.util.Date;import javax.swing.table.*;import java.applet.*;import java.io.*;public class advance extends JApplet, JFrame{public void init(){FileModel fm=new FileModel();JTable jt=new JTable();jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);jt.setColumnSelectionAllowed(true);JScrollPane jsp=new JScrollPane(jt);getContentPane().add(jsp, BorderLayout.CENTER);}public static void main(String args[]){advance ft=new advance();ft.init();JFrame. f=new JFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.getContentPane().add(ft.getContentPane());f.setSize(300,400);f.show();}}class FileModel extends AbstractTableModel{String[] columnName=new String[] {"文件名","大小","最后修改时间"};Object[][] data;public FileModel() {this(".");}public FileModel(String dir){File file=new File(dir);String files[]=file.list();data=new Object[files.length] [columnName.length];for(int i=0; i<files.length; i++){File tmp=new File(files[i]);data[i] [0]=tmp.getName();data[i] [1]=new Long(tmp.length());data[i] [2]=new Date(tmp.lastModified());}}public int getColumnNumber(){return columnName.length;}public int getRowCount(){return data.length;}public String getColumnName(int col){return columnName[col];}public Object getValueAt(int row, int col){return data[row] [col];}public Class getColumnClass(int c){return getValueAt(0,c).getClass();}}

请完成下列Java程序:创建一个进度条,可以控制其开始和暂停。要求进度条与线程关联,由线程控制,点击开始按钮开始进度条的滚动,点击停止按钮,暂停,继续点击开始按钮则继续进行。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;public class ex15_2 extends JPanel {ThreadProg thProgress;JProgressBar jPb;//声明一个进程条对象static JFrame. jf;public ex15_2(){setLayout(new BorderLayout());jPb=new JProgressBar();add(jpb,"Center");JPanel btnPanel=new JPanel();JButton btnStart=new JButton("开始");btnPanel.add(btnStart);btnStart.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){__________;}});JButton btnStop=new JButton("停止");btnPanel.add(btnStop);btnStop.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){__________;}});add(btnPanel,BorderLayout.SOUTH);}public void startProgress(){if(thProgress==null||!thProgress.isAlive()){thProgress=new ThreadProg(jpb);thProgress.start();}}public void stopProgress(){thProgress.setStop(true);}pubUc static VOid main(String args[]){jf=new JPrame("ex15_2");ex15_2 obj15_2=new ex15_2();jf.getContentPane().add("Center",obj15_2);jf.setSize(250,80);jf.addWindowListener(new WindowAdapter(){pubhc void windowClosing(WindowEvent we){System.exit(0);}});jf.setVisible(true);}}class ThreadProg extends Thread{private JProgressBar jpb;private boolean bStop=false;private boolean bAlive=false;pubhc ThreadProg(JProgressBar jpb){this.jpb=jpb;}public VOid setStop(boolean b){bStop=b;}public void run(){int min=0;int max=60;jpb.setMinimum(min);jpb.setMaximum(max);jpb.setValue(min);for(int x=min;x<=max;x++){if(bStop){break;}else{jpb.setValue(x);try{Thread.sleep(150);} catch(InterruptedException e){}}}bAlive=false;}}

请完成下列Java程序:用JFrame建立—个窗口,再用网格布局管理器,对窗口布局进行控制,上面有2行2列4个按钮,要求对按钮的大小进行设定,宽度为150,高度为80。注意:请勿改动main主方法和其他已有语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ex19_2{public static void main(String args[])}JFrame. frm=new JFrame();frm.setTitle("ex19_2");Container cont=frm.getContentPane();______;cont.add(new JButton("button1"));cont.add(new JButton("button2 "));Dimension dimen=______;JButton btn1=new JButton("button3");btn1.setPreferredSize(dimen);cont.add(btn1);cont.add(new JButton("button4"));frm.addWindowListener(new WindowAdapter(){public void winowClosing(WindowEvent we){System.exit(0);}});frm.pack();frm.setVisible(true);}}

阅读以下说明和Java代码,将应填入(n)处的语句写在对应栏内。【说明】本程序通过移动滑动条修改颜色RGB值,从而控制颜色。程序中有一个面板、3个标签和3个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会发生对应的变化,如下图所示,滑动条值的范围是0~255。【Java代码】import java.awt.*;import java.awt.event.*;import javax.swing.*;public class simple extends JFrame. implements AdjustmentListener{public simple(){setTitle("simple");setSize(300, 200);addWindowListener(new WindowAdapter(){public void windowClosing((1)){System.exit(0);}});Container contentPane=getContentPane();JPanel p=(2);p.setLayout(new GridLayout(3, 2));p.add(redLabel=new JLabel("Red 0"));p.add(red=new JScrollBar(Adjustable. HORIZONTAL, 0, 0, 0, 255));red.setBlocklncrement(16);red.addAdjustmentListener(this);p.add(greenLabel=(3) ("Green 0"));p.add(green=new JScrollBar(Adjustable.HORIZONTAL 0, 0, 0, 255));green setBIocklncrement(16);green.addAdjustmentListener(this);p.add(blueLabel=new JLabel("Blue 0"));p.add(btue=new JScrollBar(Adjustable. HORIZONTAL, 0, 0, 0, 255));blue,setBIocklncrement(16);blue.addAdjustmentListener(this);contentPane.add(p, "South");colorPanet=new JPanel();colorPanet.setBackground(new Color(0, 0, 0));contentPane.add((4),"Center");} public void adjustmentValueChanged(AdjustmentEvent evt){redLabel.setText("Red"+red.getValue());greenLabel.setText("Green"+green.getValue());blueLabel.setText("Blue"+blue.getValue());coiorPanel.setBackground(new Color(red.getValue(), green.getValue(), blue.getValue()));colorPanel.repaint();}public static void main(String[] args){JFrame. f=(5);f.show();}private JLabel redLabel;private JLabel greenLabel;private JLabel blueLabel;private JScrollBar red;private JScroilBar green;private JScrollBar blue;private JPanel colorPanel;

请完成下列Java程序:用JFrame建立一个窗口,再用网格布局管理器,对窗口布局进行控制,上面有2行2列4个按钮,要求对按钮的大小进行设定,宽度为150,高度为80。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ex9_2{public static void main(String[]args) {JFrame. frm:new JFrame();frm.setTitle("ex9_2");Container cont = frm.getContentPane();_____________;cont.add(new JButton("button1"));cont.add(new JButton("button2"));Dimension dimen=____________;JButton btn1 = new JButton("button3");btn1.setPreferredSize(dimen);cont.add(btn1);cont.add(new JButton("button4"));frm.addWindowListener(new WindowAdapter() {public void winowClosing(WindowEvent we){System.exit(0);}});frm.pack();frm.setVisible(true);}}

请完成下列Java程序;实现JComboBox,包含3个选项,分别是java、c++、vb,以及1个文本区,用于显示选择结果。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:import java.awt.event.*;import javax.swing.*;import java.awt.*;public class ex14_2 extends JFrame. implements ItemListener{BorderLayout bLay = new BorderLayout();JTextField jtf = new JTextField(27);JComboBox jcb = new JComboBox();public ex14_2(){super ("ex14_2");//调用当前类ex14_2的父类JFrame的构造方法。jcb.addItemListener(this);jcb.addItem("java");jcb.addItem("c++");jcb.addItem("vb");jcb.setEditable(false);jtf.setHorizontalAlignment(SwingConstants.CENTER);jtf.setEditable(false);JPanel jp = new JPanel();jp.setLayout(bLay);jp.add(jtf, "South");jp.add(jcb, "Center");setContentPane(jp);}public static void main(String[] args){JFrame. frame. = new ex14_2();frame.pack();frame.setVisible(true);}public void itemStateChahged(ItemEvent ie){Object o =if (o == job){Object newO;______;jtf.setText(newO.toString()+"被选中!");}repaint();}}

下列程序在Frame中设定BorderLayout布局管理器,选择正确的语句填入程序的横线处。 import java.awt.*; public class ex43 extends Frame { public static void main(String[] args) { ex43 bj = new ex43("BorderLayout"); ______ obj.add("North", new Button("North")); obj.add("South", new Button("Sourth")); obj.add("East", new Button ("East")); obj.add("West", new Button ("West")); obj. add ("Center", new Button ( "Center" ) ); obj.pack(); obj. setVisible (true); } public ex43(String str) { super (str); } }A.obj.setLayout(new BorderLayout());B.setLayout(new Borderkayout());C.setLayout(BorderLayout());D.obj.setLayout(BorderLayout());

请完成下列Java程序:用一个边框布局来安排一个工具栏和一个卡片布局。工具栏可浮动,包括2个按钮,一个向前一个向后控制卡片的显示;卡片布局包括3张卡片,分别标识为card1,card2,card3。注意;请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border. LineBorder;public class ex24_2{private static CardLayout cl=new CardLayout();private static JPanel cp=new JPanel();public static void main(String[] args){JFrame. if=new JFrame();jf.setTitle("ex24_2");Container c=jf.getContentPane();JToolBar jtb=new JToolBar();JButton btnNext=new JButton(new ImageIcon("next.gif"));JButton btnPre=new JButton(new ImageIcon("back.gif"));jtb.add(btnPre);jtb.add(btnNext);cp.setLayout(cl);Dimension dim=new Dimension(150,80);cp.setPreferredSize(dim);cp.setBorder(new LineBorder(Color. BLACK));cp.add(new JLabel("cardl",JLabel.CENTER),"card1");cp.add(new JLabel("card2",JLabel.CENTER),"card2");cp.add(new JLabel("card3",JLabel.CENTER),"card3");c.add(jtb,BorderLayout.NORTH);c.add(cp,BorderLayout.CENTER);ActionListener al=new ActionResponse();____________________________________;____________________________________;jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}});jf.pack();jf.setVisible(true);}static class ActionResponse implements ActionListener{public void actionPerformed(ActionEvent ae){if(ae.getActionCommand().equals("next"))cl.next(cp);elsecl.previous(cp);}}}

请在下列程序的横线处填入正确的语句。 import java.awt.*; import java.awt,event.*; public class ex16 { Frame. f; TextArea ta; Button btn; public static void main(String[] args) { ex16 e = new ex16(); e.m(); } public void m() { f = new Frame("ex16"); ta = new TextArea(); btn = new Button("ok"); btn.addActionListener(new MyAction()); f.add(ta, "Center"); f.add(btn, "South"); f.setSize(100, 100); f.setVisible (true); } class MyAction implements ActionListener { ______ { System. out.println (ta. getText ()); } } }A.static void actionPcrformcd(AcfionEvent e)B.public void action(ActionEwent e)C.public actionPerformed(ActionEvent e)D.public void actionPerformed(ActionEvent e)

选择正确的语句填在下列程序的横线处。 import java.awt.*; import java.awt.event.*; public class ex30 { Frame. f; public static void main(String[] args) { ex30 e = new ex30(); e. te st ( ); } public void test() { f = new Frame("ex30"); ______ f.paok(); f.setVisible(true); } class MyWindowListener extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(1); } } }A.f.addWindowListener(new MyWindowtistener());B.f.addWindowListener(MyWindowListener());C.f.addWindowAdapter(new MyWindowListener());D.addWindowListener(new MyWindowListener());

请完善程序(程序文件名:Java_3.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。[题目要求]生成下面左边图形界面,单击图中的New按钮,弹出如右图所示的对话框。源程序:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Java_3 {public static void main(String[] args) {MulticastFrame. frame=new MulticastFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}class MulticastFrame. extends JFrame. {public MulticastFrame() {setTitle("MulticastTest");setSize(WIDTH,HEIGHT);MulticastPanel panel=new MulticastPanel();Container contentPane=getContentPane();contentPane.add( (1) );}public static final int WIDTH=300;public static final int HEIGHT=200;}class MulticastPanel extends JPanel }public MulticastPanel() {JButton newButton=new JButton("New");add(newButton);ActionListener newListener=new ActionListener() {public void actionPerformed(ActionEvent event) {makeNewFrame();}};newButton.addActionListener(newListener);closeAllButton=new JButton("Close all");add(closeAllButton);}private void makeNewFrame() {final BlankFrame. frame=new BlankFrame();frame.show();ActionListener closeAllListener=new ActionListener() {public void actionPerformed(ActionEvent event) {frame. (2) (); //使窗口隐藏或消除}};closeAllButton.addActionListener( (3) );}private JButton closeAllButton;}Class BlankFrame. extends JFrame. {public BlankFrame() {(4) ++;setTitle("Frame"+counter);setSize(WIDTH,HEIGHT);setLocation(SPACING*counter,SPACING*counter);}public static final int WIDTH=200;public static final int HEIGHT=150;public static final int SPACING=30;private static int counter=0;}

请选择正确的语句填入下列程序的横线处。 import javax. Swing.*; import java.awt.*; import java.awt.event.*; public class ex46 { static String str= "单击次数:"; static intcnt = 0; public static void main(String[] args) { try{ UIManager.setLookAndFeel(UIManager. getCrossPlatformLookAndFeelClassName()); } catch(Exception e) {} JFrame. jf = new JFame("ex46"); JButton jb = new JButton("Button"); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { cnt ++; j1.setText(str +cnt); } }); j1. setLabelFor (jb); jPanel jp = new JPanel(); jp.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25); jp.setLayout(new GridLayout(0, 1)); jp.add(jb); jp.add (j1); jf.getContenPanel() .add(jp, "Center"); jf.pack(); jf.setVisible (true); } }A.Labelj1 = new JLabel(str + "0");B.final JLabel j1 = new JLabel(str + "0");C.final JLabelj1 = JLabel(str + "0");D.JLabelj1 = new JLabel(str + "0");

阅读下面Applet程序 import javax.swing.*: import java.awt.*: public Class Test SwingApplet extends JApplet { JLabel 1=new JLabel("This is a Swing Applet."); public void init() { ______ } } 程序中下画线处应填入的正确选项是A.Container contentPane=getContentPane();contentPane.add(1);B.Jpanel contentPane=new JPanel();contentPane.add(1);C.contentPane.add(1);D.add(1);

本题用复选框来控制字体的显示,窗口中有一个标签和两个复选按钮,这两个复选按钮分别对应的字体的两个特点:加粗和倾斜,任意选中一个按钮或两个都选中,标签上的字符串就显示对应的字体。tmport.java.awt.*;import java.awt.event.*;import javax.swing.*;class CheckBoxFrame. extends JFrame. implements Ac-tionListener{public CheckBoxFrame(){setTitle("CheckBoxTest");setSize(300,200);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e)(System.exit(O);}});JPanel P=new JPanel();bold=addCheckBox(p,"Bold");italic=addCheckBox(P,"Italic");getContentPane().add(p,"South");panel=new CheekBoxTestPanel();getContentPane().add(panel,"Center");}public JCheckBox addCheekBox(JPanel p,Stringname){JCheckBox c=new JCheekBox(name):c.addActionListener( );P.add(c);return C;}public void {int m=(bold.isSelected()?Font.BOLD:O)+(italic.isSelected()?Font.ITALIC:O);panel.setFont(m);}private CheckBoxTestPanel panel;private JCheckBox bold;private JCheckBox italic;}class CheckBoxTestPanel extends JPanel{public CheckBoxTestPanel(){setFont(Font.PLAIN);}public void setFont(int m){setFont(new Font("SansSerif",m,12));repaint();}public void paintComponent(Graphics g){super.paintComponent(g);9.drawstring("The quick brown fox jumps overthe lazy dog.",0,50);}}public class java2{public static void main(String[]args){JFrame. frame=new CheckBoxFrame();frame.show();}}

本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。import java.awt.*import java.awt.event.*;import javax.swin9.*;public class java3 extends JFrame. implements ActionL-istener{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);connectltem=new JMenuhem("Connect");connecthem.addActionListener(this);fileMenu.add(connecthem);exithem=new JMenuhem("Exit");exithem.addActionListener(this);fileMenu.add(exithem);}public void actionPerformed(ActionEvent evt){Object source=evt.getSource();if(source= =connecthem){Connectlnfo transfer=new ConnectInfo ("your-name","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= =exitltem)System.exit(0);}public static void main(String[]args){JFrame. f=new java3();f.show();}private ConnectDialog dialog=null;private JMenuhem connecthem;private JMenuhem exithem;}class Connectlnfo{public String username;public String password;public Connectlnfo(String U,String p){username=u;password=P;}}class ConnectDialog extends JDialog implements Ac-tionListener{public ConnectDialog(){super(parent,"Connect",true);Container contentPane=getContentPane();JPanel pl=new JPanel();pl.setLayout(new GridLayout(2,2));pl.add(newJLabel("User name:"));pl.add(username=new JTextField(""));pl.add(new JLabel("Password:"));pl.add(password=new JPasswordField(""));contentPane.add("Center",pl);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(Connectlnfo transfer){username.setText(transfer.username);password.setText(transfer.password);ok=false;show();if(ok){transfer.username=username.getText();transfer.password=new String(password.get-Password());}return ok;}private JTextField usernameprivate JPasswordField password;private boolean ok;private JButton okButton;private JButton cancelButton;}

本题的功能是获取鼠标在窗口中的位置。当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比如显示为"鼠标的当前位置:X:Y"(其中,X为横坐标,Y为纵坐标)。import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class java2{public static void main(String[]args){MouseFrame. frame=new MouseFrame();frame.setDefaultCloseoperation(JFrame.EXIT_0N CLOSE);frame.show();}}class MouseFrame. extends JFrame{public MouseFrame(){setTitle("java2");setSize(WIDTH,HEIGHT);MousePanel panel=new MousePanel();Container contentPane=getContentPane();contentPane.add(panel);}public static final int WIDTH = 300;public static final int HEIGHT=200;}class MousePanel extends JPanel{public MousePanel(){addMouseListener(new MouseHandler());addMouseMotionListener(new MouseMotionHan-dler());}public void paintComponent(Graphics g)(super.paintComponent(g);String text="鼠标指针位置:"+mousex+":"+mousey;g.drawString(text,10,10);}private int mousex,mousey;private class MouseMotionHandler {public void mouseMoved(MouseEvent event){mousex=event.getX();mousey=event.getY();repaint();}public void mouseDragged(MouseEvent event){mousex=event.getX();mousey=event.getY();repaint();}}private class MouseHandler{public void mousePressed(MouseEvent eveat){mousex=event.getX();mousey=event.getY();}}}

本题的功能是求两个交叉图形的并、减、交和异或。窗口中有4个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中以黑色填充的方式显示运算结果。import java.awt.*;import java.awt.event*+;import java.awt.geom.*;import java.util.*;import javax.swing.*;public class java3{public static void main(String[]args){JFrame. frame=new AreaTestFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}class AreaTestFrame. extends JFrame{public AreaTestFrame(){setTitle("java3");setSize(WIDTH,HEIGHT);areal=new Area(new Ellipse2D.Double(100,100,150,100));area2=new Area(new Rectangle2D.Double(150,150,150,100));Container confentPane=getContentpane();panel=newJPanel(){public void paintComponent(Graphics g){super.paintComponent(g);Graphics2D g2=(Graphics2D)g;g2.draw(areal);g2.draw(area2);if(area!=null)g2.fill(area);}};contentPane. add(panel,BorderLayout. CEN-TER);JPanel buttonPanel=new JPanel();ButtonGroup group=new ButtonGroup();JRadioButton addButton=new JRadioButton("并",false);buttonPanel.add(addButton);group.add(addButton);addButton.addActionListener(newActionListener(){public void actionPerformed(ActionEvent event){area=new Area();area.add(areal);area.add(area2);panel.repaint();}});JRadioButton subtractButton=new JRadioButton("减",false);buttonPanel.add(subtractButton);group.add(subtractButton);subtractButton.addActionListener(newActionListener(){public void actionPerformed(ActionEvent event){area=new Area();area.add(areal);area.subTract(area2);panel.repaint();}});JRadioButton intersectButton=new JRadioButton("交",false);buttonPanel.add(intersectButton);group.add(intersectButton);intersectButton.addActionListener(newActionListener(){public void actionPerformed(ActionEvent event){area=new Area():area.add(areal);area.intersect(area2);panel.repaint();}});JRadioButton exclusiveCIrButton=new JRadioButton(“异或”,false);buttonPanel.add(exelusiveOrButton);group.add(exclusiveOrButton);exclusiveOrButton.addActionListener(newActionListener(){public void actionPerformed(ActionEvent event){area=new Area();area.add(areal);area.exclusiveor(area2);panel.repaint();}});contentPane.add(buttonPanel,BorderLayout.NORTH);}private JPanel panel;private Area area;private Area areal;private Area area2;private static final int WIDTH=400;private static final int HEIGHT=400;}

本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。import java.awt.*;import java.awt.event.*;import javax.swing.*;class ComboBoxFrame. extends JFrame. {public ComboBoxFrame(){setTitle("java2");setSize(300,200);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});style=new JComboBox():style.setEditable(true);style.addhem("Serif");style.addItem("SansSerif");style.addhem("Monospaced");style.addhem("Dialog");style.addhem("Dialoglnput");style.addActionListener(this);JPanel p=new JPanel();P.add(style);getContentPane().add(p,"South");panel=new ComboBoxTestPanel();getContentPane().add(panel,"Center");}public void actionPerformed(ActionEvent evt){JComboBox source=(JComboBox) ;String item=(String)source.getSelectedhem():panel.setStyle(item);}private ComboBoxTestPanel panel;private JComboBox style;}class ComboBoxTestPanel extends JPanel{public ComboBoxTestPanel(){setStyle("Serif");}public void setStyle(String s){setFont(new Font(S,Font.PLAIN,12));repaint();}public void paintComponent(Graphics g){super.paintComponent(g);9.drawString("Welcome to China!",0,50);}}public class java2{public static void main(String[]args){JFrame. frame=new ComboBoxFrame();frame.show();}}

本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示0~9、+、-、 *、/、一运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class java2{public static void main(String[]args){try{UIManager.setLookAndFeel(UIManager.getSys-temLookAndFeelClassName());}catch(Exception e){}JFrame. frame=new CalculatorFrame();frame.show();}}class CalculatorPanel extends JPanel implements Ac-tionListener{private JTextField display;private JButton btn;private double arg=0;private String p="=";private boolean start=true;public CalculatorPanel(){setLayout(new BorderLayout());display=new JTextField("0");display.setEditable(false);add(display,"North");JPanel P=new JPanel();P.setLayout(new GridLayout(4,4));String buttons="789/456*123-0.=+":for(int i=0;i<buttons.length();i++){btn=new JButton(buttons.substring(i,i+1));P.add(btn);;}add(P,"Center");}public void actionPerformed(ActionEvent evt){String s=evt.getActionCommand();if(‘0 ‘<=s.charAt(0)&&s.charAt(O)<=‘9‘‖ s.equals("-")){if(start)display.setText(s):else display.setText(display.getText()+s);start=false;}else{if(start){if(s.equals("-")){display.setText(s):start=false;}else p=S;}else(double x=calculate(x);op=S:start=true;}}}public void calculate(double n){if(op.equals("+"))arg+=n:else if(op.equals("-"))arg-=n;else if(op.equals("*"))arg*=n;else if(op.equals("/"))arg/=n;else if(op.equals("="))arg=n;display.setText(""+arg);}}class CalculatorFrame. extends JFrame{public CalculatorFrame(){setTitle("java2");setSize(220,180);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});Container contentPane=getContentPane();contentPane.add(new CalculatorPanel());}}

现有:  package mypack;      import java.awt.Color;  import java.awt.FlowLayout;      import javax.swing.JButton;      import javax.swing.JFrame;      import javax.swing.JPanel;  public class ButtList extends JFrame  {      public static void main(String[] args)  {      ButtList bl  =new ButtList();     }  ButtList(){  JPanel pl=new JPanel();  pl.setBackground (Color.pink);      p1.add(new JButton("one"));      p1.add(new JButton("Two"));     p1.add(new JButton("Three"));      add("South",p1);  setLayout(new Fl.wLayout());      setSize(200,200);      setVisible(true);     }     }  上述代码运行后的显示效果是哪项?() A、3个按钮从左到右排列在Frame的底部B、3个按钮从左到右排列在Frame的顶部C、按钮无法显示D、只有按钮Three显示出来并占据Frame整个区域

单选题下列关于程序运行结果的描述中,错误的是(  )。import javax.swing.*;import java.awt.*;public class Test{public static void main(String[]args){JFrame f = new JFrame();JPanel P=new JPanel();f.setLayout(new BorderLayout());f.getContentPane().add(p,"Center");P.setBackground(Color.blue);f.setVisible(true);f.setSize(200,200);}}A程序不能运行B显示一个窗口C窗口大小是200×200D窗口背景为蓝色

单选题现有:  package mypack;      import java.awt.Color;  import java.awt.FlowLayout;      import javax.swing.JButton;      import javax.swing.JFrame;      import javax.swing.JPanel;  public class ButtList extends JFrame  {      public static void main(String[] args)  {      ButtList bl  =new ButtList();     }  ButtList(){  JPanel pl=new JPanel();  pl.setBackground (Color.pink);      p1.add(new JButton("one"));      p1.add(new JButton("Two"));     p1.add(new JButton("Three"));      add("South",p1);  setLayout(new Fl.wLayout());      setSize(200,200);      setVisible(true);     }     }  上述代码运行后的显示效果是哪项?()A3个按钮从左到右排列在Frame的底部B3个按钮从左到右排列在Frame的顶部C按钮无法显示D只有按钮Three显示出来并占据Frame整个区域