??作为上学年 “Java硬核程序设计实验详解” 系列补充??
目录
??|?一、实验目的
??|?二、实验内容
?1. 题目:创建一个具有关闭功能的空白窗口(如图5-1)
?2. 题目:在窗体界面上添加相应组件,程序运行结果如图5-2
?3. 题目: 编写窗体程序SY5_2_CalMoney,如图5-3所示
?5. 题目:编写一个算术测试小软件,如图5-6所示
??|?三、实验源代码
??1. 源代码:
??2. 源代码:
??3. 源代码:
??4. 源代码:
??5. 源代码
??|?四、实验总结
??|?一、实验目的
1.理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。
2.掌握编写独立运行的窗口界面的方法。
3.了解Java Swing 组件的使用方法。
4.了解对话框的使用方法
??|?二、实验内容
?1. 题目:创建一个具有关闭功能的空白窗口(如图5-1)
(知识点:在Eclipse下创建窗体,事件处理)
import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; public class SY5_1_JFrame 【代码1】{//继承JFrame类 public static void main(String[] args) { new SY5_1_JFrame(); } ????//构造函数 public SY5_1_JFrame() { ???【代码2】;// 创建标题为“JFrame窗口”窗口对象 ???【代码3】;// 设置窗口大小为400 x 200 ???【代码4】;// 设置窗口是可视的 //为窗口添加窗口事件适配用来关闭窗体,这里大家暂时照用就是 addWindowListener(new WindowAdapter() {器 public void windowClosing(WindowEvent e) {// 关闭窗口事件的方法 System.exit(0); } }); } } | 图5-1 JFrame窗口 | 图5-2 带组件的窗口 |
?2. 题目:在窗体界面上添加相应组件,程序运行结果如图5-2
实验结果(1,2):
?3. 题目: 编写窗体程序SY5_2_CalMoney,如图5-3所示
要求:
界面上设置三个文本框,第一个文本框给用户输入商品单价,第二个则是给用户输入商品数量,第三个用于显示总金额。
①单价框和商品数量框中要求输入的只能数字;②当在单价框中按“回车”时商品数量框获得焦点,当在数量框中按“回车”时完成计算并在总金额框中显示总金额。
实验结果:
要求:
在窗口中添加菜单栏,在菜单栏添加菜单项,并添加下拉菜单和2级菜单,通过选择菜单项可以执行不同操作,如“打开”可打开SY9_3_Menu类生成的窗口。
(知识点:在NetBeans/Eclipse下创建窗体,菜单,事件处理)
图5-5 菜单应用窗口
实验结果:
?5. 题目:编写一个算术测试小软件,如图5-6所示
要求:
用来训练小学生的算术能力。程序由3个类组成,其中GeneratorTM类对象负责给出算术题目,并判断回答者的答案是否正确;ComputerFrame类对象负责为算术题目提供界面,程序运行结果如图5-6所示。
(知识点:在NetBeans/Eclipse下创建窗体,基本组件,事件处理,类与类之间关系,MVC初步了解)
图5-6 算术测试小软件
实验结果:
??|?三、实验源代码
??1. 源代码:
package EXPS.Exp05;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
/**
?* 班级:19软嵌2
?* 学号:20190507223
?* 姓名:夏旭
?* 实验时间:2020-5-11
?* 本程序的功能是:创建一个具有关闭功能的窗口
?*/
public class Exp05_01_20190507223?extends JFrame{
????public static void main(String[] args) {
????????new Exp05_01_20190507223();
????}
????public Exp05_01_20190507223(){
????????JFrame jFrame=new JFrame("JFrame窗口");
????????jFrame.setSize(400,200);
????????jFrame.setVisible(true);
????????jFrame.addWindowListener(new WindowAdapter() {
????????????@Override
????????????public void windowClosing(WindowEvent e) {
????????????????System.exit(0);
????????????}
????????});
????}
}
??2. 源代码:
package EXPS.Exp05;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
?* 班级:19软嵌2
?* 学号:20190507223
?* 姓名:夏旭
?* 实验时间:2020-5-11
?* 本程序的功能是:创建一个具有关闭功能的窗口,添加要求的组件
?*/
public class Exp05_02_20190507223?extends JFrame {
????public static void main(String[] args) {
????????new Exp05_02_20190507223();
????}
????public Exp05_02_20190507223(){
????????JFrame jFrame=new JFrame("JFrame窗口");
????????jFrame.setSize(400,200);
????????jFrame.setLayout(null);
????????jFrame.addWindowListener(new WindowAdapter() {
????????????@Override
????????????public void windowClosing(WindowEvent e) {
????????????????System.exit(0);
????????????}
????????});
????????JLabel user=new JLabel("用户名");
????????JTextField user_col=new JTextField(10);
????????user.setBounds(0,0,50,20);
????????user_col.setBounds(60,0,100,20);
????????jFrame.add(user);
????????jFrame.add(user_col);
????????JLabel tel=new JLabel("电话");
????????JTextField tel_col=new JTextField(10);
????????tel.setBounds(200,0,50,20);
????????tel_col.setBounds(260,0,100,20);
????????jFrame.add(tel);
????????jFrame.add(tel_col);
????????JTextField large_col?=new JTextField(40);
????????large_col.setBounds(0,30,400,100);
????????jFrame.add(large_col);
????????JButton show=new JButton("显示");
????????show.setBounds(80,130,80,35);
????????jFrame.add(show);
????????show.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????user_col.setText("hujuxin");
????????????????tel_col.setText("13914886249");
????????????????large_col.setText("徐州工程学院信电工程学院");
????????????????large_col.setHorizontalAlignment(JTextField.NORTH_EAST);
????????????}
????????});
????????JButton exit=new JButton("退出");
????????exit.setBounds(200,130,80,35);
????????jFrame.add(exit);
????????exit.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????System.exit(0);
????????????}
????????});
???????// jFrame.add(tel,BorderLayout.NORTH);
????????jFrame.setVisible(true);
????}
}
??3. 源代码:
package EXPS.Exp05;
import javax.swing.*;
import java.awt.event.*;
/**
?* 班级:19软嵌2
?* 学号:20190507223
?* 姓名:夏旭
?* 实验时间:2020-5-11
?* 本程序的功能是:输入商品单价和数量,求结果
?*/
public class SY5_2_CalMoney {
????public JFrame jFrame=new JFrame("计算金额");
????public static void main(String[] args) {
????????new SY5_2_CalMoney();
????}
????public SY5_2_CalMoney(){
????????//JFrame jFrame=;
????????jFrame.setSize(200,200);
????????jFrame.setLayout(null);
????????jFrame.addWindowListener(new WindowAdapter() {
????????????@Override
????????????public void windowClosing(WindowEvent e) {
????????????????System.exit(0);
????????????}
????????});
????????JLabel simple=new JLabel("商品单价");
????????JTextField simple_col=new JTextField(10);
????????simple.setBounds(0,0,50,20);
????????simple_col.setBounds(60,0,100,20);
????????JLabel num=new JLabel("购买数量");
????????JTextField num_col=new JTextField(10);
????????num.setFocusable(true);
????????simple_col.addKeyListener(new KeyAdapter() {
????????????@Override
????????????public void keyTyped(KeyEvent e) {
????????????????if(e.getKeyChar()==KeyEvent.VK_ENTER){
????????????????????String s=simple_col.getText();
????????????????????try {
????????????????????????double a=Double.parseDouble(s);
????????????????????????num_col.grabFocus();
????????????????????} catch (NumberFormatException numberFormatException) {
????????????????????????JOptionPane.showMessageDialog(null,"价格必须为数值");
????????????????????????//numberFormatException.printStackTrace();
????????????????????}
????????????????}
????????????}
????????});
????????jFrame.add(simple);
????????jFrame.add(simple_col);
????????num.setBounds(0,40,50,20);
????????num_col.setBounds(60,40,100,20);
????????jFrame.add(num);
????????jFrame.add(num_col);
????????JLabel tot=new JLabel("应付金额");
????????JTextField tot_col=new JTextField(10);
????????tot_col.setEditable(false);
????????tot.setBounds(0,80,50,20);
????????tot_col.setBounds(60,80,100,20);
????????jFrame.add(tot);
????????jFrame.add(tot_col);
????????num_col.addKeyListener(new KeyAdapter() {
????????????@Override
????????????public void keyTyped(KeyEvent e) {
????????????????if(e.getKeyChar()==KeyEvent.VK_ENTER){
????????????????????try {
????????????????????????double n=Double.parseDouble(simple_col.getText());
????????????????????????double v=Double.parseDouble(num_col.getText());
????????????????????????double tot=n*v;
????????????????????????tot_col.setText(String.format("%.2f",tot)+"元");
????????????????????} catch (NumberFormatException numberFormatException) {
????????????????????????JOptionPane.showMessageDialog(null,"输入的数据有误");
????????????????????}
????????????????}
????????????}
????????});
????????jFrame.setVisible(true);
????}
????public void closeit(){
????????this.jFrame.setVisible(false);
????}
}
??4. 源代码:
package EXPS.Exp05;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
?* 班级:19软嵌2
?* 学号:20190507223
?* 姓名:夏旭
?* 实验时间:2020-5-11
?* 本程序的功能是:编写一个菜单,并且可以通过菜单打开指定文件
?*/
public class SY5_3_Menu {
????public static void main(String[] args) {
????????new SY5_3_Menu();
????}
????public SY5_3_Menu(){
????????final SY5_2_CalMoney[] thread = new SY5_2_CalMoney[1];
????????JFrame jFrame=new JFrame("菜单应用");
????????jFrame.setLayout(null);
????????jFrame.setSize(400,200);
????????jFrame.addWindowListener(new WindowAdapter() {
????????????@Override
????????????public void windowClosing(WindowEvent e) {
????????????????System.exit(0);
????????????}
????????});
????????JMenuBar menuBar=new JMenuBar();
????????JMenu fileMenu=new JMenu("文件");
????????JMenuItem open=new JMenuItem("打开");
????????open.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????if(thread[0]!=null){
????????????????????JOptionPane.showMessageDialog(null,"窗口已存在");
????????????????}
????????????????else thread[0] =new SY5_2_CalMoney();
????????????}
????????});
????????JMenuItem close=new JMenuItem("关闭");
????????close.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????if(thread[0]!=null){
????????????????????thread[0].closeit();
????????????????????thread[0]=null;
????????????????}
????????????}
????????});
????????JMenuItem exit=new JMenuItem("退出");
????????exit.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????System.exit(0);
????????????}
????????});
????????JMenu edit=new JMenu("编辑");
????????JMenuItem copy=new JMenuItem("复制");
????????JMenuItem cut=new JMenuItem("剪切");
????????JMenuItem paste=new JMenuItem("粘贴");
????????edit.add(copy);
????????edit.add(cut);
????????edit.add(paste);
????????fileMenu.add(open);
????????fileMenu.add(close);
????????fileMenu.addSeparator();
????????fileMenu.add(exit);
????????fileMenu.add(edit);
????????JMenu helpMenu=new JMenu("帮助");
????????JButton ex=new JButton("退出");
????????ex.setBounds(140,80,80,50);
????????jFrame.add(ex);
????????ex.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????System.exit(0);
????????????}
????????});
????????menuBar.add(fileMenu);
????????menuBar.add(helpMenu);
????????jFrame.setJMenuBar(menuBar);
????????jFrame.setVisible(true);
????}
}
??5. 源代码
package EXPS.Exp05.SY5_4;
import com.sun.source.tree.UsesTree;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
/**
?* 班级:19软嵌2
?* 学号:20190507223
?* 姓名:夏旭
?* 实验时间:2020-5-11
?* 本程序的功能是:编写一个类似计算器的窗口,判断输入结果是否正确
?*/
public class ComputerFrame {
????public static void main(String[] args) {
????????new ComputerFrame();
????}
????public ?ComputerFrame(){
????????JFrame jFrame=new JFrame("算数测试小软件");
????????jFrame.setSize(600,200);
????????jFrame.setLayout(null);
????????jFrame.addWindowListener(new WindowAdapter() {
????????????@Override
????????????public void windowClosing(WindowEvent e) {
????????????????System.exit(0);
????????????}
????????});
????????JLabel selector=new JLabel("选择算数范围");
????????selector.setBounds(20,20,100,40);
????????JComboBox numbers=new JComboBox(new Integer[]{10,20,30,40,50});
????????numbers.setBounds(130,30,60,20);
????????JButton getQues=new JButton("获取题目");
????????getQues.setBounds(230,30,100,20);
????????JTextField BeDecNum=new JTextField(5);
????????BeDecNum.setBounds(30,80,80,20);
????????BeDecNum.setEditable(false);
????????JLabel minus=new JLabel("-");
????????minus.setBounds(120,85,10,10);
????????JTextField DecNum=new JTextField(5);
????????DecNum.setBounds(140,80,80,20);
????????DecNum.setEditable(false);
????????JLabel equ=new JLabel("=");
????????equ.setBounds(230,85,10,10);
????????JTextField ans=new JTextField(5);
????????ans.setBounds(250,80,80,20);
????????JButton test=new JButton("计算结果");
????????test.setBounds(340,80,100,20);
????????JTextField realans=new JTextField();
????????JLabel showmes=new JLabel();
????????showmes.setBounds(160,120,100,20);
????????showmes.setVisible(false);
????????getQues.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????showmes.setVisible(false);
????????????????System.out.println(numbers.getSelectedItem());
????????????????GeneratorTM generatorTM=new GeneratorTM();
????????????????ArrayList<Integer> res=(ArrayList<Integer>) generatorTM.getData((Integer) numbers.getSelectedItem());
????????????????BeDecNum.setText(res.get(0).toString());
????????????????DecNum.setText(res.get(1).toString());
????????????????realans.setText(res.get(2).toString());
????????????}
????????});
????????test.addActionListener(new ActionListener() {
????????????@Override
????????????public void actionPerformed(ActionEvent actionEvent) {
????????????????String getAns=ans.getText();
????????????????String trueAns=realans.getText();
????????????????if(getAns.equals(trueAns)){
????????????????????showmes.setText("你答对了");
????????????????}
????????????????else{
????????????????????showmes.setText("你回答错误");
????????????????}
????????????????showmes.setVisible(true);
????????????}
????????});
????????jFrame.add(getQues);
????????jFrame.add(selector);
????????jFrame.add(numbers);
????????jFrame.add(BeDecNum);
????????jFrame.add(minus);
????????jFrame.add(DecNum);
????????jFrame.add(equ);
????????jFrame.add(ans);
????????jFrame.add(test);
????????jFrame.add(showmes);
????????jFrame.setVisible(true);
????}
}
package EXPS.Exp05.SY5_4;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GeneratorTM {
????public List<Integer> getData(Integer bound){
????????Random random=new Random();
????????List<Integer> res =new ArrayList<>();
????????boolean legal=false;
????????while(!legal){
????????????Integer num1=random.nextInt(bound);
????????????Integer num2=random.nextInt(bound);
????????????Integer num3=num1-num2;
????????????if(num3<=bound && num3>=0){
????????????????legal=true;
????????????????res.add(num1);
????????????????res.add(num2);
????????????????res.add(num3);
????????????}
????????}
????????return res;
????}
}
??|?四、实验总结
通过这次实验,我理解了Java 的事件处理机制,掌握了为不同组件编写事件处理程序的方法;编写独立运行的窗口界面的方法;Java Swing 组件的使用方法;对话框的使用方法。
|