~本特利~ |
1.在考生文件夹中丰有文件名为Java_3.java的文件。
该程序运行后的结果如下图所示:
该程序是不完整的,请在注释行“//*** Found *** ”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
Java_3.java如下:
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class Java_3
{
public static void main(String[] args)
{
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//*********Found********
class FontFrame _________ JFrame
{
public FontFrame()
{
setTitle("沁园春.雪");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
FontPanel panel = new FontPanel();
Container contentPane = getContentPane();
//*********Found********
contentPane.add(___________________);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
//*********Found********
class FontPanel extends ________
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
String message = "数风流人物,还看今朝!";
Font f = new Font("隶书", Font.BOLD, 24);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.setPaint(Color.RED);
//*********Found********
g2._________________(message, (int)x, (int)(baseY));
}
}
本题考查的是Swing构件和AWT中的事件处理。
1)类FontFrame的构造方法中有代码:“setTitle(“沁园春.雪”);“用于设置应用程序窗体的标题"沁园春.雪””,故FontFrame应该是JFrame的子类。因此第一空"class FontFrame__JFrame"应填"extends”。
2)类FontFrame的构造方法中有代码"FontPanel panel = new FontPanel();",因此第二空"contentPane. add( __ ) “应填"panel”。
3)类FontPanel的方法paintComponent(Graphics g)中的语句super. paintComponent(g) ;说明FontPanel覆盖了其父类的相应方法,而方法paintComponent(Graphics g)是抽象类javax.swing.JComponent中定义的,故FontPane1应是JComponent的子类。因此第三空"class FontPanelextends __“应填"JPanel”。
4) FontPanel的paintComponent方法中的message存储了程序中要显示的字符串,且Graphics的drawString方法用来"输出"字符串,故第四空"g2.____(message,(int)x,(int)(baseY));“应填"drawString”。
完整代码如下:
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class Demo {
public static void main(String[] args) {
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// *********Found********
class FontFrame extends JFrame {
public FontFrame() {
setTitle("沁园春.雪");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
FontPanel panel = new FontPanel();
Container contentPane = getContentPane();
// *********Found********
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
// *********Found********
class FontPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
String message = "数风流人物,还看今朝!";
Font f = new Font("隶书", Font.BOLD, 24);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.setPaint(Color.RED);
// *********Found********
g2.drawString(message, (int) x, (int) (baseY));
}
}