Java图形用户界面实验报告

合集下载

Java图形用户界面实验报告

Java图形用户界面实验报告

西安邮电大学(计算机学院)课内实验报告实验名称:图形用户界面专业名称:计算机科学与技术班级:计科1405班学生姓名:高宏伟学号:04141152指导教师:刘霞林实验日期:2016.11.24一、实验目的了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。

二、实验要求1. 掌握使用布局管理器对组件进行管理的方法。

2. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。

3. 掌握编写独立运行的窗口界面的方法。

4. 掌握组件的使用方法。

5. 了解对话框的使用方法。

三、实验内容(一)算术测试。

✧实验要求:编写一个算术测试小软件,用来训练小学生的算术能力。

程序由3个类组成,其中Teacher类对象负责给出算术题目,并判断回答者的答案是否正确;ComputerFrame类对象负责为算术题目提供视图,比如用户可以通过ComputerFrame类对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MainClass是软件的主类。

✧程序模板:Teacher.javapublic class Teacher{ int numberOne,numberTwo;String operator="";boolean right;public int giveNumberOne(int n){ numberOne=(int)(Math.random()*n)+1;return numberOne;}public int giveNumberT wo(int n){ numberTwo=(int)(Math.random()*n)+1;return numberTwo;}public String giveOperator(){ double d=Math.random();if(d>=0.5)operator="+";elseoperator="-";return operator;}public boolean getRight(int answer){ if(operator.equals("+")){ if(answer==numberOne+numberTwo)right=true;elseright=false;}else if(operator.equals("-")){ if(answer==numberOne-numberTwo)right=true;elseright=false;}return right;}}ComputerFrame.javaimport java.awt.*;import java.awt.event.*;public class ComputerFrame extends Frame implements ActionListener { TextField textOne,textTwo,textResult;Button getProblem,giveAnwser;Label operatorLabel,message;Teacher teacher;ComputerFrame(String s){ super(s);teacher=new Teacher();setLayout(new FlowLayout());textOne=【代码1】 //创建textOne,其可见字符长是10textTwo=【代码2】 //创建textTwo,其可见字符长是10textResult=【代码3】 //创建textResult,其可见字符长是10operatorLabel=new Label("+");message=new Label("你还没有回答呢");getProblem=new Button("获取题目");giveAnwser=new Button("确认答案");add(getProblem);add(textOne);add(operatorLabel);add(textTwo);add(new Label("="));add(textResult);add(giveAnwser);add(message);textResult.requestFocus();textOne.setEditable(false);textTwo.setEditable(false);【代码4】//将当前窗口注册为getProblem的ActionEvent事件监视器【代码5】//将当前窗口注册为giveAnwser的ActionEvent事件监视器【代码6】//将当前窗口注册为textResult的ActionEvent事件监视器 setBounds(100,100,450,100);setVisible(true);validate();addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0);}});}public void actionPerformed(ActionEvent e){ if(【代码7】) //判断事件源是否是getProblem{ int number1=teacher.giveNumberOne(100);int number2=teacher.giveNumberTwo(100);String operator=teacher.givetOperator();textOne.setText(""+number1);textTwo.setText(""+number2);operatorLabel.setText(operator);message.setText("请回答");textResult.setText(null);}if(【代码8】) //判断事件源是否是giveAnwser{ String answer=textResult.getText();try{int result=Integer.parseInt(answer);if(teacher.getRight(result)==true){ message.setText("你回答正确");}else{ message.setText("你回答错误");}}catch(NumberFormatException ex){ message.setText("请输入数字字符");}}textResult.requestFocus();validate();}}MainClass.javapublic class MainClass{ public static void main(String args[]){ ComputerFrame frame;frame=【代码9】//创建窗口,其标题为:算术测试}}✧实验后的练习:1. 给上述程序增加测试乘、除的功能。

南邮Java实验报告1-综合图形界面程序设计

南邮Java实验报告1-综合图形界面程序设计

南邮Java实验报告1-综合图形界面程序设计英文回答:Hello, I'm glad to help you with your Java lab report on comprehensive graphical user interface programming.In this report, I will provide a detailed description of the steps I took to create a graphical user interface (GUI) for a simple application. I will also include screenshots of the GUI and discuss the challenges I faced and how I overcame them.Step 1: Create a new Java project。

The first step was to create a new Java project in my preferred development environment. I used IntelliJ IDEA, but you can use any IDE that you are comfortable with.Step 2: Design the GUI。

Once I had created a new project, I began designing the GUI. I sketched out a rough draft on paper and then usedthe Swing library to create the actual GUI.The Swing library provides a set of components that can be used to create GUIs. These components include buttons, text fields, labels, and menus.Step 3: Add functionality to the GUI。

java图形界面实验报告1

java图形界面实验报告1

河南工业大学实验报告专业班级: 计科F1401 学号: 姓名:实验单元八【实验目的】1.掌握程序设计方法2.掌握程序设计方法3.掌握程序设计方法4.掌握程序设计方法5、掌握使用????程序设计方法。

【实验环境】安装了jdk软件的PC机。

【实验内容】第18章、图形界面。

【程序功能内容说明】设置标签的显示字体、大小背景及颜色。

【实验程序原码】import java.awt.Dimension ;import java.awt.Color ;import java.awt.Font ;import java.awt.Point ;import javax.swing.JLabel ;import javax.swing.JFrame ;public class JLabelDemo02{public static void main(String args[]){JFrame frame = new JFrame("Welcome To MLDN") ;JLabel lab = new JLabel("MLDN",JLabel.CENTER) ; // 实例化标签对象Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,28) ;lab.setFont(fnt) ;frame.add(lab) ; // 将组件件入到面板之中Dimension dim = new Dimension() ;frame.setBackground(Color.WHITE) ;//设置窗体的背景颜色dim.setSize(200,70) ;frame.setSize(dim) ;Point point = new Point(300,200) ; // 设置坐标frame.setLocation(point) ;frame.setVisible(true) ;}};【实验结果】【该程序关键技术说明】JFrame作为基本容器用于创建窗口。

java实验六-图形用户界面

java实验六-图形用户界面
getContentPane().add(text);
getContentPane().add(text1);
ButtonHandler btnh=newButtonHandler();
text.addActionListener(btnh);
//getContentPane().add(textArea);
text.setBounds(300, 60, 200, 30);
//text.setColumns(31);
getContentPane().add(text);
text1.setEditable(false);
text1.setLineWrap(true);
text1.setBounds(300, 260, 200, 300);
}
}
public class TestJFrame {
public static void main(String[] args) {
new MyFrame();
}
}
2.在JApplet中加入1个文本框,1个文本区,每次在文本框中输入文本,回车后将文本添加到文本区的最后一行。
package实验六;
importjava.awt.event.ActionEvent;
@Override
public void actionPerformed(ActionEvent e) {
// TODO自动生成的方法存根
if (e.getActionCommand()=="按钮1")
label.setText("按钮1:");
else label.setText("按钮2:"); }

java图形用户界面实验报告

java图形用户界面实验报告

java图形用户界面实验报告Java图形用户界面实验报告一、引言图形用户界面(Graphical User Interface,简称GUI)是现代软件开发中不可或缺的一部分。

通过GUI,用户能够直观地与程序进行交互,提高了软件的易用性和用户体验。

本实验旨在通过使用Java编程语言,实现一个简单的GUI应用程序,并介绍相关的技术和方法。

二、实验目标本实验的目标是设计并实现一个简单的GUI应用程序,要求具备以下功能:1. 显示一个窗口,并在窗口中心显示一个标签;2. 在窗口中添加一个按钮,并实现按钮的点击事件;3. 当按钮被点击时,标签的文本发生变化。

三、实验过程1. 环境准备在开始实验之前,需要确保已经安装了Java开发环境(JDK)和集成开发环境(IDE),例如Eclipse或IntelliJ IDEA。

2. 创建GUI应用程序首先,创建一个Java项目,并创建一个名为"GUIApplication"的类。

在该类中,引入Java的GUI库,并继承JFrame类,实现一个窗口。

3. 设计窗口布局在窗口的构造方法中,设置窗口的标题、大小和关闭方式。

然后,创建一个JPanel对象,并将其添加到窗口中。

在JPanel中,使用布局管理器(例如FlowLayout或GridBagLayout)来安排组件的位置和大小。

4. 添加标签和按钮在JPanel中,创建一个JLabel对象,并设置其文本和位置。

然后,创建一个JButton对象,并设置其文本和点击事件。

在点击事件中,通过修改JLabel的文本来实现标签内容的变化。

5. 运行程序完成以上步骤后,编译并运行程序。

将会显示一个窗口,其中包含一个标签和一个按钮。

当按钮被点击时,标签的文本会发生变化。

四、实验结果经过以上步骤,成功实现了一个简单的GUI应用程序。

运行程序后,窗口显示如下图所示:(插入程序运行截图)在窗口中心显示了一个标签,标签的文本为"Hello, GUI!"。

Java图形界面实验报告

Java图形界面实验报告

实验报告2013学年第 1 学期任课老师:课程名称Java语言与系统设计班级学号姓名实验名称实验4 图形用户界面(GUI)实验时间实验环境PC/windows2000/2003/XP/Jcreator Pro/JBuild/JDK Eclipse/。

实验目的和内容要求实验4 图形用户界面(GUI)1.实验目的掌握布局管理器的使用,掌握JFrame框架、JButton按钮、JLabel标签、JTextField文本框、对话框等组件的使用及其事件处理。

2.实验内容(1)编程:创建有一个文本框和三个按钮的小程序。

当按下每个按钮时,使不同的文字显示在文本框中。

(2)编程:创建一用户登录界面,接受用户输入的帐号和密码,给三次输入机会。

实验过程记录(学生写出实验步骤及中间的结果与现象,在实验中做了什么,怎么做,发生的现象和中间结果)实验步骤:1.建立一个类继承JFrame,然后初始化界面,给三个按钮添加监听器,点击之后进行判断。

然后根据接收信息是不同的文字出现在文本框中。

2.登录界面共有5个键,登录提示有只能输入3次密码,在登录按钮上添加监听器,每次点击之后都判断,不成功就显示失败,成功就弹出成功弹窗;实验结果分析与总结1、程序运行结果(请提供所完成的各道题运行结果界面截图):2、实验过程中的发现与收获,未解决或需进一步解决的问题:实现的比较简单,界面做的非常简陋,但还算实现了基本功能。

指导老师评阅意见指导老师:***填写内容时,可把表格扩大。

附:实验源程序代码//第一题package com.miao;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class Example extends JFrame implements ActionListener { private static final long serialVersionUID = 1L;private JPanel jPanel;private JButton jButton1,jButton2,jButton3;private JTextField tf1;public void test(String title) {init();}private void init() {jPanel=new JPanel();jPanel.setLayout(new FlowLayout());tf1=new JTextField(20);jButton1=new JButton("按钮1");jButton2=new JButton("按钮2");jButton3=new JButton("按钮3");jButton1.addActionListener(this);jButton2.addActionListener(this);jButton3.addActionListener(this);jPanel.add(tf1);jPanel.add(jButton1);jPanel.add(jButton2);jPanel.add(jButton3);this.add(jPanel);this.setSize(300,100);this.setResizable(false);this.setTitle("Show");this.setVisible(true);this.addWindowListener(new WindowAdapter() {public void windowClosing(final WindowEvent e) {System.exit(0);}});}public void actionPerformed(ActionEvent e) {if(e.getSource().equals(jButton1)){tf1.setText("lalalalalala");}if(e.getSource().equals(jButton2)){tf1.setText("heihei");}if(e.getSource().equals(jButton3)){tf1.setText("gaga");}}public static void main(String[] args){String s="文字转化";Example test=new Example();test.test(s);}}//第二题,登录package com.miao;import java.awt.HeadlessException;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;@SuppressWarnings("serial")public class Test extends JFrame {JLabel lbl1 = new JLabel("用户名:");JLabel lbl2 = new JLabel("密码:");JTextField txt = new JTextField("admin",20);JPasswordField pwd = new JPasswordField(20);JButton btn = new JButton("登录");JPanel pnl = new JPanel();private int error = 0;public void text() throws HeadlessException {init();}private void init() {this.setResizable(false);this.setTitle("测试");pnl.add(lbl1);pnl.add(txt);pnl.add(lbl2);pnl.add(pwd);pnl.add(btn);this.getContentPane().add(pnl);btn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (error<3&&"123".equals(new String(pwd.getPassword()))){// pnl.removeAll();// JLabel lbl3 = new JLabel();// pnl.add(lbl3);JOptionPane.showMessageDialog(null,"登陆成功!");}else if(error < 3){JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");error++;}else if(error >=3){JOptionPane.showMessageDialog(null,"对不起,您不是合法用户"); // txt.setEnabled(false);// pwd.setEnabled(false);// btn.setEnabled(false);error++;}}});}public static void main(String[] args) {// String str="测试";Test frm = new Test();frm.text();frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frm.setBounds(100, 100, 300, 120);frm.setVisible(true);frm.setLocationRelativeTo(null);// frm.text("测试");}}。

java实验报告实验六Java图形用户界面

java实验报告实验六Java图形用户界面

信息工程学院1Java 程序设计实习报告JAVA 图形用户界面实验六Java 图形用户界面1. 实验目的(1) 掌握图形用户界面基本组件。

(2) 了解如何使用布局管理器对组件进行管理。

(3) 掌握Java 事件处理机制。

2. 实验内容实验题1编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9) , 4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算 公式和结果显示在文本框中。

运行结果:实验报告的内容与格式按任课教师的要求书写。

,小程序亘看器:paclcagel.Calculator.dass口 I 回加法:主要代码:private void in itComp onen ts() {jButt on1 = :new javax.swing.JButton();jButt on2 = :new javax.swing.JButton();jButt on3 = :new javax.swing.JButton();jButt on4 = :new javax.swing.JButton();jButt on5 = :new javax.swing.JButton();jButt on6 = :new javax.swing.JButton();jButt on7 = :new javax.swing.JButton();jButt on8 = :new javax.swing.JButton();jButt on9 = :new javax.swing.JButton();jButto n10 =new javax.swing.JButton();jButto n11 =new javax.swing.JButton();jButto n12 =new javax.swing.JButton();jButto n13 =new javax.swing.JButton();jButto n14 =new javax.swing.JButton();jButto n15 =new javax.swing.JButton();jTextField1 =new javax.swing.JTextField();setStub( null ); jButton1 .setText( "3" );jButton1 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt); }});jButton2 .setText( "1" );jButton2 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton2ActionPerformed(evt); }});jButton3 .setText( "5" );jButton3 .addActionListener( new java.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt);}});jButton4 .setText( "2" );jButton4 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton4ActionPerformed(evt); }});jButton5 .setText( "6" );jButton5 .addActionListener(newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton5ActionPerformed(evt); }});jButton6 .setText( "8" );jButton6 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton6ActionPerformed(evt); }});jButton7 .setText( "4" );jButton7 .addActionListener( newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton7ActionPerformed(evt); }});jButton8 .setText( "7" );jButton8 .addActionListener( newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton8ActionPerformed(evt); }});jButton9 .setText( "0" );jButton9 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton9ActionPerformed(evt); }});jButton10 .setText( "9" );jButton10 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt);}});jButton11 .setText( "\u00f7" );jButton11 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton11ActionPerformed(evt); }});jButton12 .setText( "\u00d7" );jButton12 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton12ActionPerformed(evt); }});jButton13 .setText( "-" );jButton13 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton13ActionPerformed(evt);});jButton14 .setText( "+" );jButton14 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton14ActionPerformed(evt);}});jButton15 .setText( "=" );jButton15 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton15ActionPerformed(evt);}});实验题2 编写一个程序,有一个窗口,该窗口为BorderLayout 布局。

java图形实验报告doc

java图形实验报告doc

java图形实验报告篇一:java实验报告实验六Java图形用户界面信息工程学院Java程序设计实习报告JAVA图形用户界面实验六Java图形用户界面1.实验目的(1)掌握图形用户界面基本组件。

(2)了解如何使用布局管理器对组件进行管理。

(3)掌握Java事件处理机制。

2.实验内容实验题1 编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。

运行结果:实验报告的内容与格式按任课教师的要求书写。

加法:主要代码:private void initComponents() {setStub(null); jButton1 = new ; jButton2 = new ; jButton3 = new ; jButton4 = new ; jButton5 = new ;jButton6 = new ; jButton7 = new ; jButton8 = new ; jButton9 = new ; jButton10 = new ; jButton11 = new ; jButton12 = new ; jButton13 = new ; jButton14 = new ; jButton15 = new ; jTextField1 = new ;jButton1.setText("3"); jButton1.addActionListener(new {public voidactionPerformed( evt) {jButton2.setText("1"); jButton2.addActionListener(new}); } jButton1ActionPerformed(evt); {public voidactionPerformed( evt) {jButton3.setText("5"); jButton3.addActionListener(new}); } jButton2ActionPerformed(evt); {public voidactionPerformed( evt) {jButton4.setText("2"); jButton4.addActionListener(new}); } jButton3ActionPerformed(evt); {public voidactionPerformed( evt) {jButton5.setText("6");}); } jButton4ActionPerformed(evt);jButton5.addActionListener(new {public voidactionPerformed( evt) {jButton6.setText("8"); jButton6.addActionListener(new}); } jButton5ActionPerformed(evt); {public voidactionPerformed( evt) {jButton7.setText("4"); jButton7.addActionListener(new}); } jButton6ActionPerformed(evt); {public voidactionPerformed( evt) {jButton8.setText("7"); jButton8.addActionListener(new}); } jButton7ActionPerformed(evt); {public voidactionPerformed( evt) {jButton9.setText("0");jButton9.addActionListener(new}); } jButton8ActionPerformed(evt); {public voidactionPerformed( evt) {jButton10.setText("9"); jButton10.addActionListener(new}); } jButton9ActionPerformed(evt); {public voidactionPerformed( evt) {jButton11.setText("\u00f7"); jButton11.addActionListener(new}); } jButton10ActionPerformed(evt); {public voidactionPerformed( evt) {jButton12.setText("\u00d7"); jButton12.addActionListener(new}); } jButton11ActionPerformed(evt); {public voidactionPerformed( evt) {jButton13.setText("-"); jButton13.addActionListener(new}); } jButton12ActionPerformed(evt); {public voidactionPerformed( evt) {篇二:JAVA实验报告附件2:实验报告封皮20 —学年第学期课程实验报告学院:计算机科学技术专业:软件工程班级:姓名:学号:任课教师:王薇实验日期:XX年 11 月 02 日-1--2-实验日期:XX年 11 月 06 日-3--4-篇三:java图形用户界面实验报告南京工程学院实验报告课程名称 JAVA基础实验项目名称图形用户界面设计实验学生班级实验学生姓名学号同组学生姓名无实验时间 XX年11月实验地点实验成绩评定指导教师签字年月日一、实验目的和要求1.目的:掌握java AWT及Swing组件的使用方法,包括窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型,掌握不同组件,不同事件的事件处理方法,设计出能够响应事件的java图形用户界面。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

西安邮电大学(计算机学院)课内实验报告实验名称:图形用户界面专业名称:计算机科学与技术班级:计科1405班学生姓名:高宏伟学号:04141152指导教师:刘霞林实验日期:一、实验目的了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。

二、实验要求1. 掌握使用布局管理器对组件进行管理的方法。

2. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。

3. 掌握编写独立运行的窗口界面的方法。

4. 掌握组件的使用方法。

5. 了解对话框的使用方法。

三、实验内容(一)算术测试。

实验要求:编写一个算术测试小软件,用来训练小学生的算术能力。

程序由3个类组成,其中Teacher类对象负责给出算术题目,并判断回答者的答案是否正确;ComputerFrame类对象负责为算术题目提供视图,比如用户可以通过ComputerFrame类对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MainClass是软件的主类。

程序模板:public class Teacher{ int numberOne,numberTwo;String operator="";boolean right;public int giveNumberOne(int n){ numberOne=(int)()*n)+1;return numberOne;}public int giveNumberT wo(int n){ numberTwo=(int)()*n)+1;return numberTwo;}public String giveOperator(){ double d=();if(d>=operator="+";elseoperator="-";return operator;}public boolean getRight(int answer){ if("+")){ if(answer==numberOne+numberTwo)right=true;elseright=false;}else if("-")){ if(answer==numberOne-numberTwo)right=true;elseright=false;}return right;}}import .*;import class ComputerFrame extends Frame implements ActionListener{ TextField textOne,textTwo,textResult;Button getProblem,giveAnwser;Label operatorLabel,message;Teacher teacher;ComputerFrame(String s){ super(s);teacher=new Teacher();setLayout(new FlowLayout());textOne=【代码1】给上述程序增加测试乘、除的功能。

(二)信号灯。

实验要求:编写一个带有窗口的应用程序。

在窗口的北面添加一个下拉列表,该下拉列表有“”、“”和“”三个选项。

在窗口的中心添加一个画布,当用户在下拉列表选择某项后,画布上绘制相应的信号灯。

程序模板:import .*;public class SignalCanvas extends Canvas{ int red,green,yellow,x,y,r;SignalCanvas(){ setBackground;}public void setRed(int r){ red=r;}public void setGreen(int g){ green=g;}public void setYellow(int y){ yellow=y;}public void setPosition(int x,int y){ =x;=y;}public void setRadius(int r){ =r;}public void paint(Graphics g){ if(red==1){ ;}else if(green==1){ ;}else if(yellow==1){ ;}(x,y,2*r,2*r);}}import .*;import .*;import class SignalFrame extends Frame implements ItemListener { Choice choice;SignalCanvas signal=null;String itemRed="红灯",itemYellow="黄灯",itemGreen="绿灯";public SignalFrame(){ choice=【代码1】idth;int h=().height;int m=(w,h);(m/6);if(itemRed)){ if(signal!=null){ (1);(0);(0);(w/3,0);();}}else if(itemYellow)){ if(signal!=null){ (0);(1);(0);(w/3,h/3);();}}else if(itemGreen)){ if(signal!=null){ (0);(0);(1);(w/3,2*h/3);();}}}}public class SignalMainClass{ public static void main(String args[]){ SignalFrame frame;frame=new SignalFrame() ;("信号灯");}}实验后的练习:1. 改进上述程序,在下拉列表中增加“熄灭所有灯”选项,当选中该项时,画布上绘制一个半径为0,位置是(0,0)的圆。

(三)布局与日历。

实验要求:编写一个应用程序,有一个窗口,该窗口为BorderLayout布局。

窗口的中心添加一个Panel容器:pCenter,pCenter的布局是7行7列的GriderLayout布局,pCenter中放置49个标签,用来显示日历。

窗口的北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth,单击nextMonth按钮,可以显示当前月的下一月的日历;单击previousMonth按钮,可以显示当前月的上一月的日历。

窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息。

程序模板:import class CalendarBean{String day[];int year=2005,month=0;public void setYear(int year){ =year;}public int getYear(){ return year;}public void setMonth(int month){ =month;}public int getMonth(){ return month;}public String[] getCalendar(){ String a[]=new String[42];Calendar 日历=();日历.set(year,month-1,1);int 星期几=日历.get-1;int day=0;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { day=31;}if(month==4||month==6||month==9||month==11){ day=30;}if(month==2){ if(((year%4==0)&&(year%100!=0))||(year%400==0)){ day=29;}else{ day=28;}}for(int i=星期几,n=1;i<星期几+day;i++){a[i]=(n) ;n++;}return a;}}import .*;import .*;import .*;public class CalendarFrame extends Frame implements ActionListener { Label labelDay[]=new Label[42];Button titleName[]=new Button[7];String name[]={"日","一","二","三", "四","五","六"};Button nextMonth,previousMonth;int year=2006,month=10;CalendarBean calendar;Label showMessage=new Label("",;public CalendarFrame(){ Panel pCenter=new Panel();【代码1】etText(day[i]);}nextMonth=new Button("下月");previousMonth=new Button("上月");(this);(this);Panel pNorth=new Panel(),pSouth=new Panel();(previousMonth);(nextMonth);(showMessage);("日历:"+()+"年"+ ()+"月" );ScrollPane scrollPane=new ScrollPane();(pCenter);【代码4】etText(day[i]);}}else if()==previousMonth){ month=month-1;if(month<1)month=12;(month);String day[]=();for(int i=0;i<42;i++){ labelDay[i].setText(day[i]);}}("日历:"+()+"年"+()+"月" );}}public class CalendarMainClass{ public static void main(String args[]){ CalendarFrame frame=new CalendarFrame();(100,100,360,300);(true);();(new { public void windowClosing e){ (0);}});}}实验后的练习:1.请在CalendarFrame类中增加一个TextField文本框,用户可以通过在文本框中输入年份来修改calendar对象的int成员year。

相关文档
最新文档