java实验报告实验六Java图形用户界面
- 格式:doc
- 大小:560.50 KB
- 文档页数:28
图形界面(GUI)程序设计一、课题内容和要求内容:设计和编写一个用于将人民币转换为等值的美元的程序,界面要求可以输入人民币的金额并可以得到转换后的结果。
要求:学习和理解JAVA SWING中的容器,部件,布局管理器和部件事件处理方法。
通过编写和调试程序,掌握JAVA图形界面程序设计的基本方法。
二、设计思路分析class RMBtoDollar:主类,调用主函数。
class change:设置界面,并通过界面上的事件触发实现汇率转换。
三、概要设计public class RMBtoDollar{public static void main(String[] args);}class change extends JFrame implements MouseListener { JLabel l1, l2,l3;JTextField tf1, tf2;JButton b;double RMB, Dollar;public change();public void mousePressed(MouseEvent e);public void mouseClicked(MouseEvent e);public void mouseEntered(MouseEvent e);public void mouseExited(MouseEvent e);public void mouseReleaseed(MouseEvent e);//鼠标释放时进行转换}四、详细设计import java.awt.*;import java.awt.event.*;import javax.swing.*;public class RMBtoDollar {public static void main(String[] args) {new change();}}class change extends JFrame implements MouseListener { JLabel l1, l2,l3;JTextField tf1, tf2;JButton b;double RMB, Dollar;public change() {//添加组件和设置布局l1 = new JLabel("人民币/元:");l2 = new JLabel("美元/dollar:");l3=new JLabel("(汇率:6.17)");tf1 = new JTextField(12);tf2 = new JTextField(12);b = new JButton("转换");add(l1);add(tf1);add(l2);add(tf2);add(l3);add(b);setLayout(new FlowLayout(FlowLayout.CENTER));setTitle("人民币转换为美元");setBounds(300, 300, 300, 150);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);b.addMouseListener(this);//鼠标事件监视器}public void mousePressed(MouseEvent e) {if (tf2.getText() != null) {// tf2 test 默认初始设为空tf2.setText("");}}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mouseReleased(MouseEvent e) {if (tf1.getText() != null) {// 检查tf1 test 是否为空try {// 取异常RMB = Double.parseDouble(tf1.getText());// 字符转为double型Dollar = RMB / 6.17;//转换tf2.setText("" + Dollar);// 显示} catch (Exception ex) {tf1.setText("");// 如果输入不是数字,设为空}}}}五、测试数据及其结果分析1正常输入:2输入字符串不能转为double型时清空输入框:3结果:输出正常。
西安邮电大学(计算机学院)课内实验报告实验名称:图形用户界面专业名称:计算机科学与技术班级:计科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. 给上述程序增加测试乘、除的功能。
实验六图形界面实验日期:2016 年6 月12 日班级:软件1401 学号(后四位):__0127_______ 姓名:_程瑞强_______ 成绩:成绩:一.实验目的1.掌握图形用户界面的设计方法2.掌握常用的构建用户界面的组件的用法3.掌握事件操作的原理4.能够对所设计的用户界面进行事件处理5.能够应用运算符解决实际小问题6.进一步熟悉Java的面向对象的编程思想二.实验题目(前2题任选1题,第3,4题任选1题)1.采用图形界面实现两个内容的交换,图形界面如下图1所示所示:图1 内容交换代码如下:package TestChange;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 TestChange extends JFrame implements ActionListener{private static final long serialVersionUID = -3684503858019589006L;JPanel panel;JTextField tt1;JTextField tt2;JButton button;public TestChange(){this.setTitle("TestChange");this.setSize(355, 85);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);panel = new JPanel();tt1 = new JTextField(10);tt2 = new JTextField(10);button = new JButton("Change");button.addActionListener(this);panel.add(tt1);panel.add(tt2);panel.add(button);this.add(panel, BorderLayout.NORTH);this.setResizable(false);this.setVisible(true);}public static void main(String[] str){new TestChange();}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource() == button){String temp = tt1.getText();tt1.setText(tt2.getText());tt2.setText(temp);}}}2. 采用图形界面设计如下图2所示的界面。
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!"。
信息工程学院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图形实验报告篇一: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图形用户界面。
JA V A实验报告实验目的:学习组件布局和组件事件的处理。
实验要求:编写程序,实现成绩的查询和排序。
具体要求为:①选择合适的布局管理设计美观的界面,包含成绩输入、成绩查询、成绩排序功能。
②成绩输入:从界面上输入学生的学号和成绩,点击“确认”按钮进行保存。
③成绩查询:输入学生的学号,点击“查询”按钮,显示该学生成绩。
④成绩排序:点击“排序”按钮,将按成绩从高到低显示学生的学号和成绩。
实验步骤:①定义一个类表示学生,包含学号和成绩两个域;②用java提供的V ector或Arrays类(也可使用自定义类)存储输入的学生信息,使用java 提供的方法或使用自定义方法实现学生信息的查询和排序;③界面设计,可考虑以下的界面设计策略:ⅰ在界面上放置成绩查询、成绩输入、成绩排序三个按钮和一个文本区:按下成绩输入按钮则弹出一个窗口,该窗口上有学号输入和成绩输入文本框,输入完毕关闭窗口后输入的学生信息出现在文本区中;按下成绩查询按钮弹出一个与成绩输入类似的窗口,在学号文本框中输入学号后,在成绩文本框中立即显示成绩;按下排序按钮,在文本区中显示排序后的结果。
ii 可在界面上用三个Panel分别实现查询、输入、排序功能,分别在各Panel上放置按钮、文本框、文本区等控件。
此步中,要求学生会使用各种布局管理器设计美观的界面。
④为程序添加事件处理机制。
ⅰ为查询、输入、排序按钮添加事件处理;ⅱ为学号、成绩输入文本框添加事件处理;ⅲ为窗口的关闭添加事件处理。
import java.awt.*;import java.awt.event.*;public class jj extends Frame implements ActionListener {private Button add=new Button("添加");private Button search=new Button("查询");private Button up=new Button("排序");private Label snLabel=new Label("学号");private Label gLabel=new Label("成绩");private List list=new List();private TextField taskInput =new TextField();public class WindowsCloser extends WindowAdapter{public void WindowClosing (WindowEvent we){System.exit(0);}}private viod setup(){Panel buttons=new Panel();buttons.setLayout(new FlowLayout());buttons.add(add);buttons.add(search);buttons.add(up);Panel input =new Panel();input.setLayout(new BorderLayout());input.add("West",snLabel);input.add("Center",taskInput);input.add("west",snLabel);input.add("Center",taskInput);Panel top=new Panel();top.setLayout(new GridLayout(2,1));}public TaskList(){}public void actionperformed(ActionEvent ae) {}public static void main (String args[]){List t1 =new List();}}。
信息工程学院Java程序设计实习报告JAVA图形用户界面实验六Java图形用户界面1.实验目的(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握Java事件处理机制。
2.实验内容实验题1 编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:实验报告的内容与格式按任课教师的要求书写。
加法:主要代码:private void initComponents() {jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jButton5 = new javax.swing.JButton();jButton6 = new javax.swing.JButton();jButton7 = new javax.swing.JButton();jButton8 = new javax.swing.JButton();jButton9 = new javax.swing.JButton();jButton10 = new javax.swing.JButton();jButton11 = new javax.swing.JButton();jButton12 = new javax.swing.JButton();jButton13 = new javax.swing.JButton();jButton14 = new javax.swing.JButton();jButton15 = new javax.swing.JButton();jTextField1 = new javax.swing.JTextField();setStub(null);jButton1.setText("3");jButton1.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt);}});jButton2.setText("1");jButton2.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton2ActionPerformed(evt);}});jButton3.setText("5");jButton3.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton3ActionPerformed(evt);}});jButton4.setText("2");jButton4.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton4ActionPerformed(evt);}});jButton5.setText("6");jButton5.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(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 voidactionPerformed(java.awt.event.ActionEvent evt) {jButton7ActionPerformed(evt);}});jButton8.setText("7");jButton8.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(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(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton10ActionPerformed(evt);}});jButton11.setText("\u00f7");jButton11.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton11ActionPerformed(evt);}});jButton12.setText("\u00d7");jButton12.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton12ActionPerformed(evt);}});jButton13.setText("-");jButton13.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(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布局。
窗口的中心添加一个Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter 的中放置49个标签,用来显示日历。
窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击previousMonth按钮,可以显示当前月的上一个月的日历。
窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout 布局,pSouth中放置一个标签用来显示一些信息。
运行结果如图所示。
图3.8 运行结果图[基本要求] 编写完整程序。
运行结果:主要代码:private JLabel[] buttonDay = new JLabel[42]; private JButton[] buttonWeek = new JButton[7]; private JLabel labelMonth = new JLabel();private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton(); private JPanel pCenter=new JPanel();private JPanel pNorth=new JPanel();private JPanel pSouth=new JPanel();private JLabel time=new JLabel();public Calender() {super("Calender");setBounds(250, 200, 600, 500);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);buttonLastMonth.setText("上月");buttonLastMonth.addActionListener(this);pNorth.add(buttonLastMonth);buttonNextMonth.setText("下月");buttonNextMonth.addActionListener(this);pNorth.add(buttonNextMonth);getContentPane().add(pNorth,BorderLayout.NORTH);getContentPane().add(pCenter,BorderLayout.CENTER);pCenter.setLayout(new GridLayout(7,7));for (int i = 0; i < 7; i++) {buttonWeek[i] = new JButton();buttonWeek[i].setText(stringWeekCn[i]);pCenter.add(buttonWeek[i]);}for (int i = 0; i < 42; i++) {buttonDay[i] = new JLabel();buttonDay[i].setText(" ");pCenter.add(buttonDay[i]);}实验题3 实现如图3.9所示的布局方式功能:前两个文本框输入整型数据。