文档库 最新最全的文档下载
当前位置:文档库 › java图形界面设计含答案

java图形界面设计含答案

十一章 图形用户界面程序设计入门
一.选择题:
1. 容器Panel和applet缺省使用的布局编辑策略是 B
A、BorderLayout B、FlowLayout
C、GridLayout D、CarLayout
2. .applet类的直接父类是: B
A、Component类 B、Container类
C、Frame类 D、Panel类
3. .Swing与AWT的区别不包括 D
A、Swing是由纯Java实现的轻量级构件
B、Swing没有本地代码
C、Swing不依赖操作系统的支持
D、Swing支持图形用户界面
4. 在编写Java applet程序时,若需要对发生事件作出响应和处理,一般需要在程序的
开头写上 B _语句。
A、import java.awt.*; B、import java.applet.*;
C、import java.io.*; D、import java.awt.event.*;
5. 下面哪个操作是applet可以做的? B
A、读取客户端部分系统变量 B、读取客户端文件
C、在客户端主机上创建新文件 D、在客户端装载程序库
6. 在Java中,所有类的根类是 A
A.https://www.wendangku.net/doc/3b2358554.html,ng.Object B.https://www.wendangku.net/doc/3b2358554.html,ng.Class
C.java.applet.Applet D.java.awt.Frame
7.在JAVA中实现图形用户界面可以使用组建AWT和组件 A
A、swing B、Swing
C、JOptionPane D、import
8.在JAVA中,一般菜单格式包含有下列类对象 D
A、JMenuBar B、JMenu
C、JMenuItem D、JMenuBar、JMenu、JMenuItem
9.JAVA中提供了多种布局对象类。其中使用卡片式布局的是 D
A、FlowLayout B、BoederLayout
C、BoxLayout D、CardLayout
10. 下列关于Applet的叙述中,错误的是 A
A、Applet是Java类,所以可以由JDK中的解释器java.exe直接解释运行
B、Applet应该定义为java.applet.Applet类或javax.swing.JApplet类的子类
C、Applet与Application的主要区别在执行方式上
D、通过在HTML文件中采用标记可以向Applet传递参数
11 与Applet生命周期相关的方法的数量是 A
A、4种 B、3种 C、2种 D、5种
12下列关于Applet的叙述中,错误的是 A
A、Applet是Java类,所以可以由JDK中的解释器java.exe直接解释运行
B、Applet应该定义为java.applet.Applet类或javax.swing.JApplet类的子类
C、Applet与Application的主要区别在执行方式上
D、通过在HTML文件中采用标记可以向Applet传递参数
13 java中对Applet设置了严格的安全限制。下列关于Applet在Java2中安全限制叙述正确的是 A
A、根本无法解除 B、只有部分限制可以解除,而其它限制无法解除
C、可以在安全策略的控制下解除 D、已经缺省地全部解除
14.下面关于Applet的说法正确的是 B
A、Applet也需要main方法 B

、Applet继承自java.awt.Applet或javax.swing.JApplet
C、Applet能访问本地文件 D、Applet程序不需要编译
15.在 Java 中,所有类的根类是 A
A、https://www.wendangku.net/doc/3b2358554.html,ng.Objet B、https://www.wendangku.net/doc/3b2358554.html,ng.Class
C、java.applet.Applet D、java.awt.Frame
16.类 Panel 默认的布局管理器是 C
A、GridLayout B、BorderLayout
C、FlowLayout D、GardLayout
17.下列关于 Java Application 与 Applet 的说法中,正确的是 C
A、都包含 main() 方法 B、都通过“appletviewer”命令执行
C、都通过“javac”命令编译 D、都嵌入在 HTML 文件中执行
18.Frame默认的布局管理器是 B
A、FlowLayout B、BorderLayout
C、GridLayout D、CardLayou
19.下列关于Applet的叙述中,错误的是 A
A、Applet是Java类,所以可以由JDK中的解释器java.exe直接解释运行
B、Applet应该定义为java.applet.Applet类或javax.swing.Japplet类的子类
C、Applet与Applicationg的主要区别在执行方式上
D、通过在HTML文件中采用标记可以向Applet传递参数
20.以下哪个是java.io包中的一个兼有输入输出功能的类 C
A、Object B、Serializable
C、RandomaccessFile D、java.io中不存在这样的类
21.一个Java application 运行后,在系统中是作为一个 B
A、线程 B、进程
C、进程或线程 D、不可预知
22.在Java图形用户界面编程中,若显示一些不需要修改的文本信息,一般是使用
A 类的 对象来实现。
A、Label B、Button
C、Textarea D、TestField
23.下面关于applet生命周期的说法正确的是 D
A、applet生命周期是从浏览器解析HTML文件开始的
B、浏览器加载结束时,终止applet的运行
C、applet生命周期包括:applet的创建、运行、等待与消亡四个状态
D、以上说法均不正确
24.下列不属于Swing中构件的是 C
A.JPanel B.JTable
C.Menu D.JFrame
25. 在Java中,由Java编译器自动导入,而无需在程序中用import导入的包是 D
A.java.applet B.java.awt
C.java.util D.https://www.wendangku.net/doc/3b2358554.html,ng
26.容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变? B
A CardLayout B FlowLayout
C BorderLayout D GridLayout
27.下列哪个属于容器的构件?_A___
A JFrame B JButton C JPnel D JApplet
28下列关于frame类的说法不正确的是 _D___
A frame是window类的直接子类
B frame对象显示的效果是一个窗口
C frame被默认初始化为可见的
D frame的默认布局管理器为borderlayout
29 下面这些方法中 方法不是Applet的基本方法_B___
A. init() B. run() C. stop() D. start()
30 作为Java

应用程序图形界面基础的是_B___
A、Panel B、Frame C、Windows D、Applet
31 类Panel默认的布局管理器是 _C__
A.GridLayout
B.BorderLayout
C.FlowLayout
D.CardLayout
32.下面哪个操作是Applet可以做的? __D__
A.读取客户端部分系统变量 B.读取客户端文件
C.在客户端主机上创建文件 D.在客户端装载程序库
33.APPLET标记中的__B__属性给出了APPLET实例的名称,以便同一主页中
的APPLET能够实现对方并互相通信.
A.code https://www.wendangku.net/doc/3b2358554.html, C.alt D.param
34. APPLET运行时,被浏览器和appletviewer调用的第一个方法是_ ___B_____
A.paint() B.init() C.start() D.destroy()
35. 下列不属于Swing中构件的是__C__
A.JPanel
B.JTable
C.Menu
D.JFrame
36.下列选项中,不是APPLET标记的是___B___
A.PARAM
B.BODY
C.CODEBASE
D.ALT
37.在Applet中显示文字、图形等信息时,应使用的方法是_A___
A.paint() B.init()
C.start() D.destroy()
38下列关于Applet的说法中,错误的是_C__
A.Applet自身不能运行,必须嵌入到其它应用程序(如浏览器)中运行
B.可以在安全策略的控制下读写本地磁盘文件
C.Java中不支持向Applet传递参数
D.Applet的主类要定义为java.applet.Applet类的子类
39 Applet可以做下列那些操作? __A___
A)读取客户端文件 B)在客户端主机上创建新文件
C)在客户端装载程序库 D)读取客户端部分系统变量
40如果希望所有的控件在界面上均匀排列,应使
用下列那种布局管理器? _B___
A)BoxLayout B)GridLayout C)BorderLayout D)FlowLouLayout
41. 编译Java Applet 源程序文件产生的字节码文件的扩展名为_B__。
A、java B、class C、html D、exe
42. 下面 方法与applet的显示无关的是_B___
A、update() B、draw() C、repaint() D、paint()
43.Swing构件的程序不具有 __A__ 。
A、本地代码 B、标签 C、菜单 D、容器
43.下列操作中,不属于Applet安全限制的是___D___。
A、加载本 B、读写本地文件系统 C、运行本地可执行程序 D、与同一个页面中的Applet通信
44. 下列关于Applet的叙述中,错误的是 _A___ 。
A、Applet是Java类,所以可以由JDK中的解释器java.exe直接解释运行
B、Applet应该定义为java.applet.Applet类或javax.swing.JApplet类的子类
C、Applet与Application的主要区别在执行方式上
D、通过在HTML文件中采用标记可以向Applet传递参数
45 .java中对Applet设置了严格的安全限制。下列关于Applet在Java2中安全限制叙述正确的是 _C__ 。
A、根本无法解除 B、只有部分限制可以解除,而其它限制无法解除 C、

可以在安全策略的控制下解除 D、已经缺省地全部解除
46. 下面关于Applet的说法正确的是__B____。
A、Applet也需要main方法 B、Applet继承自java.awt.Applet或javax.swing.JApplet
C、Applet能访问本地文件 D、Applet程序不需要编译
47下面哪个用来在Container上排列GUI构件__D____。
A BorderLayout类
B Component类
C 事件控制
D 布局管理器

48.以下哪个为构件为容器设置布局管理器__B____。
A BorderLayout
B setLayout
C Container
D Component
49.任何一个Applet开始执行时必然会被自动调用三个方法,它们被调用的顺序是_D_
A init paint start
B start paint init
C paint start init
D init start paint
50.每当浏览器的用户离开applet所在的HTML页时都调用方法_A__
A stop()
B init ()
C paint()
D repaint()

二.填空题
1.一个Java Application源程序文件名为MyJavaApplication.java,如果使用Sun公司 的Java开发工具JDK编译该源程序文件并使用其虚拟机运算这个程序的字节码文件, 应该首先执行的命令是: __ javac MyJavaApplication.java __。
2.Swing的事件处理机制包括__事件源__、事件和事件处理者。
3.FlowLayout是Panel和Applet的默认布局管理器
4.BorderLayout是Window,Frame和Dialog的默认布局管理器
5. 包含Swing构件的Applet(小应用程序)应该是javax.swing.JApplet 或 JApplet类的直接子类
6.如果有一个类MyFrame是Frame的子类,能够被不同包中的类所使用,请写出该类的声明头:public class MyFrame extends Frame
7.容器java.awt.Container是Component的子类,一个容器可以容纳多个构件,并使他们成为一个整体。
8. 常用的容器有Frame(窗口容器),Panel(面板容器),Applet(java小程序的容器),Dialog(对话框容器)等
9. 对JFrame添加构件有两种方式:getContentPane()方法和Jpanel
10.java.awt包是Java语言用来构建图形用户界面(GUI)的类库
11每个Applet程序必须有一个类是__Applet__类的子类。
12. .如果有一个类MyFrame是Frame的子类,能够被不同包中的类所使用,请写出该类的 声明头:__public class MyFrame extends Frame__
13. Applet是能够嵌入到 _HTML____ 格式的文件中,并能够在浏览器中运行的Java。
14. 使用Swing编写Applet,则该Applet的主类应该定义为___Applet_______ 类的子类。
15编译Java Applet 源程序文件产生的字节码文件的扩展名为_class__。
16. 在Applet中显示文字、图形等信息时,应使用的方法是_paint()___
17. Java应用程序图形界面基础的是_Frame_________
18. BorderLayout 是Window, Frame和Dialog的默认布局管理器。
19. FlowLayout 是Panel和Applet的默认布局管理器.
20. 构件在容器中的放置规律是从上到下、从左到右进行放置.
三.是非题
1. Java Applet的字节码文件

必须嵌入HTML的文件中并由负责解释HTML文件的WWW浏览器充当解释器来解释运行。(√)
2. java.awt包是Java语言用来构建图形用户界面(GUI)的类库。(√)
3. 容器java.awt.Container是Component的子类,一个容器可以容纳多个构件,并使他们成为一个整体。(√)
4. Java中所有的类都是通过直接或间接地继承https://www.wendangku.net/doc/3b2358554.html,ng.Object类得到的。(√)
5. Java的图形用户界面的最基本的组成部分就是构件(Component)。(√)
6. 在Swing中用重量级的构件替代了AWT中的轻量级的构件。(X)
7. 与AWT构件不同,Swing构件不能直接添加到顶层容器中,它必须添加到一个与Swing顶层容器相关联的内容面板(contentPane)上。(√)
8. 类Panel默认的布局管理器是GridLayout。(X)
9. 每个Java小应用程序必须定义为Jframe类的子类。(√)
10. Applet是能够嵌入到HTML 格式的文件中,并能够在浏览器中运行的Java。(√)
11.AWT组件称为重型组件(√)
12.Swing组件称为轻型组件(√)
13.容器类不是GUI组件(X)
14.使用add方法可以在框架的内容窗格中添加组件(√)
15.FlowLayout 是最简单的布局管理器(√)
16.布局管理器GridLayout不是以网格(矩阵)的形式排列组件(X)
17.布局管理器的属性不可以动态的改变(X)
18.每一时刻,一个容器只能有一个布局管理器(√)
19.要创建一个用户界面,需要创建一个框架或applet来存放用户界面组件(√)
https://www.wendangku.net/doc/3b2358554.html,ponent类是所有用户界面的父类(√)
四.程序题
1.编写程序显示一个3X3的网格,,使用红线画垂直线,使用蓝线画水平线。
import javax.swing.*;
import java.awt.*;
public class Exercise1 extends JFrame {
// Create two buttons
private OvalButton jbtOk = new OvalButton("OK");
private OvalButton jbtCancel = new OvalButton("Cancel");

/** Default constructor */
public Exercise11_6() {
// Set the window title
setTitle("Exercise11_6");

// Set FlowLayout manager to arrange the components
// inside the frame
getContentPane().setLayout(new FlowLayout());

// Add buttons to the frame
getContentPane().add(jbtOk);
getContentPane().add(jbtCancel);
}

/** Main method */
public static void main(String[] args) {
Exercise11_6 frame = new Exercise11_6();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 80);
frame.setVisible(true);
}
}

class OvalButton extends JButton {
public OvalButton() {
}

public OvalButton(String text) {
super(text);
}

protected void paintComponent(Graphics g) {
// Draw an oval
super.paintComponent(g);
g.drawOval(5, 5, getWidth() - 10, getHeight() - 10);
}

/** Override get method for preferredSize */
public Dimension getPreferredSize()

{
return new Dimension(100, 50);
}

/** Override get method for minimumSize */
public Dimension getMinimumSize() {
return new Dimension(100, 50);
}

/** Main method */
public static void main(String[] args) {
// Create a frame and add two OvalButtons to
// the frame
}
}
2.编写程序,用绘画方法在画板上显示乘法表。如图所示:
Multiplication Table
1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
………………………
2
3
4
5
6
7
8
9


import java.awt.*;
import javax.swing.*;

public class Exercise2 extends JFrame {
public Exercise11_8() {
getContentPane().add(new MultiplicationTablePanel());
}

public static void main(String[] args) {
Exercise11_8 frame = new Exercise11_8();
frame.setSize(300, 400);
frame.setTitle("Exercise11_8");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MultiplicationTablePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int x = 10;
int y = 40;
String s = "";
int i = 0;

// Display the title
g.setColor(Color.red);
g.setFont(new Font("Times", Font.BOLD, 20));
g.drawString("Multiplication Table", x+50, y);

g.setFont(new Font("Times",Font.BOLD,15));

y += 30;
for (i = 1; i < 10; i++)
g.drawString(" " + i, x + 10, y + 10 + i * 20);

x += 40;
for (i = 1; i < 10; i++) {
s = s + " " + i;
}
g.drawString(s, x, y);

y += 10;
g.drawRect(x, y, 200, 200);

s = "";
y += 20;

for (i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (i*j < 10)
s = s + " " + i * j;
else
s = s + " " + i * j;
}

g.drawString(s, x, y);
s = "";
y += 20;
}
}
}
3.编写程序将数字显示成三角形图案如图所示
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
import java.awt.*;

import javax.swing.*;
public class exercise 3{
public static void main(String []args)
{
JFrame frame=new JFrame();
frame.getContentPane().add(new DrawTrangle());
frame.setTitle("exercise 11.7");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class DrawTrangle extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.setFont(new Font("Serif",Font.BOLD,20));
for(int i=1;i<=11;i++)
{
String s="";
for(int j=1;j<=i;j++)
{
if(j<10)
{
s+=" "+j;
}
else
s+=" "+j;

}
g.drawString(s, 20,20+i*20);
}
}
}
4.编写程序,绘制一个八边形,如图所示
import java.awt.*;
import javax.swing.*;

public class Exercise4 extends JFrame {
public static void main(String[] args) {
JFrame frame = new Exercise11_12();
frame.setSize(300, 300);
frame.setTitle("Exercise11_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public Exercise11_12() {

getContentPane().add(new OctagonPanel());
}
}

class OctagonPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius =
(int) (Math.min(getWidth(), getHeight()) * 0.4);

// Create a Polygon object
Polygon polygon = new Polygon();

// Add points to the polygon
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius * Math.cos(2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(2 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(2 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(3 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(3 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(4 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(4 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(5 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(5 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(6 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(6 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * Math.cos(7 * 2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(7 * 2 * Math.PI / 8)));

// Draw the polygon
g.drawPolygon(polygon);
}
}
5.编写一个程序,画出函数f(x)=x2的图形
import javax.swing.*;
public class exercise5 {
public static void main(String args[])
{
PolylineFrame frame=new PolylineFrame();
frame.setTitle("exercise 11.15");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class PolylineFrame extends JFrame{
public PolylineFrame(){
Container container=getContentPane();
container.setLayout(new BorderLayout());
container.add(new DrawPolyline(),BorderLayout.CENTER);
}

}
class DrawPolyline extends JPanel{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(10, 200, 390, 200);
g.drawLine(200, 20, 200, 290);
g.drawString("∧", 195, 25);
g.drawString("〉", 390, 205);
g.drawString("X", 390,190);
g.drawString("Y", 210, 15);
Polygon polygon=new Polygon();
double scaleFactor=0.1;
for(int x=-100;x<=100;x++)
{
polygon.addPoint(x

+200, 200-(int)(scaleFactor*x*x));
}
g.drawPolyline(polygon.xpoints, polygon.ypoints,polygon.npoints);
g.drawString("0", 200, 200);
}
}
6.画出正弦函数的图形
import java.awt.*;
import javax.swing.*;

public class Exercise 6 extends JFrame {
public Exercise11_16() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
Exercise11_16 frame = new Exercise11_16();
frame.setSize(400, 300);
frame.setTitle("Exercise11_16");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

// Inner class
class DrawSine extends JPanel {
double f(double x) {
return Math.sin(x);
}

// Draw the function
public void drawFunction() {
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);

// Draw arrows
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);

// Draw x, y axises
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);

// Draw function
Polygon p = new Polygon();

for (int x = -100; x <= 100; x++) {
p.addPoint(x + 200, 100 - (int)(50 * f((x / 100.0) * 2 * Math.PI)));
}

g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
g.drawString("-2\u03c0", 95, 115);
g.drawString("2\u03c0", 305, 115);
g.drawString("0", 200, 115);
}
}
}
7.编写程序,使用饼图显示作业(20%),平时成绩(10%),期中(30%)和期末考试(40%)占总成绩的百分比
import java.awt.Color;
import java.awt.*;
import javax.swing.*;
public class exericse7 {
public static void main(String []args)
{
ArcsFrame frame=new ArcsFrame();
frame.setTitle("exercise 11.11");
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
class Arcs extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
int radius=(int)(Math.min(getWidth(), getHeight())*0.4);
int x=xCenter-radius;
int y=yCenter-radius;
g.setColor(Color.red);
g.fillArc(x,y,2*radius,2*radius,0,72);
g.setColor(Color.blue);
g.fillArc(x,y,2*radius,2*radius,72,36);
g.setColor(Color.green);
g.fillArc(x,y,2*radius,2*radius,108,108);
g.setColor(Color.black);
g.drawString("Quizzes---10%", x+100, y);
g.drawString("Projects---20%",x+180,y+40);
g.drawString("Midterms---30%",x-30,y+50);
g.drawString("Final---40%",x+170,y+250);
}
}
class ArcsFrame extends JFrame{
public ArcsFrame(){
Container container=getContentPane();
container.setLayout(new BorderLayout

());
container.add(new Arcs(),BorderLayout.CENTER);
}

}
8.编写程序,使用柱显示作业(20%),平时成绩(10%),期中(30%)和期末考试(40%)占总成绩的百分比。
import java.awt.*;
import javax.swing.*;

public class Exercise8 extends JFrame {
public Exercise11_18() {
Container container = getContentPane();
container.setLayout(new GridLayout(1, 3, 20, 5));

BarChart chart1 = new BarChart();
double[] data1 = {20, 10, 30, 40};
String[] dataName1 = {"Project -- 20%", "Quizzes -- 10%", "Midtems -- 30%", "Final -- 40%"};
chart1.setData(dataName1, data1);
container.add(chart1);
}

public static void main(String[] args) {
Exercise11_18 frame = new Exercise11_18();
frame.setTitle("Exercise11_18");
frame.setSize(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
9.编写一个国际象棋棋盘
import java.awt.*;
import javax.swing.*;
public class exercise9 {
public static void main(String args[])
{
RectFrame frame=new RectFrame();
frame.setTitle("exercise 11.7");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
class RectPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(getWidth()/8, 0, getWidth()/8, getHeight());
g.fillRect(3*getWidth()/8, 0, getWidth()/8, getHeight());
g.fillRect(5*getWidth()/8, 0, getWidth()/8, getHeight());
g.fillRect(7*getWidth()/8, 0, getWidth()/8, getHeight());
}
public Dimension getPreferredSize() {
return new Dimension(100, 50);
}

}
class FillRectPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(0, 0, getWidth()/8, getHeight());
g.fillRect(getWidth()/4, 0, getWidth()/8, getHeight());
g.fillRect(getWidth()/2, 0, getWidth()/8, getHeight());
g.fillRect(3*getWidth()/4, 0, getWidth()/8, getHeight());
}

}
class RectFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;

public RectFrame(){
getContentPane().setLayout(new GridLayout(8,1));
getContentPane().add(new RectPanel());
getContentPane().add(new FillRectPanel());
getContentPane().add(new RectPanel());
getContentPane().add(new FillRectPanel());
getContentPane().add(new RectPanel());
getContentPane().add(new FillRectPanel());
getContentPane().add(new RectPanel());
getContentPane().add(new FillRectPanel());

}

}
10.编写一个名为OvalButton的自定义按钮类,它扩展JButton,把按钮上的文本显示在一个椭圆中,如图所示



import java.awt.*;
import javax.swing.*;
public class exercise10 {
public static void main(String []args

)
{
DrawOval frame=new DrawOval();
frame.setTitle("Exercise 11.6");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(150,100);
frame.setVisible(true);
}


}
class OvalButton extends JButton{
/**
*
*/
private static final long serialVersionUID = 1L;
public OvalButton(String s)
{
super(s);
}
protected void paintComponnet(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawOval(5, 5, getWidth()-10,getHeight()-10);
}
/*public Dimension getPreferredSize()
{
return new Dimension(100, 50);
}
public Dimension getMinimumSize()
{
return new Dimension(100, 50);
}*/
}
class DrawOval extends JFrame{
/**
*
*/
private static final long serialVersionUID = -2301011935626641177L;

public DrawOval()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
OvalButton ovalButton1=new OvalButton("Ok");
OvalButton ovalButton2=new OvalButton("Cancel");
container.add(ovalButton1);
container.add(ovalButton2);
}
}

相关文档
相关文档 最新文档