网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

setForeground()和()setBackground()定义在哪个类中

  • A、Graphics
  • B、Container
  • C、Component
  • D、Applet

参考答案

更多 “setForeground()和()setBackground()定义在哪个类中A、GraphicsB、ContainerC、ComponentD、Applet” 相关考题
考题 在Java语言中,下面是main()方法的部分代码:Framef=newFrame(MyFrame);f.setSize(100,100);为在屏幕显示f,应增加的代码是()。 A.f.appear();B.f.setForeground();C.f.setVisible();D.f.enable();

考题 下面是一个Applet程序,其功能是构造n阶魔方阵,魔方阵是这样一个方阵,它的每一行,每一列和对角线之和均相等,例如3阶魔方阵为8 1 63 5 74 9 2要求用Applet实现上述魔方阵。窗口中包括提示栏,提示输入0~15之间的奇数;输入框,输入方阵的阶数;按钮;点击则输出魔方阵;画布用于输出结果。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如下:import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.lang.Math.*;/*<applet code="ex10_3.class" width=800 height=400></applet>*/public class ex10_3 extends Applet implements ActionListener{Panel pane=new Panel();drawWnd dw;Label 11 = new Label("输入(0~15的奇数): ");TextField tf1 = new TextField(5);Button btn = new Button("OK");int[] [] a=new int[16] [16];int n=15;public void init(){pane.setLayout(new FlowLayout(FlowLayout.CENTER, 25,5));pane.add(11);pane.add(tf1);pane.add(btn);pane.addActionListener(this);add("North",pane);dw=new drawWnd();add("South",dw);}class drawWnd extends Canvas{drawWnd ( ){setSize(300,300);setBackground(Color.GRAY);}public void paint(Graphics g){g.setColor(Color. GREEN);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)g.drawString(Integer.toString(a[i] [j]),i*20,j*20);}}public void actionPerformed(ActionEvent ae)}try{n=Integer.parseInt (tf1.getText ());int i=1, j=1;for (i=1;i<=n;i++)for (j=1; j<=n; j++)a[i] [j]=0;j=n/2+1;a[i] [j]=1;for (int k=1; k<=n*n; k++){i--;j++;if((i<1)(j>n)){i=i+2;j--;}else{if (i<1)i=n;if (j>n)j=1;}if (a[i] [j]==0)a[i] [j]=k;else{i=i+2;j--;a[i] [j]=k;}}dw. repaint ( );}catch (NumberFormatException nfe)&n

考题 下列程序的作用是在屏幕上显示一个200×200大小的窗口,在横线上填入相应的语句。import java.awt.*;public class Test extends Frame. {public static void main (String args[]) {Test t=new Test ("Hello");t.setSize (200,200);t.setBackground (Color.re@D)@;【 】;}public Test (String str) {super(str);}}

考题 本题的功能是定义自已的组件类。窗口中排布着12个按钮,鼠标移动按钮时,按钮背景颜色改变,用鼠标单击 按钮时,后台将显示该按钮对应的字符。 import java.awt.*; import java.awt.event.*; import java.util.*; class java3 extends Frame{ String keys="l23456789*0"; java3{ super("java3"); addWindowListener(new WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(O); } }); setLayout(new GridLayout(4,3,6,6)); for(int i=0;ikeyslength;i++)){ KeyButton kb=new KeyButton(keys.charAt (i)); kb.addkeyListener(this); kb.setBackground(Color.pink); kb.setForeground(Color.black); add(kb); } setSize(200,200); show; } class KeyEventHandler extends KeyAdapter{ public void keyTyPed(KeyEvent evt){ System.out.println(evt.getChar); } } public static void main(String[]args){ newjava3; } } class KeyButton extends Component{ KeyListener keyListener; boolean highlighted; char key; KeyButton(char k){ this.key=k; addMouseListener(new MouseEventHandler): } public void paint(Graphics g){ int W=getSize.width; int h=getSize.height; String s=""+key; FontMetrics fm=g.getFontMetrics; if(highlighted){ g.setColor(getBackground); g.fillRoundRect(0,0,w-1,h-1,10,10); } g.setColor(getForeground); g.drawRoundRect(0,0,w-1,h-1,10,10); g.drawString(s,(w-fm.stringWidth(s))/2, (h-fm.getHeight)/2+fm.getAscent); } class MouseEventHandler extends MouseAdapter{ public void mousePressed(MouseEvent evt){ if(keyListener!=null){ keyListener.keyTyped( new KeyEvent(KeyButton.this,KeyEvent.KEY_ TYPED, System.currentTimeMillis, 0,KeyEvent.VK_UNDEFINED,key)); } } public void mouseEntered(MouseEvent evt){ highlighted=true; repaint; } public void mouseExited(MouseEvent evt){ highlighted=false; repaint; } } public synchronized void addKeyListerner(KeyLis- tener l){ keyListener=AWTEventMuhieaster.add(key- Listener,1); } public synchronized void removeKeyListener(Key- Listener l){ keyListener=AWTEventMuhicaster.remove (keyListener,1); } }

考题 关于下面的程序段,说法正确的是 import java.awt.*; import java.applet.*; public class Test extends Applet{ Canvas MyCanvas; public void init(){ MyCanvas=new Canvas(); MyCanvas.setBackground(Color.cyan); add(MyCanvas); } }A.程序可以运行,且看到显示B.程序编译出错C.程序可以运行,但看不到显示D.以上说法都不对

考题 下面的程序是完成一个容器的例子,所缺部分正确的选项是( )。 import java.awt.*; public class MyFrame. extends Frame{ public static void main(String args[]){ MyFrame. fr=new MyFrame("Hello Out There!"); fr.setSize(200,200); fr.setBackground(Color.red); ______; } public MyFrame(String str){ super(str);//调用父类的构造方法 } }A.fr.setVisible(false)B.fr.setVisibleC.fr.setVisible(true)D.以上都不是

考题 请完成下列Java程序:建立一个Applet程序,包括创建一个画布构件、一个面板构件,面板构件上包含3个按钮,用来设置画布和面板的背景颜色,这3个按钮(Red、Green、Blue)分别控制画布和面板背景色改变为3原色,即红、绿、蓝。要求画布宽度为300,高度为200。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:import java.awt.*;import java.applet.*;/*<applet code="ex10_2.class" width=800 height=400 ></applet>*/public class ex10_2 extends Applet{private Panel pane110_2;private Canvas canvas10_2;private Button btn1, btn2, btn3;public void init(){canvas10_2=new Canvas();canvas10_2. ______;canvas10_2.setBackground(Color.black);add(canvas10_2);pane110_2 = new Panel();pane110_2.setBackground( Color.black );btn1 = new Button( "Red" );btn2 = new Button( "Green" );btn3 = new Button( "Blue" );pane110_2.add( btn1 );pane110_2.add( btn2 );pane110_2.add( btn3 );______;}public boolean action( Event e, Object o ){if( e.target == btn1 ){pane110_2.setBackground( Color.red );canvas10_2.setBackground( Color.red );}else if( e.target == btn2 ){pane110_2.setBackground( Color.green );canvas10_2.setBackground(Color.green);}else if( e.target == btn3 ){canvas10_2.setBackground( Color.blue );pane110_2.setBackground( Color.blue );}return true;}}

考题 下面是一个Applet程序,其功能是根据公式:y=a*sin(x)绘制正弦曲线。要求窗口中有一个文本区作为曲线峰值a的输入,可以判断输入的数字是否符合要求,一个ok按钮,点击则在画布中绘制曲线,以画布中的横坐标值作为sin()的参数x。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.lang.Math.*;/*<applet code="ex18_3.class" width=800 height=400 ></applet>*/public class ex18_3 extends Applet implements ActionListener {Panel pane=new Panel();drawWnd dw;Label 11 = new Label("峰值");TextField tf1 = new TextField(15);Button btn = new Button("OK");private float a=0;public void init(){pane.setLayout(new FlowLayout(FlowLayout.CENTER,25,5));pane.add(11);pane.add(tf1);pane.add(btn);btn.addActionListener(this);add("North",pane);dw=new drawWnd();add("South",dw);}class drawWnd extends Canvas {drawWnd() {setSize(300,100);setBackground(Color. GRAY);}public void paint(Graphics g) {g.setColor(Color.GREEN);for(int i=0;i<getSize().width-1;i++){int y=0;int y1=y+(int) (Math.sin(i)*a);int y2=y1+(int) (a*Math.sin(i+1));g.drawLine(i,y1,i+1,y2);}}}public void actionPerformed(ActionEvent ae) {try{a=Integer.parseInt(tf1.getText());dw.repaint();}catch(NumberFormatException nfe){tf1.setText("error!");}}}ex18_3.html<HTML><HEAD><TITLE>ex18_3</TITLE></HEAD><BODY><applet code="ex18_3.class" width=800 height=400 ></applet></BODY></HTML>

考题 下面是一个Applet程序,其功能是绘制三维矩形以使该矩形面凹下,并将图像放入凹下的矩形面中。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如下:import java.awt.*;import java.applet.*;import java.awt.image.*;import java.util.*;import java.net.*;/*<applet code="ex01_3.class"width=800 height=400></applet>*/public class ex01_3 extends Applet{private Image IMG nClick=over(this) title=放大;public void init(){URL url=getCodeBase();IMG nClick=over(this) title=放大=getImage(url,"ex01_3.gif");MediaTracker mt=new MediaTracker();mt.addImage(IMG nClick=over(this) title=放大,1);try{mt.waitForAll();} catch(Exception e){e.printStackTrace();}setBackground(Color.black);}public void paint(Graphics g){int thick=3,x=10,y=10;g.drawImage(IMG nClick=over(this) title=放大,x,y);g.setColor(Color.lightGray);int w = IMG nClick=over(this) title=放大.getWidth(this);int h = IMG nClick=over(this) title=放大.getHeight(this);for(int i=0;i<thick;i++)g.draw3DRect(x,y,w+(a*i),true);}}ex01 3.html<HTML><HEAD><TITLE>ex01_3</TITLE></HEAD><BODY><applet code="ex01_3.class"width=800 height=400></applet></BODY></HTML>

考题 阅读以下说明和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;

考题 现有:packagemypack;importjava.awt.Color;importjava.awt.FlowLayout;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassButtListextendsJFrame{publicstaticvoidmain(String[]args){ButtListbl=newButtList();}ButtList(){JPanelpl=newJPanel();pl.setBackground(Color.pink);p1.add(newJButton(one));p1.add(newJButton(Two));p1.add(newJButton(Three));add(South,p1);setLayout(newFl.wLayout());setSize(200,200);setVisible(true);}}上述代码运行后的显示效果是哪项?()

考题 请完成下列Java程序。实例listener是监听器,frm是事件源,fr上发生的事件委托tat进行处理。程序的执行结果是显示一个蓝色的窗口,单击关闭按钮,可关闭窗口。注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:import java.awt.*;import java.awt.event.*;public class BlueWindow{public static void main(String args[]){Frame. frm=new Frame. ("欢迎参加Java考试!");TheAdapterTest listener=new TheAdapterTest();frm. ______;frm.setSize(200,200);frm.setBackground(Color.blue);frm. ______;}}class TheAdapterTest extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(1);}}

考题 请完成下面的程序:实现一个可以每秒跳动的时钟。运行如下图所示。请填写横线处的内容。注意:请勿改动main主方法和其他已有语句内容,仅在下划线处填入适当的语句。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Example2_12 extends JFrame (1) implements Runnable{Thread thread1;Color handColor;Color numberColor;JLabel jlabell = new JLabel();public Example2_12(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{getContentPane().add(jlabell, BorderLayout. SOUTH);}catch (Exception e){System.out.println(e.getMessage());}}public static void main(String args[]){Example2 12_clock1 = new Example2_12();clock1.init();clock1.start();clock1.setSize(260, 230);clock1.setResizable(false);clock1.show();}public void init(){int x, y;handColor = Color.blue;numberColor = Color.darkGray;setBackground(Color.white);}public void paint (Graphics g){int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xpoint, ypoint;String today;Calendar c1 = ______;s = c1.get(Calendar.SECOND);m = c1.get(Calendar.MINUTE);h = c1.get(Calendar.HOUR);int day, month, year, weekday;day = c1.get(Calendar.DATE);month = c1.get(Calendar.MONTH) + 1;year = c1. get (Calendar. YEAR);weekday = c1.get (Calendar. DAY_OF_WEEK);jlabell.setText("Today is "+ year + "/" + month + "/" + day + "/ "+ "Time:" + h + ":" + m + ":" + s);xpoint = 130;ypoint = 100;xs =(int) (Math.cos(s * 3.14f / 30 - 3.14f / 2) * 45 + xpoint);ys =(int) (Math.sin(s * 3.14f / 30 - 3.14f / 2) * 45 + ypoint);xm =(int) (Math.cos (m * 3.14f / 30 - 3.14f / 2) * 40 + xpoint);ym =(int) (Math.sin(m * 3.14f / 30 - 3.14f / 2) * 40 + ypoint);xh =(int) (Math.cos( (h * 30 + m / 2) * 3.14f / 180 - 3.14f / 2) * 30 + xpoint);yh =(int) (Math.sin( (h * 30 + m / 2) * 3.14f / 180 - 3.14f / 2) * 30 + ypoint);g.setColor (handColor);g.clearRect(0, 0, 260, 200);g.drawOval(xpoint / 2 + 10, ypoint / 2 - 5, 110, 110);g.setColor(numberColor);g.drawString("9", xpoint - 45, ypoint + 3);g.drawString("3", xpoint + 40, yp

考题 下面是一个Applet程序,其功能是用一组坐标点绘制一个多边形,并通过沿坐标的垂直方向移动,把它移到窗口的下半部分,然后填充它。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件清单如下:import java.awt.*;import java.applet.*;/*<applet code="ex11_3.class"width=800 height=400></applet>*/public class ex11_3 extends Applet{int[] x = {15,50,100,160,120,190 };int[] y = {15,100,30, 15, 80, 50 };public void init(){setBackground (Color. lightGray);}public void paint (Graphics g){int[] y2 = new int[6];g. setColor (Color. red);Rectangle rect = getBounds();g.drawPolygon (x, y2,6);for(int i=0; i<6; i++)y2[i] = y[i] + (rect.height / 2);g. fillPolygon (x, y, 6);}}ex11_3. html<HTML><HEAD><TITLE>ex11_3</TITLE></HEAD><BODY><applet code="ex11_3.class" width=800 height=400 ></applet></BODY></HTML>

考题 3下面的程序是完成一个容器的例子,所缺部分正确的选项是( )。 import java.awt.*; public class MyFrame. extends Frame{ public static void main(String args[]) { MyFrame. fr=new MyFrame("Hello Out There!"); fr.setSize(200,200); fr.setBackground(Color.red); _______; } public MyFrame(String str){ super(str); //调用你类的构造方法 } }A.fr. setVisible(false)B.fr. setVisibleC.fr. setVisible(true)D.以上都不是

考题 下面的Applet程序,其功能为画一个正方形,大小140*140,其背景色为蓝色(其填充色为pink色,各边离Applet的边为10像素)和一个在填充的正方形中自右上到左下来回移动的小球(半径15)。请改正程序中的错误(有下划线的语句),使程序执行后,能得到预期的结果。注意:不改动程序的结构,不得增行或删行。程序的执行结果为:import java.awt.*;import java.applet.*;/*<applet code=BallAnim width=800 height=600></applet>*///画一个正方形和一个移动的球,实现了Runnable接口。public class BallAnim extends Applet implements Runnable{Thread animThread;int ballX=100;//球的x坐标int bally;10;//球的Y坐标int ballDirection=0;//球移动的方向标志:0表示从上向下移动,1表示从下向上移动public void init(){super.setBackground(Color.blue);}public void start(){if (animThread !=null){animThread = new Thread(this);animThread.start();}}public void stop(){animThread.stop();animThread = null;}// 实现Runnable接public void run(){Thread.currentThread().setPriority(Thread. NORM_PRIORITY);//设置线呈优先级NORM_PRIORITYwhile (true){moveBall();try{Thread.sleep(100); // 休眠0.1秒}catch (Exception sleepProblem){// This applet ignores any exceptions if it has a problem sleeping.// Maybe it should take Sominex}}}private void moveBall (){// 球对角运动if (ballDirection == 0){ballX-=2; / / 如果球从左向右运动,球的X坐标减少2ballY+=2; / / 如果球从上向下运动,球的Y坐标增加2if (ballY > 100)//球的Y坐标增加到100, 改变球的运动方向{ballDirection = 1;ballX = 10;ballY=100;}}else{ballX+=2; //如果球从右向左运动,球的X坐标增加2ballY-=2;//如果球从下向上运动,球的Y坐标减少2if (bally <= 10)//球的X坐标减少到10,改变球的运动方向{ballDirection = 0;ballX = 100;ballY=10;}}paint();}public void paint(Graphics g){g.setColor(Color.pink);g.fillRect(10, 10, 120, 120);g.setColor(Color.green);g.fillOval(ballX,ballY, 30, 30);}}ex40_3, html:<html><head><title> ex40_3</title></head><body><applet code=" BallAnim.class" width=800 height=400></applet> </body> </html>

考题 本题中使用了选项卡,窗口中有一个选项卡,总共有“系统”、“声卡”、“显卡”、“网卡”和“帮助”5项选项面。单击各个选项的文字标签后,所选中的选项将为当前选项。“系统”选项上有3个复选按钮,分别控制“声卡”、“显卡”和“网卡”三个选项,选中某个按钮后,它所指示的选项就可用,否则不可用。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class java3 extends JFrame{ JTabbedPane config=new JTabbedPane: public java3{ super("java3"); setSize(500,300); setDefaultCloseoperation(EXIT_ON_CLOSE); JPanel configPane=new JPanel; configPane.setLayout(new BoxLayout(config- Pane,BoxLayout.Y_AXIS)); JTextArea question=new JTextArea("下面的哪 个选项\n"+"你想设置?"); question.setEditable(false); question.setMaximumSize(new Dimension(300, 50)); question.setAlignmentX(0.Of); question.setBackground(configPane.getBack- ground); JCheckBox audioCB=new JCheckBox ("声卡", true); JCheckBox nicCB=new JCheckBox("网卡", true); JCheckBox tvCB=new JCheckBox("显示卡", false); configPane.add(Box.createVerticalGlue); configPane.add(question); configPane.add(audioCB); configPane.add(nicCB); configPane.add(tvCB); configPane.add(Box.ereateVerticalGlue); JLabel audioPane=new JLabel("声卡页面"); JLabel niePane=new JLabel("网卡页面"); JLabel tvPane=new JLabel("显示卡页面"); JLabel helpPane=new JLabel("帮助信息"); audioCB.addItemListener(new TabManager(au- dioPane)); nicCB.addItemListener(new TabManager (nicPane)); tvCB.addItemListener(new TabManager (tvPane)): config.addTab("系统",null,configPane," Choose Installed Options”); config.addTab("声卡",null,audioPane,"Audio system configuration”); config.addTab("网卡",null,nicPane;"Netwot- king configuration"); config.addTab("显示卡",null,tvPane,"Video system configuration"); config.addTab("帮助",null,helpPane,"How Do I…"); getContentPane.add(config,BorderLayout. CENTER); } class TabManger implements ActionListener{ Component tab; public TabManager(Component tabToManage){ tab=tabToManage; } public void ItemStateChanged(ItemEvent ie){ int index=config.indexOfComponent(tab); if(index!=-l){ config.setEnabledAt(index,ie.getStateChange = =ItemEvent.SELECTED); } this.repaint; } } public static void main(String args[]){ java3 SC=new java3; se.setVisible(true); } }

考题 请完成下列Java程序:制作一个图形用户界面,上方包含一个TextField和Button构件,实现输入字符串,点击Button获取文本区的字符:中间显示Label的内容:下方是4个按钮,分别实现控制Label在最左边,在中间,在右边和退出程序的功能。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;public class ex15_2 extends Frame. implements ActionListener {private Label 1;private TextField tf;public static void main(String[] args) {ex15_2 obj15_2 = new ex15_2();}public ex15_2() {setBackground(Color.gray);l = new Label("Welcom to the NCR Examination!");Font font = new Font("TimesRoman",Font.BOLD,20);l.setFont(font);add("Center",l);Panel p = new Panel();Button b = new Button("Left");b.addActionListener(this);p.add(b);b = new Button("Center");b.addActionListener(this);p.add(b);b = new Button("Right");b.addActionListener(this);p.add(b);________________;b = new Button("Exit");b.addActionListener(this);p.add(b);p = new Panel();tf = new TextField(40);p.add(tf);b = new Button("Set");b.addActionListener(this);p.add(b);add("North",p);setSize(500,300);show();}public void actionPerformed(ActionEvent ae) {if(ae.getActionCommand().equals("Exit"))System.exit(0);else if(ae.getActionCommand().equals("Left"))____________________;else if(ae.getActionCommand().equals("Center")l.setAlignment(Label.CENTER);else if(ae.getActionCommand().equals("Right"))l.setAlignment(Label.RIGHT);else if(ae.getActionCommand().equals("Set"))l.setText(tf.getText());}}

考题 请完成下列Java程序:用awt实现一个图形用户界面,包含一个List构件和一个TextField构件,List中包含5个项目。要求选择不用的项目时,能在TextField构件中显示出相关的信息,包括事件的名称,选择的项目编号和项目的状态。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;public class ex14_2 extends Frame. implements ItemListener {private TextField tf;private List 1;public static void main(String[] arg) {ex14_2 obj14_2 = new ex14_2();}public ex14_2() {setBackground(Color. GRAY);setTitle("ex14_2");l = new List(5);l.addItemListener(this);l.addItem("Item1");l.addItem("Item2");l.addItem("Item3");l.addItem("Item4");l.addItem("Item5");add("Center",l);tf = new TextField(50);add("South",tf);______________;show ( );}public void itemStateChanged(ItemEvent event)tf.setText( );}}

考题 本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。import javax.swing.*;import javax.swing.event.*;import java.awt.*;public class java3 extends JFrame{public static JTextPane textPane;public static JScrollPane scrollPane;JPanel panel;public java3(){super("java3()");panel=new JPanel();panel.setLayout(new BorderLayout());panel.setBorder(BorderFactory.createEmptyBor-der(20,20,20,20));textPane=new JTextPane();textPane.setFont(new Font("monospaeed",Font.PLAIN,12));scrollPane=new JScrollPane(textPane);panel.add(scrollPane);scrollPane.setPreferredsize(new Dimension(300,250));setContentPane(panel);setCloseOperation(JFrame.EXIT_ON_CLOSE);LineNumber lineNumber=new LineNumber();scrollPane.setRowHeaderView(lineNumber);}public static void main(String[]args){java3 ttp=new java3();ttp.pack();ttp.setVisible(true);}}class LineNumber extends JTextPane{private final static Color DEFAULT_BACK-GROUND=Color.gray;private final static Color DEFAULT_FORE-GROUND=Color.black;private final static Font DEFAUl。T—FONT=newFont("monospaced",Font.PLAIN,12);private final static int HEIGHT=Integer.MAX_VALUE-1000000;private final static int MARGIN=5;private FontMetrics fontMetrics;private int lineHeight;private int currentRowWidth;private JComponent component;private int componentFontHeight;private int componentFontAscent;public LineNumber(JComponent component){if(component= =null){setBackground(DEFAULT_BACKGROUND);setForegroun"DEFAULT_FOREGROUND);setFont(DEFAULT FONT);this.component=this;}else{setBaekground(DEFAULT_BACKGROUND);setForeground(component.getForeground());setFont(component.getFont());this.component=component;}componentFontHeight=component.getFontMet-rics(component.getFont()).getHeight();componentFontAscent=component.getFontMet-ries(component.getFont()).getAscent();setPreferredWidth(9999);}public void setPreferredWidth(int row){int width=fontMetrics.stringWidth(String.val-ueOf(row));if(currentRowWidth<width){currentRowWidth=width;setPreferredSize(new Dfimension(2 * MARGIN+width,HEIGHT));}}public void setFont(Font font){super.setFont(font);fontMetrics=getFontMetrics(getFont());}public int getLineHeight(){if(hneHeight= =0)return componentFontHeight;elsereturn lineHeight;}public void setLineHeight(int lineHeight){if(hneHeight>0)this.lineHeight=lineHeight;}public int getStartOffset(){return component.getlnsets().top+component-FontAscent;}public void paintComponent(Graphics g){int lineHeight=getLineHeight();int startOffset=getStartOffset();Rectangle drawHere=g.getClipBounds();g.setColor(getBackground());g.fillRect(drawHere.x,drawHere.Y,drawHere.width,drawHere.height);g.setColor(getForeground());int startLineNumber=(drawHere.y/line-Height)+1;int endLineNUmber = startLineNumber+(drawHere.height/lineHeight);int start=(drawHere.Y/hneHeight)*line-Height+startOffset;for(int i=startLineNumber;i<=endLineN-umber;i++){String lineNumber=String.valueOf(i);int width=fontMetrics.stringWidth(lineNumber);g.drawstring(lineNumber,MARGIN+current-RowWidth-width,start);start+=lineHeight:}setPreferredWidth(endLineNumber);}}

考题 本题是一个Applet,它的功能是在窗口上添加12×12个标签,并且横向和纵向标签的颜色为黑白相间。import java.applet.*;import java..awt.*;import java,.awt.event*;pubhc class java2extends Applet{GridLayout grid;pubhc void init(){grid=new GridLayout(12,12);setLayout(grid);Label =new Label[12][12];for(int i=0;i<12;i++){for(int j=0;j<12;j++){label[i][j]= ;if((i+j)%2= =0)label[i][j].setBackground(Color.black);elselabel[i][j].setBackground(color.white);add(label[i][j]);}}}}

考题 本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮:“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable changes”复选项,则三个颜色按钮变为不可用,如果取消选中复选项,则三个按钮变为可用。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class java3 extends JFrame{private JPanel upper,middle,lower;private JTextField text;private JButton black,red,green;private JCheckBox disable;public java3(String titleText){super(titleText);addWindowListener(new WindowAdapter(){public voidwindowClosing(WindowEvent e){System.exit(0);}});upper=new JPanel();upper.setBorder(BorderFactory.ereateTitledBor-der("Sample text"));upper.setlayout(new BorderLayout());text=new JTextField("Change the color of thistext"):upper.add(text,BorderLayout.CENTER);middle=new JPanel();middle.setBorder(BorderFactory.createTitledBor-der("Text color control"));middle.setLayout(new FlowLayout(FlowLayout.CENTER)):black=new JButton("Black",new ColorIcon(Color.black));black.addActionListener( new ButtonListener(Color.black));middle.add(black);red=new JButton("Red",new ColorIcon(Col-or.red));red.addActionListener(new ButtonListener(Col-or.red));middle.add(red);green=new JButton("Green",new ColorIcon(Color.green));green.addActionListener(new ButtonListener(Color.green));middle.add(green);lower=new JPanel();lower.setLayout(new FlowLayout(FlowLayout.RIGHT));disable=new JCheckBox("Disable changes"):disable.addItemListener(new ItemListener()(public void itemStateChanged(ItemEvent e){boolean enabled=(e.getStateChange()= =ItemEvent.DESELECTED):black.setEnabled(enabled);red.setEnabled(enabled);green.setEnabled(enabled);}});lower.add(disable);Container cp=getContentPane();cp.add(upper,BorderLayout.NORTH);cp.add(middle,BorderLayout.CENTER);cp.add(10wer,BorderLayout.SoUTH);pack();setVisible(true);}class ButtonListener extends ActionListener{private Color c;public ButtonListener(Color c){this.c=c;}public void actionPerformed(ActionEvent e){text.setForeground(c);}}class ColorIcon implements Icon{private Color c;private static final int DIAMETER=10;public ColorIcon(Color c){c=c;}public void paintlcon(Component cp,Graphics g,int x,int y){g.setColor(c);g.fillOval(X,y,DIAMETER,DIAMETER);g.setColor(Color.black);g.drawOval(x,y,DIAMETER,DIAMETER);}public int getlconHeight(){return DIAMETER;}public int getlconWidth(){return DIAMETER;}}public static void main(String[]args){new java3("advance");}}

考题 ●试题七【说明】下面是一个Applet程序,其功能是通过一个按钮控制一个窗口的创建,显示与隐藏,并且以按钮文字作为提示,可以随着窗口的状态改变,即如果窗口出现,则按钮文字为"Hide myFrm",提示用户点击按钮,则隐藏窗口,反之亦然。请将横线处语句补充完整。程序运行结果如图5所示:图5import javA.awt.*;import javA.applet.*;/*applet code="ex8_7.class" width=800 height=400 /applet*/public class ex8_7 extends Applet{private Frame. frm;private Button showBtn;public void init(){showBtn = new Button( "Show Frame" );(1) ;}public boolean action( Event e, Object o ){if( e.target == showBtn ){if( (2) ){(3) ;frm.dispose();(4) ;showBtn.setLabel("Show myFrm");}else{frm = new Frame( "myFrm" );frm.resize( 200, 150 );frm.setBackground( Color.gray );(5) ;showBtn.setLabel("Hide myFrm");}}return true;}}ex8_7.htmlHTMLHEADTITLEex8_7/TITLE/HEADBODYapplet code="ex8_7.class" width=800 height=400 /applet/BODY/HTML

考题 JFrame中的哪个方法用来将它显示在屏幕上()。A、setResizableB、setVisibleC、isVisibleD、setForeground

考题 单选题下列关于程序运行结果的描述中,错误的是(  )。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 窗口背景为蓝色

考题 单选题在Java语言中,下面是main()方法的部分代码:    Frame f = new Frame("My Frame");   f.setSize(100,100);    为在屏幕显示f,应增加的代码是()。A f.appear();B f.setForeground();C f.setVisible();D f.enable();

考题 单选题现有:  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整个区域

考题 单选题setForeground()和()setBackground()定义在哪个类中A GraphicsB ContainerC ComponentD Applet