Java程序设计 实验报告2

Java程序设计上机实验报告

第_1_次上机实验报告

一.

二. 实验题目

1. 编写字符界面的java application,将用户输入的小写字符转化成大写字符打印在屏幕上,若用户输入的不是小写字符则打印信息说明无法完成的操作。

2. 编写一个Applet程序,从键盘输入一个整数,求出其阶乘值

三. 源程序(包含注释)

1.

//This program is used for change upper

import java.io.*;

public class upper {

private void l2u(char c) {

if (c < 97 || c > 122) {

System.out.println("Error !");

}

else

{

System.out.print("The Upper is:");

System.out.println((char) (c - 32));

}

}

public static void main(String[] args) {

upper upper = new upper();

System.out.println("Please Input the Character:");

InputStream is = System.in;

try {

char c = (char) (is.read());

upper.l2u(c);

} catch (IOException ex) {

}

}

}

2.

//This program is used for calculating factorial

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class factorial extends Applet implements ActionListener

{

Label prompt;

TextField input,output;

public void init()

{

prompt = new Label("请输入一个整数:");

input = new TextField(6);

output = new TextField(20);

add(prompt);

add(input);

add(output);

input.addActionListener(this);

}

public int Sum()

{

int s = Integer.valueOf(input.getText());

int sum=1;

for(int i=s;i>0;i--){

sum*=i;

}

return sum;

}

public void actionPerformed(ActionEvent e)

{

}

} output.setText(input.getText()+"的阶乘为"+Sum());

一. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

1.编写字符界面的java application,将用户输入的小写字符转化成大写字符打印在屏 幕上,若用户输入的不是小写字符则打印信息说明无法完成的操作。

Java程序设计实验报告2

2.编写一个Applet程序,从键盘输入一个整数,求出其阶乘值

Java程序设计实验报告2

第_2_次上机实验报告

二.

三. 实验题目

1. 阅读下面的程序,在main()方法里添加语句完成如下的功能:

(1) 创建一个MyValue类的对象myValue

(2) 为myValue对象中的value域赋值10

(3) 使用getValue()方法获得myValue对象中的数据并将它们打印在屏幕上

class MyValue

{

private int value;

public void setValue(int x)

{

Value=x;

}

public int getValue()

{

Returen value;

}

}

public class UseValue

{

public static void main(String args[])

{

}

}

2.编写Java代码实现一个计数器类Counter, 其中包含:

域counterValue 用来保存计数器的当前数值

方法increment(), 计数器加一

方法decrement(), 计数器减一

方法reset(), 计数器清零

3.编程实现矩形类,其中包括计算矩形周长和面积的方法。

4.使用矩形类,编程统计若干块土地的相关信息,由用户输入每块土地的长与宽,程序将计算其面积并显示出来。

四. 源程序(包含注释)

//This is a counter class

class counter {

int counterValue = 0;

public int increment(){

counterValue++;

return counterValue;

}

public int decrement(){

counterValue--;

return counterValue;

}

public int reset(){

counterValue = 0;

return counterValue;

}

}

import java.io.*;

import java.util.Scanner;

import MyFigures.rectangle;

public class landCalculate {

public static void main(String args[]) throws System.out.print( "Input the width:" ); myRectangle.width = ( int ) ( input.nextInt() ); //判断程序终止 if( (myRectangle.length == 0) && (myRectangle.width == 0) ){ } System.out.println( "The girth is " + System.out.println( "End" ); break; counter myCounter = new counter(); rectangle myRectangle = new rectangle(); Scanner input = new Scanner( System.in ); while( true ){ //Break the loop when both the inputs //录入信息 System.out.print( "Input the length:" ); myRectangle.length = ( int ) ( input.nextInt() ); IOException{ //main method are zero myRectangle.findGirth() );

} } } System.out.println( "The area is " + myRectangle.findArea() ); myCounter.increment(); System.out.println( "There is " + myCounter.counterValue + " System.out.println( " " ); lands in total." );

package MyFigures;

/*

public class rectangle { //长方形类

public int length;

public int width; public int findGirth (){ } public int findArea(){ } return length * width; return ( length + width ) * 2;

}*/

public class rectangle implements Printable{ //修改的长方形类 public int length;

public int width; public int findGirth (){ } public int findArea(){ } public void printItMyWay(){ //打印 System.out.println( "The length is " + length ); } public void printItMyFigure( char c ){ } for( int i = 0; i < width; i++ ){ //行循环 for( int j = 0; j < length; j++){ //列循环 System.out.print( c ); //打印c字符 } System.out.println( "The width is " + width ); System.out.println( "The girth is " + findGirth() ); System.out.println( "The area is " + findArea() ); return length * width; return ( length + width ) * 2; System.out.println( "" ); //每行末尾换行 }

}

五. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

源程序定义了一个counter类,实现对输入次数的计算,其中有counter++,counter--,清零等运算。调用了java.io.*;java.util.Scanner;两个个系统类库。运行中开始的时候会有无法输出的问题,没有考虑到输出格式的问题,数据的录入和输出很麻烦,最终定义了一个自己的类,才解决这类问题。

创建一个MyValue类的对象myValue

为myValue对象中的value域赋值10

使用getValue()方法获得myValue对象中的数据并将它们打印在屏幕上

编写Java代码实现一个计数器类Counter, 其中包含:

域counterValue 用来保存计数器的当前数值

方法increment(), 计数器加一

方法decrement(), 计数器减一

方法reset(), 计数器清零

编程实现矩形类,其中包括计算矩形周长和面积的方法。

使用矩形类,编程统计若干块土地的相关信息,由用户输入每块土地的长与宽,程序将计算其面积并显示出来。

Java程序设计实验报告2

第_3_次上机实验报告

一.

二. 实验题目

1) 为上一实验中的矩形类派生一个子类:正方形类。若正方形类的操作同样是求周长和面

积,则这个字类除了从父类继承来的方法之外,还需要定义哪些方法。列出正方形类的所有域和方法。编程检查、运行所编写的正方形类。

2) 定义接口Printable, 其中包括一个方法printItMyWay(),这个方法没有形参,返回值为空。

3) 改写矩形类,使之实现Printable接口,用printItMyWay()方法将矩形的相关信息(长,

宽,周长,面积)打印在屏幕上。

4) 改写正方形类,重载printItMyWay()方法,将正方形的边长、周长,面积打印在屏幕上。

5) 在Printable接口中增加一个新的printItMyWay(char)方法,这个方法有一个字符类型的

形参,返回值为空。其功能是利用给出的字符打印,例如若给定的字符为#,一个长为5,宽为3的矩形的屏幕打印结果为:

#####

#####

#####

6) 在矩形类和正方形类中实现该方法.

7)将矩形和正方形组成一个包MyFigures.

三. 源程序(包含注释)

package MyFigures;

/*

public class square extends rectangle{

return length * 4;

}

return length * length;

}

}*/

public class square implements Printable{

int length;

int squareGirth(){

} return length * 4; } int squareArea(){ return length * length; } public void printItMyWay(){ System.out.println( "The length is " + length ); System.out.println( "The girth is " + squareGirth() ); System.out.println( "The area is " + squareArea() ); } public void printItMyFigure( char c ){ for( int i = 0; i < length; i++ ){ //行循环 for( int j = 0; j < length; j++){ //列循环 System.out.print( c ); //打印c字符 } System.out.println( "" ); //每行末尾换行 } }

package MyFigures;

public interface Printable{

/*

public class rectangle { //长方形类

public int length;

public int width; public int findGirth (){ } public int findArea(){ } return length * width; return ( length + width ) * 2; void printItMyWay(); void printItMyFigure ( char c ); } package MyFigures;

}*/

public class rectangle implements Printable{ //修改的长方形类 public int length;

public int width; public int findGirth (){ } public int findArea(){ return ( length + width ) * 2;

} } return length * width; public void printItMyWay(){ //打印 System.out.println( "The length is " + length ); } public void printItMyFigure( char c ){ } for( int i = 0; i < width; i++ ){ //行循环 for( int j = 0; j < length; j++){ //列循环 System.out.print( c ); //打印c字符 } System.out.println( "The width is " + width ); System.out.println( "The girth is " + findGirth() ); System.out.println( "The area is " + findArea() ); System.out.println( "" ); //每行末尾换行 }

四. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

为上一实验中的矩形类派生一个子类:正方形类。若正方形类的操作同样是求周长和面积,则这个字类除了从父类继承来的方法之外,还需要定义哪些方法。列出正方形类的所有域和方法。编程检查、运行所编写的正方形类。

定义接口Printable, 其中包括一个方法printItMyWay(),这个方法没有形参,返回值为空。 改写矩形类,使之实现Printable接口,用printItMyWay()方法将矩形的相关信息(长,宽,周长,面积)打印在屏幕上。

改写正方形类,重载printItMyWay()方法,将正方形的边长、周长,面积打印在屏幕上。 在Printable接口中增加一个新的printItMyWay(char)方法,这个方法有一个字符类型的形参,返回值为空。其功能是利用给出的字符打印,例如若给定的字符为#,一个长为5,宽为3的矩形的屏幕打印结果为:

#####

#####

#####

在矩形类和正方形类中实现该方法.将矩形和正方形组成一个包MyFigures.

Java程序设计实验报告2

第_4_次上机实验报告

一.

二. 实验题目

1) 编程接收用户输入的一个正浮点数,随机生成一个不大于该数的正整数,在屏幕打印输

出。

2) 使用数组实现一个10到20的平方表,打印输出这个平方表。

3) 编程记录用户输入的一组正整数数据,-1表示输入结束,求这些数据的平均值,并统

计大于平均值的数据个数。

4) 编写图形界面的程序,接收用户输入的一个字符串和一个字符,将字符串中的所有该字

符删除,打印输出新生成的结果字符串。

5) 编写Java Application,将用户输入的命令行参数一个一行打印出来。

三. 源程序(包含注释)

//This class is used to print a square table from 10 to 20

public class CalculateSquare {

static int[][] myList = new int[11][11];

public static void main( String[] args ){

System.out.println( " 10 11 12 13 14 15 16 17 18 19 20 " ); for( int i = 0; i < 11; i++ ){

System.out.print( (i + 10) + " " );

for( int j = 0; j < 11; j++ ){

myList[i][j] = ( i + 10 ) * ( j + 10 );

System.out.print( myList[i][j] + " " );

}

System.out.println( " " );

}

}

}

//This class is used to calculate a group of value's average

public class Everage {

}

//This class is used to input import java.io.*;

public class MyInput {

//Read a string from the keyboard public static String readString (){ BufferedReader br = new BufferedReader ( new InputStreamReader String string = " "; try{ } string = br.readLine(); static int[] number = new int[100]; public static void main( String[] args ){ } int i = 0; int sum = 0; int count = 0; System.out.println( "Input number : " ); number[i] = MyInput.readInt(); while( number[i] != (-1) ){ } double aver = sum / i; System.out.println( "The average is : " + aver ); System.out.println( "The value which greater than average are : for( int j = 0; j < i; j++ ){ } System.out.println( " " ); System.out.println( "There are " + count + " values in total." ); if( number[j] > aver ){ } count++; System.out.print( number[j] + " " ); sum += number[i]; i++; number[i] = MyInput.readInt(); " ); ( System.in ), 1 );

} } catch( IOException ex ){ } return string; System.out.println( ex ); //Read an integer value from the keyboard public static int readInt(){ } //Read a double value from the keyboard public static double readDouble(){ } return Double.parseDouble( readString() ); return Integer.parseInt( readString() );

//This is the first problem ,which is used to create a random integer //import java.io.*;

public class receiveDouble {

} public static void main ( String[] args ){ } System.out.println ( "Input a double:" ); double myDouble = MyInput.readDouble(); //Read a double value int randomValue = ( int ) ( 1 + Math.random()*10 ); //Create a while( randomValue > myDouble ) { } System.out.println( "The random int value is : " + randomValue ); randomValue = ( int ) ( 1 + Math.random()*10 ); from the keyboard random double and turn to integer

四. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

考虑到输入问题,自己定义了一个类myinput,这样对数据的输入很方便。

编程接收用户输入的一个正浮点数,随机生成一个不大于该数的正整数,在屏幕打印输出。

Java程序设计实验报告2

使用数组实现一个10到20的平方表,打印输出这个平方表。

Java程序设计实验报告2

编程记录用户输入的一组正整数数据,-1表示输入结束,求这些数据的平均值,并统计大于平均值的数据个数。

Java程序设计实验报告2

第_5_次上机实验报告

一.

二. 实验题目

1) 为第2次实验中编写的计数器类Counter编写图形界面,其中包括三个按钮、一个标签。

按钮分别是计数器加一、计数器减一和计数器清零,标签显示当前的计数器数值。

2) 编程实现一个模拟手机的图形用户界面,包括一个显示区和一个键盘区,显示区用来显

示数字和信息,键盘区包括0~910个数字键和“发送”“清除”“关机”三个命令键。考虑应该使用什么样的图形用户界面布局策略。

3) 为上题所编写的界面增加事件响应功能:当用户按数字键时,相应的数字回显在显示区

上,当用户按清除键时,显示区的数字清空,当用户按发送键时,显示区出现当前的号码和“拨号中。。。。。”的信息,当用户按关机键时结束程序。

4) 编程实现一个学生信息管理系统的图形界面。需要录入的信息包括学生学号、姓名、性

别、出生年、月、日、年级以及是否联合培养。根据这些信息的性质选择使用不同的图形组件来设计界面。

5) 为4)所编写的图形界面增加事件响应功能。当用户选定了一个出生月份,例如3月份

时,系统自动把日期的选择范围限制在1~31之间,当用户单击“录入”按钮“时,系统把所有的学生信息打印在屏幕上。

6) 编写一个图形界面的Java Application?为用户提供三种关闭窗口的方法:使用按钮,适用

菜单项,使用窗口关闭图标。

三. 源程序(包含注释)

/* 1) 为第2次实验中编写的计数器类Counter编写图形界面,

* 其中包括三个按钮、一个标签。按钮分别是计数器加一、

* 计数器减一和计数器清零,标签显示当前的计数器数值。

*/

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class GraphicInterfaceofCounter {

JTextField intField = new JTextField();

JButton addButton = new JButton( "Add" );

JButton decreaseButton = new JButton( "Decrease" );

JButton clearingButton = new JButton( "Clear" );

}

/*2) 编程实现一个模拟手机的图形用户界面,

* 包括一个显示区和一个键盘区,显示区用来显示数字和信息,

* 键盘区包括0~910个数字键和“发送”“清除”“关机”三个命令键。

* 考虑应该使用什么样的图形用户界面布局策略

* 当用户按数字键时,响应的数字回现在显示区上;

* 当用户按“清除”键时,显示区清空;当用户 按“发送”键时

* ,显示区出现当前的号码和“拨号中??”的信息;当用户按“关闭”键时,结束程序。 */

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Mobile extends JFrame {

private static final long serialVersionUID = 1L;

private JTextField txtView;

public static void main(String args[]) {

try {

Mobile moble = new Mobile();

moble.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the frame

*/

public Mobile() {

setTitle("Moble");

setBounds(100, 100, 200, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();

getContentPane().add(panel, BorderLayout.CENTER);

txtView = new JTextField();

panel.setLayout(null);

panel.add(txtView);

final JButton btn1 = new JButton();

btn1.addActionListener(new ActionListener() { //加监听

public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("1")); }

});

btn1.setText("1");

btn1.setBounds(0, 73, 59, 23);

panel.add(btn1);

final JButton btn2 = new JButton();

btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //文本显示

txtView.setText(txtView.getText().concat("2")); }

});

btn2.setText("2");

btn2.setBounds(68, 73, 59, 23);

panel.add(btn2);

final JButton btn3 = new JButton();

btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("3")); }

});

btn3.setText("3");

btn3.setBounds(133, 73, 59, 23);

panel.add(btn3);

final JButton btn4 = new JButton();

btn4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("4")); }

});

btn4.setText("4");

btn4.setBounds(0, 102, 59, 23);

panel.add(btn4);

final JButton btn5 = new JButton();

btn5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("5")); }

});

btn5.setText("5");

btn5.setBounds(65, 102, 59, 23);

panel.add(btn5);

final JButton btn6 = new JButton();

btn6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("6")); }

});

btn6.setText("6");

btn6.setBounds(133, 102, 59, 23);

panel.add(btn6);

final JButton btn7 = new JButton();

btn7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("7")); }

});

btn7.setText("7");

btn7.setBounds(0, 131, 59, 23);

panel.add(btn7);

final JButton btn8 = new JButton();

btn8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("8"));

}

});

btn8.setText("8");

btn8.setBounds(65, 131, 59, 23);

panel.add(btn8);

final JButton btn9 = new JButton();

btn9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("9")); }

});

btn9.setText("9");

btn9.setBounds(133, 131, 59, 23);

panel.add(btn9);

final JButton btn0 = new JButton();

btn0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("0")); }

});

btn0.setText("0");

btn0.setBounds(68, 160, 57, 23);

panel.add(btn0);

txtView = new JTextField();

txtView.setBounds(59, 24, 90, 21);

panel.add(txtView);

final JButton btnSend = new JButton();

btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //如果拨过号了,就不能再拨

if(txtView.getText().indexOf("calling...")==-1){

txtView.setText(txtView.getText().concat("calling...")); }

else{

return;

}

}

});

btnSend.setText("Send");

btnSend.setBounds(40, 189, 127, 23);

panel.add(btnSend);

final JButton btnclear = new JButton();

btnclear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText("");

}

});

btnclear.setText("Clear");

btnclear.setBounds(40, 212, 127, 23);

panel.add(btnclear);

final JButton btnOff = new JButton();

btnOff.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { dispose();

}

});

btnOff.setText("Quit");

btnOff.setBounds(40, 232, 127, 23);

panel.add(btnOff);

}

}

//This is a counter class

class counter {

} int counterValue = 0; public int increment(){ } public int decrement(){ } public int reset(){ } counterValue = 0; return counterValue; counterValue--; return counterValue; counterValue++; return counterValue;

import javax.swing.*;

import java.awt.event.*;

public class GraphicCounter {

JFrame frame = new JFrame("Counter");

JButton btnAdd = new JButton("加一"); JButton btnMinus=new JButton("减一"); JButton btnClear=new JButton("清零"); JLabel label = new JLabel("Counter is 0"); int i; public GraphicCounter() { frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.getContentPane().setLayout( new java.awt.FlowLayout() ); /* 为一般按钮添加动作监听器 */ btnAdd.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { i++; label.setText("Counter is "+i); } }); btnMinus.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { i--; label.setText("Counter is "+i); } }); btnClear.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { i=0; label.setText("Counter is "+i); } }); frame.getContentPane().add(btnAdd); frame.getContentPane().add(btnMinus); frame.getContentPane().add(btnClear); frame.getContentPane().add(label); frame.setSize(200, 150); }

} @SuppressWarnings("deprecation") public void show() { frame.show(); } public static void main(String[] args) { GraphicCounter ct = new GraphicCounter(); ct.show(); }

/*2) 编程实现一个模拟手机的图形用户界面,

* 包括一个显示区和一个键盘区,显示区用来显示数字和信息,

* 键盘区包括0~910个数字键和“发送”“清除”“关机”三个命令键。

* 考虑应该使用什么样的图形用户界面布局策略

* 当用户按数字键时,响应的数字回现在显示区上;

* 当用户按“清除”键时,显示区清空;当用户 按“发送”键时

* ,显示区出现当前的号码和“拨号中??”的信息;当用户按“关闭”键时,结束程序。 */

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Mobile extends JFrame {

private static final long serialVersionUID = 1L;

private JTextField txtView;

public static void main(String args[]) {

try {

Mobile moble = new Mobile();

moble.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the frame

*/

public Mobile() {

setTitle("Moble");

setBounds(100, 100, 200, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();

getContentPane().add(panel, BorderLayout.CENTER);

txtView = new JTextField();

panel.setLayout(null);

panel.add(txtView);

final JButton btn1 = new JButton();

btn1.addActionListener(new ActionListener() { //加监听

public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("1")); }

});

btn1.setText("1");

btn1.setBounds(0, 73, 59, 23);

panel.add(btn1);

final JButton btn2 = new JButton();

btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //文本显示

txtView.setText(txtView.getText().concat("2")); }

});

btn2.setText("2");

btn2.setBounds(68, 73, 59, 23);

panel.add(btn2);

final JButton btn3 = new JButton();

btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("3")); }

});

btn3.setText("3");

btn3.setBounds(133, 73, 59, 23);

panel.add(btn3);

final JButton btn4 = new JButton();

btn4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("4")); }

});

btn4.setText("4");

btn4.setBounds(0, 102, 59, 23);

panel.add(btn4);

final JButton btn5 = new JButton();

btn5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("5")); }

});

btn5.setText("5");

btn5.setBounds(65, 102, 59, 23);

panel.add(btn5);

final JButton btn6 = new JButton();

btn6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("6")); }

});

btn6.setText("6");

btn6.setBounds(133, 102, 59, 23);

panel.add(btn6);

final JButton btn7 = new JButton();

btn7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("7")); }

});

btn7.setText("7");

btn7.setBounds(0, 131, 59, 23);

panel.add(btn7);

final JButton btn8 = new JButton();

btn8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("8")); }

});

btn8.setText("8");

btn8.setBounds(65, 131, 59, 23);

panel.add(btn8);

final JButton btn9 = new JButton();

btn9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("9")); }

});

btn9.setText("9");

btn9.setBounds(133, 131, 59, 23);

panel.add(btn9);

final JButton btn0 = new JButton();

btn0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txtView.setText(txtView.getText().concat("0")); }

});

btn0.setText("0");

btn0.setBounds(68, 160, 57, 23);

panel.add(btn0);

txtView = new JTextField();

txtView.setBounds(59, 24, 90, 21);

panel.add(txtView);

final JButton btnSend = new JButton();

btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //如果拨过号了,就不能再拨

if(txtView.getText().indexOf("calling...")==-1){

txtView.setText(txtView.getText().concat("calling...")); }

else{

return;

}

}

});

btnSend.setText("Send");

btnSend.setBounds(40, 189, 127, 23);

panel.add(btnSend);

final JButton btnclear = new JButton();

btnclear.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

txtView.setText("");

}

});

btnclear.setText("Clear");

btnclear.setBounds(40, 212, 127, 23);

panel.add(btnclear);

final JButton btnOff = new JButton();

btnOff.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

dispose();

}

});

btnOff.setText("Quit");

btnOff.setBounds(40, 232, 127, 23);

panel.add(btnOff);

}

}

/*4) 编程实现一个学生信息管理系统的图形界面。

* 需要录入的信息包括学生学号、姓名、性别、出生年、月、日、年级以及是否联合培养。 * 根据这些信息的性质选择使用不同的图形组件来设计界面。

*/

import java.awt.BorderLayout;

import java.awt.Checkbox;

import java.awt.CheckboxGroup;

import java.awt.Container;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

//录入的信息包括学号、姓名、性别、出生日期、是否联合培养等

public class 学生信息图形界面 extends JFrame implements ActionListener {

public 学生信息图形界面() { //构造方法

//界面显示问题

super("学生信息图形界面");

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setSize(320, 300);

setVisible(true);

Container content = getContentPane();

JPanel jp1 = new JPanel(); //创建一个面板

JLabel label = new JLabel("欢迎使用学生信息管理系统"); //欢迎界面的欢迎语句 label.setFont(new Font("楷体_GB2312", Font.BOLD, 20)); //设置显示的字体

jp1.add(label); //将标签添加到面板jp1中

JPanel jp2 = new JPanel(); //创建一个面板,用于摆放提示各种功能的标签

JLabel label1 = new JLabel("请输入学生的详细资料:"); //输入提示输入的语言: label1.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JLabel label2 = new JLabel("学 生 学 号:"); //提示各种输入内容的标签

label2.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体t

JTextField field1=new JTextField(10); //10为学号长度,可自行设定

JLabel label3 = new JLabel("学 生 姓 名:"); //提示输入学生姓名的标签

label3.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JTextField field2=new JTextField(10); //10为姓名长度,可自行设定

JLabel label4 = new JLabel("学 生 性 别: "); //提示输入学生性别的标签

label4.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

CheckboxGroup group1 = new CheckboxGroup(); //创建一个复选框用于选择某一功能

Checkbox check11 = new Checkbox("男",true,group1); //添加选项到复选框中 check11.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

check11.addItemListener(new CheckHandler1(1)); //给相应复选框添加事件监听器 Checkbox check12 = new Checkbox("女",false,group1); //添加选项到复选框中

check12.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

check12.addItemListener(new CheckHandler1(2)); //给相应复选框添加事件监听器

JLabel label5 = new JLabel("出 生 日 期:"); //提示输入出生日期的标签

label5.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JTextField field3=new JTextField("2000",3); //3为姓名长度,可自行设定

JLabel label51 = new JLabel("年");

label51.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JTextField field4=new JTextField("01",2); //2为姓名长度,可自行设定

JLabel label52 = new JLabel("月");

label52.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JTextField field5=new JTextField("01",2); //2为姓名长度,可自行设定

JLabel label53 = new JLabel("日");

label53.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

JLabel label6 = new JLabel("联合培养:"); //提示选择联合培养与否的标签 label6.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

CheckboxGroup group2 = new CheckboxGroup(); //创建一个复选框用于选择某一功能

Checkbox check21 = new Checkbox("是",true,group2); //添加选项到复选框中

check21.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

check21.addItemListener(new CheckHandler2(1)); //给相应复选框添加事件监听器 Checkbox check22 = new Checkbox("否",false,group2); //添加选项到复选框中

check22.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); //设置显示的字体

check22.addItemListener(new CheckHandler2(2)); //给相应复选框添加事件监听器 //添加组件到面板容器中

jp2.add(label1);

jp2.add(label2);

jp2.add(field1);

jp2.add(label3);

jp2.add(field2);

jp2.add(label4);

jp2.add(check11); jp2.add(check12);

jp2.add(label5);

jp2.add(field3);jp2.add(label51); jp2.add(field4);jp2.add(label52); jp2.add(field5);jp2.add(label53);

jp2.add(label6);

jp2.add(check21); jp2.add(check22);

content.add(jp1, BorderLayout.NORTH); //分别添加面板到容器中

content.add(jp2, BorderLayout.CENTER); //分别添加面板到容器中

}

public void actionPerformed(ActionEvent e) {

// 事件响应处理

}

class CheckHandler1 implements ItemListener //复选框键的事件响应类

{

private int sel;

CheckHandler1(int select)

{

sel = select;

}

public void itemStateChanged(ItemEvent e)

{

switch(sel)

{

case 1:method1();break; //调用选择的相应功能的方法 case 2:method2();break; //调用选择的相应功能的方法 }

}

private void method1() { //事件响应方法

}

private void method2() { // 事件响应方法

}

}

class CheckHandler2 implements ItemListener //复选框键的事件响应类 {

private int sel;

CheckHandler2(int select)

{

sel = select;

}

public void itemStateChanged(ItemEvent e)

{

switch(sel)

{

case 1:method1();break; //调用选择的相应功能的方法 case 2:method2();break; //调用选择的相应功能的方法 }

}

private void method1() { //事件响应方法

}

private void method2() { //事件响应方法

}

}

//测试

public static void main(String[] args) {

学生信息图形界面 界面 = new 学生信息图形界面();

界面.setVisible(true);

}

}

/*6) 编写一个图形界面的Java Application??为用户提供三种关闭窗口的方法: * 使用按钮,

* 适用菜单项,

* 使用窗口关闭图标。

*/

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

public class Close extends JFrame implements ActionListener {

JMenuBar menu;

JMenu file;

JMenuItem closeMenu;

public Close() {

menu = new JMenuBar();

file = new JMenu("文件");

closeMenu = new JMenuItem("关闭");

closeMenu.addActionListener(this);

JButton closeButton = new JButton(" 关闭 ");

closeButton.addActionListener(this);

JPanel closePanel = new JPanel();

closePanel.setLayout(new FlowLayout());

closePanel.add(closeButton);

this.add(closePanel, BorderLayout.CENTER);

this.add(menu, BorderLayout.NORTH);

menu.add(file);

file.add(closeMenu);

this.setBounds(200, 100, 200, 120);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

public static void main(String[] args) {

new Close();

}

}

四. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

为第2次实验中编写的计数器类Counter编写图形界面,其中包括三个按钮、一个标签。按钮分别是计数器加一、计数器减一和计数器清零,标签显示当前的计数器数值。

Java程序设计实验报告2

编程实现一个模拟手机的图形用户界面,包括一个显示区和一个键盘区,显示区用来显示数字和信息,键盘区包括0~910个数字键和“发送”“清除”“关机”三个命令键。考虑应该使用什么样的图形用户界面布局策略。

Java程序设计实验报告2

为上题所编写的界面增加事件响应功能:当用户按数字键时,相应的数字回显在显示区上,当用户按清除键时,显示区的数字清空,当用户按发送键时,显示区出现当前的号码和“拨号中。。。。。”的信息,当用户按关机键时结束程序。

Java程序设计实验报告2

编程实现一个学生信息管理系统的图形界面。需要录入的信息包括学生学号、姓名、性别、出生年、月、日、年级以及是否联合培养。根据这些信息的性质选择使用不同的图形组件来设计界面。

Java程序设计实验报告2

为4)所编写的图形界面增加事件响应功能。当用户选定了一个出生月份,例如3月份时,系统自动把日期的选择范围限制在1~31之间,当用户单击“录入”按钮“时,系统把所有的学生信息打印在屏幕上。

编写一个图形界面的Java Application?为用户提供三种关闭窗口的方法:使用按钮,适用菜单项,使用窗口关闭图标。

Java程序设计实验报告2

第_6_次上机实验报告

一. 实验题目

1) 在图形界面中绘制二维的坐标系统,其中x轴位于绘图区正中,y轴位于绘图区的最左

边。

2) 根据下面试验数据,在1)题的坐标系统中绘制数据点,每个数据点用一个小叉号表示,

并在叉号边以(x,y)的格式标示出数据点的坐标数值,最后用折线连接相邻的数据点。 数据点序号 x坐标 y坐标

1 0 20

2 50 -30

3 100 16

3 150 126

5 200 164

3) 在2)题的基础上设计界面接收用户输入的实验数据,绘制数据点和实验曲线。

4) 编写程序跟踪用户的鼠标,并时刻把鼠标的位置显示出来。

5) 编写图形界面的Java程序响应鼠标单击和键盘事件,从鼠标单击的位置开始横向显示用

户键入的字符,若鼠标双击则清空屏幕。

二. 源程序(包含注释)

import java.awt.*;

import javax.swing.*;

public class DrawFigurs1 extends JFrame{

//JFrame fra=new JFrame();

JPanel pan=new Mypan();

public DrawFigurs1()

{

add(pan);

pan.setBounds(0, 0, 640, 480);

this.setLayout(null);

this.setVisible(true);

this.setSize(400, 400);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);

}

static class Mypan extends JPanel{

public void paintComponent(Graphics g) {

super.paintComponents(g);

g.setColor(Color.RED);

g.translate(0, 200);

g.drawLine(0,0, 640, 0);

g.drawLine(0, 240, 0, -240);

}

}

public static void main(String args[])

{

DrawFigurs1 dd=new DrawFigurs1();

}

}

import java.awt.*;

import javax.swing.*;

public class DrawFigurs2 extends JFrame{

JFrame fra=new JFrame();

JPanel pan=new Mypan();

public DrawFigurs2()

{

add(pan);

pan.setBounds(0, 0, 640, 480);

this.setLayout(null);

this.setVisible(true);

this.setSize(500, 600);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

static class Mypan extends JPanel{

public void paintComponent(Graphics g)

{

super.paintComponents(g);

g.setColor(Color.RED);

g.translate(0, 200);

g.drawLine(0,0, 640, 0);

g.drawLine(0, 240, 0, -240);

Point[] p = new Point[5];

for(int i = 0; i < 5; i++)

p[i] = new Point();

p[0].x = 0; p[0].y = 20;

p[1].x = 50; p[1].y = -30;

p[2].x = 100; p[2].y = 16;

p[3].x = 150; p[3].y = 126;

p[4].x = 200; p[4].y = 164;

g.setColor(Color.BLUE);

for(int i = 0; i < 5; i++)

{

g.drawLine(p[i].x-5, p[i].y-5, p[i].x+5, p[i].y+5); g.drawLine(p[i].x+5, p[i].y-5, p[i].x-5, p[i].y+5); }

g.drawString(" (0,20)", 0, 20+2);

g.drawString(" (50,-30)", 50, -30+2);

g.drawString(" (100,16)", 100, 16+2);

g.drawString(" (150,126)", 150, 126+2); g.drawString(" (200,164)", 200, 164+2); g.setColor(Color.RED);

for(int i = 0; i < 4; i++)

g.drawLine(p[i].x, p[i].y, p[i+1].x, p[i+1].y); }

}

public static void main(String args[])

{

DrawFigurs2 dd=new DrawFigurs2();

}

}

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Vector;

import javax.swing.*;

public class DrawFigurs3 extends JFrame{

JPanel pan=new Mypan();

public DrawFigurs3()

{

add(pan);

pan.setBounds(0, 0, 640, 550);

this.setLayout(null);

this.setVisible(true);

this.setSize(640, 550);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);

}

static class Mypan extends JPanel{

static JLabel label_x = new JLabel("X:"); static JLabel label_y = new JLabel("Y:"); static JTextField jtf_x = new JTextField(); static JTextField jtf_y = new JTextField();

static JButton btn = new JButton("绘制"); static Vector v = new Vector(); static Point p1 = new Point(); static Point p2 = new Point(); static int flag = 1; public Mypan() { this.setLayout(null); label_x.setBounds(40,480, 20, 20); jtf_x.setBounds(60, 480, 80, 20); jtf_x.setEditable(true); label_y.setBounds(160, 480, 20, 20); jtf_y.setBounds(180, 480, 80, 20); jtf_y.setEditable(true); btn.setBounds(300, 480, 80, 20); btn.setMargin(new Insets(0,0,0,0)); this.add(jtf_x); this.add(jtf_y); this.add(label_x); this.add(label_y);

this.add(btn);

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

if (flag == 1)

{

p1.x = Integer.parseInt(jtf_x.getText());

p1.y = Integer.parseInt(jtf_y.getText());

}

else

{

p2.x = Integer.parseInt(jtf_x.getText());

p2.y = Integer.parseInt(jtf_y.getText());

}

if (v.size() == 0) v.add(p1);

else if (v.size() == 1) v.add(p2);

repaint();

}

});

}

public void drawPoint(Graphics g)

{

int i = flag - 1;

if (v.size() != 0)

{

g.setColor(Color.BLUE);

g.drawLine(((Point)v.get(i)).x-5, ((Point)v.get(i)).y-5, ((Point)v.get(i)).x+5, ((Point)v.get(i)).y+5);

g.drawLine(((Point)v.get(i)).x+5, ((Point)v.get(i)).y-5, ((Point)v.get(i)).x-5, ((Point)v.get(i)).y+5);

g.drawString("(" + ((Point)v.get(i)).x + "," + ((Point)v.get(i)).y + ")", ((Point)v.get(i)).x+8, ((Point)v.get(i)).y+5);

}

if (v.size() >= 2)

{

for(int ii = 0; ii < v.size()-1; ii++)

{

g.setColor(Color.BLACK);

System.out.println("画线");

g.drawLine(((Point)v.get(ii)).x, ((Point)v.get(ii)).y, ((Point)v.get(ii+1)).x, ((Point)v.get(ii+1)).y);

System.out.println(((Point)v.get(ii)).x + "," + ((Point)v.get(ii)).y + "; "); }

}

if (flag ==1) flag = 2;

else flag = 1;

}

public void paintComponent(Graphics g)

{

super.paintComponents(g);

g.setColor(Color.RED);

g.translate(0, 200);

g.drawLine(0, 240, 640, 240);

g.drawLine(0, 0, 0, 480);

drawPoint(g);

}

}

public static void main(String args[])

{

DrawFigurs3 dd=new DrawFigurs3();

}

}

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Mouse extends JFrame{

private JPanel pan = new MyPanel();

Mouse()

{

pan.setBounds(0, 0, 640, 480);

this.add(pan);

this.setLayout(null);

this.setBounds(500, 500, 640, 480);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args)

{

new Mouse();

}

public class MyPanel extends JPanel

{

MyPanel()

{

this.addMouseMotionListener(new MyMouseMoution()); }

}

public class MyMouseMoution extends MouseMotionAdapter {

public void mouseMoved(MouseEvent e)

{

clear();

drawString(e.getX(),e.getY());

}

}

public void drawString(int x, int y)

{

Graphics g = this.getGraphics();

g.drawString("(" + x + "," + y + ")", x+30, y+40);

}

public void clear()

{

Graphics g = this.getGraphics();

g.clearRect(0, 0, 640, 480);

}

}

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.awt.print.*;

public class MyMouseKeyboard extends JFrame{

private JPanel pan = new MyPanel();

private Point p = new Point();

static int charXLocation=0;

MyMouseKeyboard()

{

pan.setBounds(0, 0, 640, 480);

this.add(pan);

this.setLayout(null);

this.setResizable(false);

this.setBounds(500, 500, 640, 480);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); }

public static void main(String[] args)

{

new MyMouseKeyboard();

}

} public class MyPanel extends JPanel{ MyPanel() { this.setFocusable(true); this.addMouseListener(new MyMouse()); this.addKeyListener(new MyKey()); } } public class MyMouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { charXLocation = e.getX(); p.x = e.getX(); p.y = e.getY(); int clicktimes = e.getClickCount(); if (clicktimes == 2) { clear(); clicktimes = 0; } } } public void clear() { Graphics g = this.getGraphics(); g.clearRect(0, 0, 640, 480); } public class MyKey extends KeyAdapter { public void keyPressed(KeyEvent e) { char ch = e.getKeyChar(); drawChar(ch); } } public void drawChar(char c) { Graphics g = this.getGraphics(); g.setColor(Color.blue); g.drawString(String.valueOf(c), p.x+charXLocation-105, p.y+30); charXLocation += 8; }

三. 实验总结:

说明源程序的结构

说明用到了那些系统包, 类, 函数

说明调试过程出现什么错误,如何进行改正

1) 在图形界面中绘制二维的坐标系统,其中x轴位于绘图区正中,y轴位于绘图区的最左

边。

Java程序设计实验报告2

2) 根据下面试验数据,在1)题的坐标系统中绘制数据点,每个数据点用一个小叉号表示,

并在叉号边以(x,y)的格式标示出数据点的坐标数值,最后用折线连接相邻的数据点。 数据点序号 x坐标 y坐标

1 0 20

2 50 -30

3 100 16

4 150 126

5 200 164

Java程序设计实验报告2

3) 在2)题的基础上设计界面接收用户输入的实验数据,绘制数据点和实验曲线。

Java程序设计实验报告2

4) 编写程序跟踪用户的鼠标,并时刻把鼠标的位置显示出来。

Java程序设计实验报告2

5) 编写图形界面的Java程序响应鼠标单击和键盘事件,从鼠标单击的位置开始横向显示

用户键入的字符,若鼠标双击则清空屏幕。

Java程序设计实验报告2

相关推荐