Java二级操作题第24套
基本操作
在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
程序的功能是在给定球的半径时计算出球的体积。当程序运行时,在命令屏幕上给出程序功能提示,并显示输入半径的窗口: 
根据考生输入球半径的值计算体积,并在命令屏幕上显示结果。
//*********Found**********
import __________________.swing.*;
public class Java_1
{
//*********Found**********
public _______________ void main(String[] args)
{
System.out.println();
System.out.println("这是一个指定球半径,求球体积的程序。");
String input=JOptionPane.showInputDialog("请输入球半径。");
//*********Found**********
double r=Double.parseDouble(________________);
System.out.println("当球的半径是" + r + "时,该球的体积是 " + (Math.PI*r*r*r*4/3));
System.exit(0);
}
}
本题考查的是JavaSwing。
Swing包位于Javax包下,所以第一空填写"javax"。
主函数必须用static修饰,所以第二空填写"static"。
Input接收的是文本框中输入的值,所以半径r接收的应为input,所以第三空填写"input"。
具体程序如下:
//*********Found**********
import javax.swing.*;
public class Java_1
{
//*********Found**********
public static void main(String[] args)
{
System.out.println();
System.out.println("这是一个指定球半径,求球体积的程序。");
String input=JOptionPane.showInputDialog("请输入球半径。");
//*********Found**********
double r=Double.parseDouble(input);
System.out.println("当球的半径是" + r + "时,该球的体积是 " + (Math.PI*r*r*r*4/3));
System.exit(0);
}
}
简单应用
在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
程序的功能是:定义了一个简单的线程,可以对其名称进行设置。该程序运行时的输出结果如下:
Running the Testing_Thread:
___0___Testing_Thread
___1___Testing_Thread
___2___Testing_Thread
___3___Testing_Thread
___4___Testing_Thread
public class Java_2
{
public static void main(String[] args)
{
Thread t = new SimpleThread("Testing_Thread");
//*********Found**********
______________ ;
}
}
//*********Found**********
class SimpleThread extends _________
{
public SimpleThread(String str)
{
//*********Found**********
_____________ ;
}
//*********Found**********
public void _________()
{
System.out.println("Running the " + getName() + ":");
for (int i = 0; i < 5; i++)
{
System.out.println("---" + i + "---" + getName());
try
{
sleep((int)(2 * 100));
}
//*********Found**********
_________(InterruptedException e) { }
}
}
}
本题考查的是线程。
线程的启动方法是start,所以第一空填写"t.start()"。
创建线程时,应该继承Thread类或者实现Runnable接口,本题中,该类已知的关键字为extends,所以第二空填写"Thread"。
当需要给线程命名时,只需要在本类中实现父类String参数的构造器,所以第三空填写"super(str)"。
用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。而直接执行run方法却会出现等待。
本类中,主函数调用start方法,具体实现则在run中,所以第四空填写"run"。
异常的捕捉有很多种,这里给出了已知的try,所以第五空填写"catch"。
具体程序如下:
public class Java_2
{
public static void main(String[] args)
{
Thread t = new SimpleThread("Testing_Thread");
//*********Found**********
t.start();
}
}
//*********Found**********
class SimpleThread extends Thread
{
public SimpleThread(String str)
{
//*********Found**********
super(str);
}
//*********Found**********
public void run()
{
System.out.println("Running the " + getName() + ":");
for (int i = 0; i < 5; i++)
{
System.out.println("---" + i + "---" + getName());
try
{
sleep((int)(2 * 100));
}
//*********Found**********
catch(InterruptedException e) { }
}
}
}
综合应用
在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
在JFrame窗口中,显示出红色的2008北京奥运主题口号“同一个世界,同一个梦想”,同时在窗口的上方,有三个按钮可以改变窗口的背景色。
该程序的运行结果如下: 
import java.awt.event.*;
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 extends ________________
{
public FontFrame()
{
setTitle("北京 2008");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
FontPanel panel = new FontPanel();
Container contentPane = getContentPane();
//*********Found**********
contentPane.add(_____________________);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
}
class FontPanel extends JPanel
{
public FontPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Green");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction greenAction = new ColorAction(Color.GREEN);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(greenAction);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
String message = "同一个世界,同一个梦想!";
Font f = new Font("隶书", Font.BOLD, 27);
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);
g2.drawString (message, (int)x, (int)(baseY));
}
//*********Found**********
private class ColorAction ____________________ ActionListener
{
public ColorAction(Color c)
{
BackgroundColor = c;
}
//*********Found**********
public void _____________________ (ActionEvent event)
{
setBackground(BackgroundColor);
}
private Color BackgroundColor;
}
}
本题考查的是JavaSwing。
一个窗口的最基本的框架为JFrame,JFrame中可以嵌套很多其他的诸如Panel的对象,所以,FontFrame类应继承JFrame类,第一空填写"JFrame"。
JFrame有一个ContentPane,窗口能显示的所有组件都是添加在这个ContentPane中,所以第二空填写"panel"。
ActionListener是事件监听接口,所以第三空填写"implements"。
actionPerformed是接口ActionListener里面定义的一个抽象方法,所有实现这个接口的类都要重写这个方法。所以第四空填写"actionPerformed"。
具体程序如下:
import java.awt.event.*;
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 extends JFrame
{
public FontFrame()
{
setTitle("北京 2008");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
FontPanel panel = new FontPanel();
Container contentPane = getContentPane();
//*********Found**********
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
}
class FontPanel extends JPanel
{
public FontPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Green");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction greenAction = new ColorAction(Color.GREEN);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(greenAction);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
String message = "同一个世界,同一个梦想!";
Font f = new Font("隶书", Font.BOLD, 27);
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);
g2.drawString (message, (int)x, (int)(baseY));
}
//*********Found**********
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
BackgroundColor = c;
}
//*********Found**********
public void actionPerformed (ActionEvent event)
{
setBackground(BackgroundColor);
}
private Color BackgroundColor;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!