The buttons labeled “North” and “South” will have the same width.
The buttons labeled “North” and “South” will have the same height.
The height of the button labeled “North” can very if the Frame is resized.
The height of the button labeled “South” can very if the Frame is resized.
The width of the button labeled “North” is constant even if the Frame is resized.
The width of the button labeled “South” is constant even if the Frame is resized.
第1题:
下列程序使用CardLayout管理了2张卡片,每张都是一个Panel,每个Panel有一个Button,单击按钮,显示下一张卡片中的内容。请将程序补充完整。
注意:不改动程序结构,不得增行或删行。
import java.awt.*;
import java.awt.event.*;
public class ex3 implements______
{
private Panel p1,p2;
private Button btn1,btn2;
private Frame. frm;
private CardLayout cl;
public static void main(String[] args)
{
ex3 tt=new ex3();
tt.method();
}
public void method()
{
Frm=new Frame("CardLayout");
Cl=new CardLayout();
btn1=new Button("Card1");
btn2=new Button("Card2");
pl=new Panel();
p2=new Panel();
p1.add(btn1);
btn1.addActionListener(this);
p2.add(btn2);
______
frm.SetLayout(cl);
frm.add(pl,"Layer1");
frm.add(pl,"Layer1");
frm.SetSize{200,200);
frm.SetVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
______
}
}
第2题:
下列程序在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());
第3题:
选择正确的语句填在下列程序的横线处。 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());
第4题:
本题的功能是获取鼠标在窗口中的位置。当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比如显示为"鼠标的当前位置: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();
}
}
}
第5题:
以下程序调试结果为:
public class Test {
int m=5;
public void some(int x) {
m=x;
}
public static void main(String args []) {
new Demo().some(7);
}
}
class Demo extends Test {
int m=8;
public void some(int x) {
super.some(x);
System.out.println(m);
}
}
A.5
B.8
C.7
D.无任何输出
E.编译错误
第6题:
现有: class Top { static int X=l; public Top() { x*=3; } } class Middle extends Top { public Middle() {x+=l; } public static void main(String [] args) { Middle m=new Middle(); System.out.println (x); } } 结果是什么?()
第7题:
public class A extends Thread { A() { setDaemon(true); } public void run() { (new B()).start(); try { Thread.sleep(60000); } catch (InterruptedException x) {} System.out.println(“A done”); } class B extends Thread { public void run() { try { Thread.sleep(60000); } catch (InterruptedException x) {} System.out.println(“B done”); } } public static void main(String[] args) { (new A()).start(); } } What is the result?()
第8题:
class Top { static int x = 1; public Top(int y) { x *= 3; } } class Middle extends Top { public Middle() { x += 1; } public static void main(String [] args) { Middle m = new Middle(); System.out.println(x); } } 结果为:()
第9题:
public class Starter extends Thread { private int x= 2; public static void main(String[] args) throws Exception { new Starter().makeItSo(); } public Starter() { x=5; start(); } public void makeItSo() throws Exception { join(); x=x- 1; System.out.println(x); } public void run() { x *= 2; } } What is the output if the main() method is rum?()
第10题:
i = m;
i = b;
i = p.a;
i = p.change(30);
i = t.b.
第11题:
win.setLayout(new BorderLayout()); win.add(but);
win.setLayout(new GridLayout(1, 1)); win.add(but);
win.setLayout(new BorderLayout()); win.add(but, BorderLayout.CENTER);
win.add(but);
win.setLayout(new FlowLayout()); win.add(but);
第12题:
x = y;
z = x;
y = (B) x;
z = (C) y;
y = (A) y;
第13题:
请完善程序(程序文件名:Java_2.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。
[题目要求]
在JFrame窗口中,显示一个字符串并保证窗口正常关闭和退出,使程序运行结果如下图所示。
源程序:
import javax.swing.*:
import java.awt.*;
public class Java_2 {
public static void main(String[] args) {
WelcomFrame. frame=new WelcomFrame();
frame.setDefaultCloseOperation(JFrame. (1) );
frame.show();
}
}
class WelcomFrame. extends (2) {
public WelcomFrame() {
setTitle("Java等级考试");
(3) (DEFAULT_WIDTH,DEFAULT_HEIGHT);
WelcomPanel panel=new WelcomPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=250;
public static final int DEFAULT_HEIGHT=100;
}
class WelcomPanel extends (4) {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("欢迎参加Java等级考试!",MESSAGE_X,MESSAGE_Y);
}
public static final int MESSAGE_X=60:
public static final int MESSAGE_Y=50:
}
第14题:
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的
x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。
对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可
以通过A.x 来明确。
第15题:
请完善程序(程序文件名: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;
}
第16题:
本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。
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();
}
}
第17题:
Which are syntactically valid statement at// point x?() class Person { private int a; public int change(int m){ return m; } } public class Teacher extends Person { public int b; public static void main(String arg[]){ Person p = new Person(); Teacher t = new Teacher(); int i; // point x } }
第18题:
Which statements, when inserted at the indicated position in the following code, will cause a runtime exception when attempting to run the program?() class A {} class B extends A {} class C extends A {} public class Q3ae4 { public static void main(String args[]) { A x = new A(); B y = new B(); C z = new C(); // insert statement here } }
第19题:
Given the following code, which code fragments, when inserted at the indicated location, will succeed in making the program display a button spanning the whole window area?() import java.awt.*; public class Q1e65 { public static void main(String args[]) { Window win = new Frame(); Button but = new Button("button"); // insert code fragment here win.setSize(200, 200); win.setVisible(true); } }
第20题:
import java.awt.*; public class X extends Frame { public static void main (String args) { X x = new X(); x.pack(); x.setVisible(true); } public X() { setLayout (new BordrLayout()); Panel p = new Panel (); add(p, BorderLayout.NORTH); Button b = new Button (“North”); p.add(b): Button b = new Button (“South”); add(b1, BorderLayout.SOUTH): } } Which two statements are true?()
第21题:
import java.awt*; public class X extends Frame ( public static void main(string args) ( X x = new X (); X.pack(); x.setVisible(true); ) public X () ( setlayout (new GridLayout (2,2)); Panel p1 = new panel(); Add(p1); Button b1= new Button (“One”); P1.add(b1); Panel p2 = new panel(); Add(p2); Button b2= new Button (“Two”); P2.add(b2); Button b3= new Button (“Three”); add(b3); Button b4= new Button (“Four”); add(b4); ) ) Which two statements are true? ()
第22题:
The buttons labeled “North” and “South” will have the same width.
The buttons labeled “North” and “South” will have the same height.
The height of the button labeled “North” can very if the Frame is resized.
The height of the button labeled “South” can very if the Frame is resized.
The width of the button labeled “North” is constant even if the Frame is resized.
The width of the button labeled “South” is constant even if the Frame is resized.
第23题:
All the buttons change height if the frame height is resized.
All the buttons change width if the Frame width is resized.
The size of the button labeled “One” is constant even if the Frame is resized.
Both width and height of the button labeled “Three” might change if the Frame is resized.
第24题:
2
3
4
编译失败