java实验报告6 java_Swing
- 格式:docx
- 大小:307.93 KB
- 文档页数:9
java中swing组件开发一、实验目的1、掌握swing组件开发模型2、掌握flowlayout布局特点3、掌握gridlayout布局特点二、实验内容1. 启动eclipse2. 使用flowlayout创建登录界面3. 使用borderlayout创建登录界面三、实验步骤1、实验内容及要求:使用布局设计登录界面,要求输入用户名和密码正确才能登陆(验证方式不限)。
密码正确后进入系统主界面,主界面至少包括菜单条和文本区域(可以添加工具栏等)。
菜单至少包含“文件”、“编辑”、“格式”三个菜单(可自增加)。
每个菜单有相应菜单项,并要求点击相应菜单项可以实现相应行为。
如点击“文件”菜单中的“打开”菜单项时弹出“打开对话框”。
文本区域要有滚动条,并可进行文字编辑。
应用到的关键技术的详细说明。
可附相关的关键程序代码。
系统页面设计要求清晰、实用、美观。
2、代码:(1)、LoginIn.java(登录页面)1)、public class LoginIn extends JFrame{JTextField f1; JTextField f2; JPanel p5;JButton b1; JButton b2;LoginIn(){Container cp=getContentPane();Label l1=new Label("用户:"); Label l2=new Label("密码:");JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();JPanel p4=new JPanel();b1=new JButton("登录"); b2=new JButton("重置");p2.add(l1);p2.add(f1);p3.add(l2);p3.add(f2);p4.add(b1);p4.add(b2);p5.add(p2); p5.add(p3);p5.add(p4);cp.add(p5,BorderLayout.CENTER);b1.addActionListener(new Enter()); b2.addActionListener(new ReWrite());addWindowListener(new winClose());}public static void main(String[] args) { LoginIn log=new LoginIn();log.setTitle("系统登录");log.setSize(360,250);log.setVisible(true); }2)点击按钮键监听器class Enter implements ActionListener{public void actionPerformed(ActionEvent e){ if((f1.getText()).equals("admin")&&(f2.getText()).equals("123")) {JOptionPane.showMessageDialog(null, "登录成功!"); //进入写字板new NewProgram().setVisible(true);setVisible(false);}else JOptionPane.showMessageDialog(null, "登录失败,请重新登录!"); }} class ReWrite implements ActionListener{//重置public void actionPerformed(ActionEvent e){f1.setText("");f2.setText("");f1.requestFocus();} }class winClose extends WindowAdapter{ public void windowClosing(WindowEvent e){ (e.getWindow()).dispose(); System.exit(0); }}}3)运行程序如图a:当输入错误的用户名或密码时如图b:当输入正确的用户名和密码时,如图c:,点击确定,进入写字板图D:a b cD(2)、写字板代码:1)菜单//新建菜单项监听器New.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { NewProgram.this.setTitle("无标题- 记事本.txt"); ja.setText(""); } });//退出菜单项监听器exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ fileName=NewProgram.this.getTitle(); if(flag==true){int i=JOptionPane.showConfirmDialog(null,NewProgram.this.getTitle()+"的文字已经改变,想保存文件嘛?", "写字板",JOptionPane.YES_NO_CANCEL_OPTION);});//保存菜单项监听器save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FileDialog fd=new FileDialog(NewProgram.this,"保存",FileDialog.SA VE);fd.setFile("*.txt"); fd.setVisible(true); filePath=fd.getDirectory();fileName=fd.getFile(); try {FileWriter fw = new FileWriter(filePath+fileName);BufferedWriter bw = new BufferedWriter(fw);PrintWriter pw = new PrintWriter(bw); pw.println(ja.getText()); pw.flush(); pw.close();} catch(IOException ioe){ ioe.printStackTrace(); } } });//另存为菜单项监听器saveas.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) { FileDialog fd=new FileDialog(NewProgram.this,"另存为...",FileDialog.SA VE);fd.setFile("*.txt"); fd.setVisible(true); filePath=fd.getDirectory();fileName=fd.getFile(); try {FileWriter fw = new FileWriter(filePath+fileName);BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw);pw.println(ja.getText()); pw.flush(); pw.close(); } catch(IOException ioe){ ioe.printStackTrace(); } } });//打开菜单项监听器open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){FileDialog fd1 = new FileDialog(NewProgram.this,"打开",FileDialog.LOAD);fd1.setFile("*.txt"); fd1.setVisible(true); fileName=fd1.getFile();filePath=fd1.getDirectory(); NewProgram.this.setTitle(fileName);try { FileReader fr = new FileReader(filePath+fileName);BufferedReader br = new BufferedReader(fr);String sinput = ""; ja.setText(""); int lineNum = 0;while ((sinput = br.readLine()) != null)// 读行,直到为空时停止循环{ ja.append(sinput + "\r\n"); lineNum++; } } catch(IOException ex){ ex.printStackTrace(); }} });2)、格式菜单//加系统的字体设置String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();for(int i=0;i<fontNames.length;i++) { fontList.add(fontNames[i]); }//字体颜色、大小、形状面板监听器:color.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ JColorChooser jColor=new JColorChooser(); //调用颜色面板,设置前景就可更改字体颜色Color fcolor=ja.getForeground();ja.setForeground( jColor.showDialog(ja,"选择字体颜色",fcolor)); } });cancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { fontMain.setVisible(false); }});ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { fontMain.setVisible(false); } });font.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fontJd.setLocationRelativeTo(NewProgram.this);fontJd.setVisible(true); int style = 0; fontJd.dispose(); } } } });//文件改变事件的监听器ja.getDocument().addDocumentListener(new DocumentListener(){ public void changedUpdate(DocumentEvent arg0) { flag=true; }public void insertUpdate(DocumentEvent arg0) { flag=true; }public void removeUpdate(DocumentEvent arg0) { flag=true; } });this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent w) {fileName=NewProgram.this.getTitle();if(flag==true){int i=JOptionPane.showConfirmDialog(null,NewProgram.this.getTitle()+"的文字已经改变,想保存文件嘛?", "写字板",JOptionPane.YES_NO_CANCEL_OPTION); });3、编辑菜单//右键快捷方式监听器jcopy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ ja.copy(); } } ); jpaste.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ja.paste(); } } );jcut.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ja.cut(); } });jselectall.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ja.selectAll(); } });ja.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e){ if (e.isPopupTrigger()){ jp.show((Component) (e.getSource()), e.getX(), e.getY());//复制菜单项监听器copy.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ja.copy(); } } );//粘贴菜单项监听器paste.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ try { ja.paste(); }catch(Exception ex) { ex.printStackTrace(); } } } );//剪切菜单项监听器cut.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) { try { ja.cut(); } Catch(Exception ex) { ex.printStackTrace(); } } });selectall.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ja.selectAll(); } });//关于监听器about.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){jfm.setLocationRelativeTo(NewProgram.this); jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { jfm.setVisible(false); } }); } });//状态栏菜单监听器lookup.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (a==1) {lb.setVisible(false); a=0; } else if (a==0){ NewProgram.this.add(lb, BorderLayout.SOUTH); lb.setVisible(true);lb.setText("当前字数: "+String.valueOf(ja.getText().trim().length())+" "+"当前行数:"+String.valueOf(ja.getLineCount())); a=1; } } });//添加滚动条JScrollPane jsp = new JScrollPane(ja); Container c = this.getContentPane(); c.add(jsp);jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEE DED);jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.setVisible(true); }}实验问题:1、使用布局设计登录界面,要求输入用户名和密码正确才能登陆,否则则不能。
图形界面(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结果:输出正常。
南昌航空大学实验报告二0一一年四月十六日课程名称:java语言与面向对象程序设计实验名称:异常、多线程与Swing班级:08061105 姓名:黄依婷同组人:指导教师评定:签名:一、实验目的(1)了解java的异常处理机制。
(2)掌握使用java的异常处理机制来提高程序的容错性的方法。
(3)了解线程的概念。
(4)学习简单的多线程编程。
(5)学习简单的Swing图形界面程序设计。
二、实验任务(1)选择一个比较了解的java系统异常,例如ArrayIndexOutofBoundsException,编程创建并抛出这个异常类的实例。
运行该程序并观察执行结果。
(2)利用多线程技术编写Applet程序,包含一个滚动的字符串,字符串从左向右运动,当所有的字符都从屏幕的右边消失后,字符串重新从左边出现并继续向右滚动。
(3)利用多线程技术编写Applet程序,实现在播放一个动画文件的同时,播放一段背景音乐。
三、实验分析与代码实现(1)根据题意,自定义一个异常类ArrayIndexOutofBoundsException,当一个数组的长度大于100或则小于0的时候为数组越界,源代码如下:import java.io.*;class ArrayIndexOutOfBoundsException extends Exception{public ArrayIndexOutOfBoundsException(){super();}public ArrayIndexOutOfBoundsException(String s){super(s);}}public class ArrayIndex{public static void main(String args[]){int str[] = new int[109];String s;try{i nt array = Array(str);s = String.valueOf(array);}catch (ArrayIndexOutOfBoundsException e){ s = e.getMessage(); }System.out.println(s);}public static int Array(int str[]) throws ArrayIndexOutOfBoundsException{int int1;int answer = -1;try {int1 = str.length;if( int1 > 100 || int1 < 0 ){ ArrayIndexOutOfBoundsException e = newArrayIndexOutOfBoundsException("The array is out ofbounds.");throw e; }answer = int1;} catch (ArrayIndexOutOfBoundsException e){System.out.println( e.toString() ); } return answer; }}(2)import java.awt.*;import java.applet.*;public class Huang extends Applet implements Runnable{final String rollingMessage = "做自己有计划。
java实训报告总结和心得3篇Summary and experience of java training report汇报人:JinTai Collegejava实训报告总结和心得3篇前言:报告是按照上级部署或工作计划,每完成一项任务,一般都要向上级写报告,反映工作中的基本情况、工作中取得的经验教训、存在的问题以及今后工作设想等,以取得上级领导部门的指导。
本文档根据申请报告内容要求展开说明,具有实践指导意义,便于学习和使用,本文档下载后内容可按需编辑修改及打印。
本文简要目录如下:【下载该文档后使用Word打开,按住键盘Ctrl键且鼠标单击目录内容即可跳转到对应篇章】1、篇章1:java实训个人总结样本(最新版)2、篇章2:java项目总结范文标准版3、篇章3:工程实训心得体会模板篇章1:java实训个人总结样本(最新版)在学院领导老师的带领和安排下,我们在上个学期末,到北京进行了为期10天的专业实习。
在实习过程中,我们在专编程技能以及软件开发的总体架构思想上都收获颇丰。
本次实训我们分为两个阶段,前五天为第一阶段,我们进行了理论知识的学习,巩固和深化了所学的编程知识。
以下谈一谈个人的感受与体会一、理论知识的学习在实训进行的前五天,我们上的是java编程的理论课。
负责上课的老师言语风趣幽默而不失严谨,在老师的引导和启发下,我们巩固了之前在学校所学的编程知识,并加以深化,澄清之前对编程技术认识的一些错误或模糊的概念。
我们还在课上以java里GUI编程常用的swing框架和JAVA的容器体系作为切入点,框架作为这期间除了学习一般的编程的知识技巧。
在全面的系统的认识了框架同时补充学习了泛型技术,以及框架中架构思想的知识。
在企业的实际编码过程中,一个个框架构成了软件的基石,只有掌握了几门框架知识,才能在实践中高效开发,让自己的产品在市场上立于不败之地。
在理论课的学习过程中,我们也暴露了一些问题,比如在学校上课学习时,对所学知识只是知其然而不知其所以然,不太愿意深钻,和超前学习一些知识。
信息工程学院Java语言课内实习报告(2013~2014学年第二学期)实习题目:Swing姓名:学号:专业:年级班级:一、实习目的使用Java做图形界面啦,了解图形用户界面基本组件的使用,布局管理器如何对组件进行管理以及事件处理机制的应用。
二、实习设计过程任务一:小小界面设计请设计一个界面,包括:菜单,文本框,按钮,标签,文本区,选择框,单选按钮和下拉列表。
任务二: 计算器(1)编写一个计算器的程序,主界面由多个按钮组建、文本框组建构成。
运行结果如图所示:任务三:算数测试编写一个算术测试小软件,用来训练小学生的算术能力。
程序由3个类组成,其中Teacher类对象负责给出算术题目,并判断回答者的答案是否正确:ComputerFrame类对象提供的GUI界面看到题目并通过该GUI界面给出题目的答案;MailClass是主类。
任务四:日历该窗口为BorderLayout布局。
窗口的中心添加一个Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter 的中放置49个标签,用来显示日历。
窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击previousMonth按钮,可以显示当前月的上一个月的日历。
窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中请完成界面设计和相关功能。
三、调试过程中存在问题分析布局过程中运行出来的图片效果不是自己设计的那样,有的文字被隐藏,重新调整文本框大小就弄好了。
四、心得、体会与建议这次实习过程中,感觉最有难度的是算数测试题目的设计,要判定结果是否正确。
总体做起来还是很简单的。
附录:题目1代码:import javax.swing.*;import java.awt.*;public class MyTu {private JFrame frame;private JTextField textField;private JTextField textField_1;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MyTu window = new MyTu();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public MyTu() {initialize(null);}/*** Initialize the contents of the frame.* @param menuBar*/@SuppressWarnings({ "unchecked", "rawtypes" })private void initialize(JMenuBar menuBar) {frame = new JFrame();frame.setTitle("\u6211\u81EA\u5DF1\u7684\u5C0F\u5C0F\u754C\u9762");frame.setBounds(100, 100, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);menuBar = new JMenuBar();frame.setJMenuBar(menuBar);JMenu mnNewMenu = new JMenu("\u6587\u4EF6");mnNewMenu.setFont(new Font("宋体", Font.PLAIN, 14));mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);menuBar.add(mnNewMenu);JMenuItem mntmNewMenuItem = new JMenuItem("\u65B0\u5EFA");mntmNewMenuItem.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu.add(mntmNewMenuItem);JMenuItem menuItem = new JMenuItem("\u6253\u5F00");menuItem.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu.add(menuItem);JMenuItem menuItem_1 = new JMenuItem("\u5173\u95ED");menuItem_1.setHorizontalAlignment(SwingConstants.LEFT); mnNewMenu.add(menuItem_1);JMenu mnNewMenu_1 = new JMenu("\u7F16\u8F91"); mnNewMenu_1.setFont(new Font("宋体", Font.PLAIN, 14)); mnNewMenu_1.setHorizontalAlignment(SwingConstants.CENTER); menuBar.add(mnNewMenu_1);JMenuItem menuItem_2 = new JMenuItem("\u67E5\u627E"); menuItem_2.setHorizontalAlignment(SwingConstants.LEFT); mnNewMenu_1.add(menuItem_2);JMenuItem menuItem_3 = new JMenuItem("\u66FF\u6362"); menuItem_3.setHorizontalAlignment(SwingConstants.LEFT); mnNewMenu_1.add(menuItem_3);JMenuItem menuItem_4 = new JMenuItem("\u5B9A\u4F4D"); menuItem_4.setHorizontalAlignment(SwingConstants.LEFT); mnNewMenu_1.add(menuItem_4);JMenu menu = new JMenu("\u683C\u5F0F");menu.setHorizontalAlignment(SwingConstants.CENTER);menu.setFont(new Font("宋体", Font.PLAIN, 14));menuBar.add(menu);JMenuItem menuItem_5 = new JMenuItem("\u6BB5\u843D"); menu.add(menuItem_5);JMenuItem menuItem_6 = new JMenuItem("\u5B57\u4F53"); menu.add(menuItem_6);frame.getContentPane().setLayout(null);textField = new JT extField();textField.setBounds(91, 38, 122, 21);frame.getContentPane().add(textField);textField.setColumns(10);JLabel label = new JLabel("\u7528\u6237\u540D");label.setBounds(23, 38, 58, 21);frame.getContentPane().add(label);JLabel label_1 = new JLabel("\u5BC6\u7801");label_1.setBounds(23, 72, 41, 15);frame.getContentPane().add(label_1);textField_1 = new JTextField();textField_1.setBounds(91, 69, 122, 21);frame.getContentPane().add(textField_1);textField_1.setColumns(10);JLabel label_2 = new JLabel("\u6027\u522B");label_2.setBounds(23, 107, 54, 15);frame.getContentPane().add(label_2);JRadioButton RadioButton2 = new JRadioButton("\u7537"); RadioButton2.setBounds(84, 103, 54, 23);frame.getContentPane().add(RadioButton2);JRadioButton RadioButton1 = new JRadioButton("\u5973"); RadioButton1.setBounds(157, 103, 54, 23);frame.getContentPane().add(RadioButton1);ButtonGroup select=new ButtonGroup();select.add(RadioButton1);select.add(RadioButton2);JLabel label_3 = new JLabel("\u6BD4\u8D5B\u7C7B\u578B"); label_3.setBounds(23, 147, 75, 21);frame.getContentPane().add(label_3);JCheckBox checkBox = new JCheckBox("\u4E13\u4E1A\u7EC4");checkBox.setBounds(84, 146, 66, 23);frame.getContentPane().add(checkBox);JCheckBox checkBox_1 = new JCheckBox("\u4E1A\u4F59\u7EC4");checkBox_1.setBounds(157, 146, 103, 23);frame.getContentPane().add(checkBox_1);JButton button = new JButton("\u767B\u9646");button.setBounds(38, 210, 93, 23);frame.getContentPane().add(button);JButton button_1 = new JButton("\u53D6\u6D88");button_1.setBounds(182, 210, 93, 23);frame.getContentPane().add(button_1);JTextArea textArea = new JTextArea();textArea.setT ext("\u62A5\u540D\u7684\u76EE\u7684\u662F\uFF1A");textArea.setBounds(266, 58, 160, 144);frame.getContentPane().add(textArea);JComboBox comboBox = new JComboBox();comboBox.setBounds(265, 27, 103, 21);comboBox.addItem("比赛");comboBox.addItem("报名");frame.getContentPane().add(comboBox);JLabel label_4 = new JLabel("\u62A5\u540D\u76EE\u7684");label_4.setBounds(266, 10, 54, 15);frame.getContentPane().add(label_4);}}题目2代码:import java.awt.EventQueue;public class MyTuTwo {private JFrame frame;private JTextField textField;/*** Launch the application.*/public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {public void run() {try {MyTuTwo window = new MyTuTwo();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public MyTuTwo() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame();frame.setTitle("\u7B80\u6613\u8BA1\u7B97\u5668");frame.getContentPane().setFont(new Font("Dialog", Font.PLAIN, 15)); frame.setBounds(100, 100, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);JPanel panel_1 = new JPanel();panel_1.setBounds(20, 66, 404, 37);frame.getContentPane().add(panel_1);panel_1.setLayout(null);JButton btnNewButton_23 = new JButton("Back");btnNewButton_23.setFont(new Font("Dialog", Font.PLAIN, 15)); btnNewButton_23.setBounds(0, 0, 96, 33);panel_1.add(btnNewButton_23);JButton btnNewButton_22 = new JButton("CE");btnNewButton_22.setFont(new Font("Dialog", Font.PLAIN, 15)); btnNewButton_22.setBounds(100, 0, 96, 33);panel_1.add(btnNewButton_22);JButton btnNewButton_21 = new JButton("C");btnNewButton_21.setFont(new Font("Dialog", Font.PLAIN, 15)); btnNewButton_21.setBounds(225, 0, 86, 33);panel_1.add(btnNewButton_21);JButton btnNewButton_20 = new JButton("About");btnNewButton_20.setFont(new Font("Dialog", Font.PLAIN, 15)); btnNewButton_20.setBounds(316, 0, 88, 33);panel_1.add(btnNewButton_20);JPanel panel_2 = new JPanel();panel_2.setBounds(20, 113, 195, 139);frame.getContentPane().add(panel_2);panel_2.setLayout(new GridLayout(4, 3, 4, 4));JButton btnNewButton_1 = new JButton("9");btnNewButton_1.setFont(new Font("Dialog", Font.PLAIN, 15));panel_2.add(btnNewButton_1);JButton btnNewButton_3 = new JButton("8");btnNewButton_3.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_3);JButton btnNewButton_6 = new JButton("7");btnNewButton_6.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_6);JButton btnNewButton_4 = new JButton("6");btnNewButton_4.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_4);JButton btnNewButton_5 = new JButton("5");btnNewButton_5.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_5);JButton btnNewButton_2 = new JButton("4");btnNewButton_2.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_2);JButton btnNewButton = new JButton("3");btnNewButton.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton);JButton btnNewButton_9 = new JButton("2");btnNewButton_9.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_9);JButton btnNewButton_7 = new JButton("1");btnNewButton_7.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_7);JButton btnNewButton_8 = new JButton("0");btnNewButton_8.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_8);JButton btnNewButton_10 = new JButton("+/-");btnNewButton_10.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_10);JButton btnNewButton_11 = new JButton(".");btnNewButton_11.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_2.add(btnNewButton_11);JPanel panel_3 = new JPanel();panel_3.setBounds(245, 115, 179, 137);frame.getContentPane().add(panel_3);panel_3.setLayout(new GridLayout(4, 2, 4, 4));JButton btnNewButton_13 = new JButton("/");btnNewButton_13.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_3.add(btnNewButton_13);JButton btnNewButton_14 = new JButton("sqrt"); btnNewButton_14.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_3.add(btnNewButton_14);JButton btnNewButton_16 = new JButton("*");btnNewButton_16.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_3.add(btnNewButton_16);JButton btnNewButton_15 = new JButton("%");btnNewButton_15.setFont(new Font("Dialog", Font.PLAIN, 15)); panel_3.add(btnNewButton_15);JButton btnNewButton_18 = new JButton("-");btnNewButton_18.setFont(new Font("Dialog", Font.PLAIN, 15));panel_3.add(btnNewButton_18);JButton btnNewButton_12 = new JButton("1/x");btnNewButton_12.setFont(new Font("Dialog", Font.PLAIN, 15));panel_3.add(btnNewButton_12);JButton btnNewButton_17 = new JButton("+");btnNewButton_17.setFont(new Font("Dialog", Font.PLAIN, 15));panel_3.add(btnNewButton_17);JButton btnNewButton_19 = new JButton("=");btnNewButton_19.setFont(new Font("Dialog", Font.PLAIN, 15));panel_3.add(btnNewButton_19);JPanel panel = new JPanel();panel.setBounds(20, 10, 404, 30);frame.getContentPane().add(panel);panel.setLayout(null);textField = new JTextField();textField.setBounds(0, 0, 404, 30);panel.add(textField);textField.setHorizontalAlignment(SwingConstants.RIGHT);textField.setText("0");textField.setEditable(false);textField.setColumns(10);}}题目3代码:import java.awt.*;public class ComputerJFrame extends JFrame implements ActionListener{JTextField textOne,textTwo,textResult;JButton getProblem,giveAnswer;JLabel operatorLabel,message;Teacher teacher;ComputerJFrame(String s){super(s);teacher = new Teacher();textOne=new JTextField(10);textOne.setBounds(111, 6, 66, 21);textTwo=new JTextField(10);textTwo.setBounds(215, 6, 66, 21);textResult=new JT extField(10);textResult.setBounds(307, 6, 66, 21);operatorLabel =new JLabel("+");operatorLabel.setBounds(187, 9, 18, 15);message = new JLabel("你还没有回答呢");message.setBounds(220, 37, 108, 15);getProblem = new JButton("获取题目");getProblem.setBounds(10, 5, 91, 23);giveAnswer = new JButton("确认答案");giveAnswer.setBounds(103, 33, 101, 23);getContentPane().setLayout(null);getContentPane().setLayout(null);getContentPane().setLayout(null);getContentPane().setLayout(null);getContentPane().add(getProblem);getContentPane().add(textOne);getContentPane().add(operatorLabel);getContentPane().add(textTwo);Label label = new Label("=");label.setBounds(290, 5, 21, 23);getContentPane().add(label);getContentPane().add(textResult);getContentPane().add(giveAnswer);getContentPane().add(message);//添加若干组件到窗口textResult.requestFocus();//textResult 获得焦点textOne.setEditable(false);textTwo.setEditable(false);//textOne设置textOne和textTwo不可编辑getProblem.addActionListener(this);//将当前窗口注册为getProblem的ActionEvent事件监视器giveAnswer.addActionListener(this);//将当前窗口注册为giveAnwser的ActionEvent事件监视器textResult.addActionListener(this);//将当前窗口注册为textResult的ActionEvent事件监视器setBounds(100, 100, 432, 100);setLocationRelativeTo(null);//设置窗体居中显示setVisible(true);//窗口的可见性设置validate();addWindowFocusListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}public void actionPerformed(ActionEvent e){if(e.getSource()==getProblem)//如果点击了“获取题目”{int number1 = teacher.giveNumberOne(100);//获取第一个操作数的值int number2 = teacher.giveNumberTwo(100);//获取第二个操作数的值String operator = teacher.giveOperator();//获取运算符textOne.setText(""+number1);//设置文本框参数字符串指定文本textTwo.setText(""+number2);operatorLabel.setText(operator);message.setText("请回答");textResult.setText(null);//设置图形界面上文本框以及标签的文本,包括:textOne,textTwo,textResult,operatorLabel,message}if(e.getSource()==giveAnswer)//如果点击了“确认答案”{String answer = textResult.getText();//获取文本框中输入的答案try{int result = Integer.parseInt(answer);if(teacher.getRight(result)==true){message.setText("回答正确");}else{message.setText("回答错误");}//如果答案正确,message显示“你回答正确”,如果不正确,显示“你回答错误”}catch(NumberFormatException ex){message.setText("请输入数字字符");//如果输入的不是数字,显示“请输入数字字符”}}textResult.requestFocus();//textResult 获得焦点validate();}}题目4代码:import java.awt.EventQueue;public class Calendar {private JFrame frame;private JLabel lblNewLabel_1;private JLabel[] labelDay= new JLabel[42];private JButton button_1;private JButton button_2;private JButton button_3;private JButton button_4;private JButton button_5;private JButton button_6;private JButton button_7;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Calendar window = new Calendar();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public Calendar() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame();frame.setTitle("简易日历");frame.setBounds(100, 100, 348, 260);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new BorderLayout(0, 0));JPanel pNorth = new JPanel();pNorth.setBackground(Color.WHITE);FlowLayout fl_pNorth = (FlowLayout) pNorth.getLayout();fl_pNorth.setHgap(10);frame.getContentPane().add(pNorth, BorderLayout.NORTH);JPanel pSouth = new JPanel();pSouth.setBackground(Color.WHITE);frame.getContentPane().add(pSouth, BorderLayout.SOUTH);Date date=new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");String s=formatter.format(date);String[] dateArr = s.split("-");final String year=dateArr[0]; //年final String month=dateArr[1]; //月//int m=Integer.parseInt(month);final int y=Integer.parseInt(year);final JLabel message = new JLabel("日历:"+y+"年"+month+"月");String[] str = message.getText().split("年");final String[] st=str[1].split("月");pSouth.add(message);JPanel pCenter = new JPanel();pCenter.setBackground(Color.WHITE);pCenter.setBorder(null);frame.getContentPane().add(pCenter, BorderLayout.CENTER);pCenter.setLayout(new GridLayout(7, 7, 0, 0));button_7 = new JButton("日");pCenter.add(button_7);button_1 = new JButton("一");pCenter.add(button_1);button_2 = new JButton("二");pCenter.add(button_2);button_3 = new JButton("三");pCenter.add(button_3);button_4 = new JButton("四");pCenter.add(button_4);button_5 = new JButton("五");pCenter.add(button_5);button_6 = new JButton("六");pCenter.add(button_6);for(int i=0;i<42;i++){labelDay[i] = new JLabel("",JLabel.CENTER);labelDay[i].setBackground(Color.WHITE);labelDay[i].setFont(new Font("宋体", Font.PLAIN, 15));labelDay[i].setHorizontalAlignment(SwingConstants.CENTER);pCenter.add(labelDay[i]);}int j=1;for(int i=0;i<30;i++){labelDay[i].setText(""+(j++));}JButton previousMonth = new JButton("上月");previousMonth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {String[] str = message.getText().split("年");String[] st=str[1].split("月");int m=Integer.parseInt(st[0]);m=m-1;if(m==0){m=12;}message.setText("日历:"+y+"年"+m+"月");int j=1;if(m==5){for(int k=0;k<5;k++){labelDay[k].setT ext("");}for(int i=4;i<35;i++){labelDay[i].setText(""+(j++));}}else if(m==6){for(int i=0;i<30;i++){labelDay[i].setText(""+(j++));for(int q=30;q<42;q++){labelDay[q].setText("");}}}}});pNorth.add(previousMonth);JButton nextMonth = new JButton("下月");nextMonth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {String[] str = message.getText().split("年");String[] st=str[1].split("月");int m=Integer.parseInt(st[0]);m=m+1;if(m==13){m=1;}message.setText("日历:"+y+"年"+m+"月");int j=1;if(m==7){for(int i=2;i<33;i++){labelDay[0].setText("");labelDay[1].setText("");labelDay[i].setText(""+(j++));for(int w=33;w<42;w++){labelDay[w].setText("");}}}else if(m==6){for(int i=0;i<30;i++){labelDay[i].setText(""+(j++));}for(int q=30;q<42;q++){labelDay[q].setText("");}}}});pNorth.add(nextMonth);}}。
四、实验方法和步骤
实验方法:
1、本系统是基于Java Swing+MySQL实现的图书管理系统。
2、采用Java Swing作为UI界面
3、MySQL数据库作为存储
4、JDBC原生方法操作
(封装了公共的增删改查方法,见src.model.access.Connect.java)
5、采用MVC模式分为Model层、View层、Controller层
6、采用了MD5进行密码加密
实验步骤:
1、需求分析
2、概念结构,绘制E-R图
3、逻辑结构设计:将E-R图转化为等价的关系模式
4、物理结构设计:数据库表的基本结构
5、功能实现
五、实验结果(截图)
1、登录界面
2、普通用户登录界面
3、管理员登录界面
4用户界面功能分别是
图书查询借阅、借阅归还信息、个人信息管理4.1借阅
4.3查询
4.4借阅信息
4.5归还
4.6个人信息管理4.7修改个人信息
4.8修改密码
5.登录界面功能5.1注册功能
6.忘记密码功能
7.管理端功能
图书信息管理、图书种类管理、读者类型管理、图书借阅信息、超级用户7.1界面
7.2添加图书
7.3删除图书
7.4修改图书
7.5图书类型管理7.6读者信息管理历史借阅信息
7.7超级管理员界面修改个人信息
修改个人密码
7.8超级管理员验证查询管理员信息
新增管理员
删除管理员
修改该管理员信息。
《Java程序设计-实验报告》
计算机科学系级班
课程名:《Java程序设计》
姓名:
学号:
指导教师:
实验项目:“Java GUI程序设计”
实验目的:
1、熟悉和掌握java Swing的基本GUI程序设计,掌握如何构建一个窗口程序,并且窗口中添加常用的Swing组件,使用各种布局管理器来完成对窗口中的组件进行布局。
2、了解Java事件处理机制,掌握基本的事件处理机制的基本程序编写。
实验要求:
使用java语言完成相关程序设计要求,编写代码、调试、运行,将编写的程序写在题目要求后面,对结果进行分析、得出编程的结论和心得。
实验内容:(两个题目,二选一)
1、试着用Swing类库完成以下程序界面(自行截图,尽量与下面界面接近):
2、试着用Swing类库完成以下程序界面(自行截图,尽量与下面界面接近):
提交要求:独立完成以上各个程序,查询相关资料和文档完成以上程序设计,将最后做完的界面截图,摘抄部分关键程序语句,写出碰到的问题,如何接近,心得总结等,17周之内提交打印文档。
实验报告课程名称: Java语言程序设计院(系):管理学院专业班级:姓名:学号:指导教师:2013年6月9日《Java语言程序设计》上机实验报告序号实验五组号 1 成绩名称图形用户界面AWT/Swing组件学时 2评阅教师签名: 2013年月日一、实验目的1.熟悉Java AWT、swing组件的功能;2.掌握常用组件的事件接口;3.会应用AWT、swing组件进行应用程序设计。
二、实验内容AWT、swing组件应用程序设计设计(完成windows计算器两种以上的运算功能)。
三、实验硬件、软件环境1.PC计算机一台,配置为CPU为P42.8G,内存为512M,硬盘为80G;2.WindowsXP + JSDK1.6 + JCreatorV4。
四、实验算法设计在设计计算其界面的基础上,添加事件监视,对按钮进行各种操作。
五、程序设计/*** @(#)Calculator.java*** @author* @version 1.00 2013/6/9*/import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Calculator extends JFrame{private final JTextField textField;private String num="0";private String operator="+";private String result="0";public static void main(String agrs[]){Calculator frame=new Calculator();frame.setVisible(true);}public Calculator(){super();setTitle("计算器");setResizable(false);setBounds(100,100,208,242);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel viewPanel=new JPanel();add(viewPanel,BorderLayout.NORTH);textField=new JTextField();textField.setText(num);textField.setColumns(18);textField.setEditable(false);textField.setHorizontalAlignment(SwingConstants.RIGHT);viewPanel.add(textField);add(viewPanel,BorderLayout.NORTH);final JPanel clearButtonPanel=new JPanel();clearButtonPanel.setLayout(new GridLayout(1,3,5,5));add(clearButtonPanel,BorderLayout.CENTER);String[] clearButtonNames={"<-","CE","C"};for(int i=0;i<3;i++){final JButton button=new JButton(clearButtonNames[i]);button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {JButton button=(JButton)e.getSource();String text=button.getText().trim();if(text.equals("<-")){int length=num.length();if(length==1)num="0";else num=num.substring(0,length-1);}else if(text.equals("CE")){num="0";}else{num="0";operator="+";result="0";}textField.setText(num);}});clearButtonPanel.add(button);};final JPanel inputButtonPanel=new JPanel();final GridLayout gridLayout=new GridLayout(4,0,10,10);inputButtonPanel.setLayout(gridLayout);add(inputButtonPanel,BorderLayout.SOUTH);String[][]inputButtonNames={{"1","2","3","+"},{"4","5","6","-"},{"7","8","9","*"},{".","0","=","/"} };for(int row=0;row<4;row++){for(int col=0;col<4;col++){final JButton button=new JButton(inputButtonNames[row][col]);button.setName(row+""+col);button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JButton button=(JButton)e.getSource();String name=button.getName();int row=Integer.valueOf(name.substring(0,1));int col=Integer.valueOf(name.substring(1,2));100if(col==3){count();textField.setText(result);operator=button.getText();}else if(row==3){if(col==0){if(num.indexOf(".")<0){num=num+button.getText();textField.setText(num);}}else if(col==1){if(num.indexOf(".")>0){num=num+button.getText();textField.setText(num);}else{if(!num.substring(0,1).equals("0")){num=num+button.getText();textField.setText(num);}}}else{count();textField.setText(result);operator="+";}}else{if(num.equals("0"))num=button.getText();else num=num+button.getText();textField.setText(num);}}void count(){float n=Float.valueOf(num);float r=Float.valueOf(result);if(r==0){result=num;num="0";}else{if(operator.equals("+")){r=r+n;}else if(operator.equals("-")){r=r-n;}else if(operator.equals("*")){r=r*n;}else {r=r/n;}num="0";result=r+"";}}});inputButtonPanel.add(button);}}}}六、程序测试记录及结果、运行说明七、实验总结系统的设计程序,完成程序前后关照,解决每个问题关键要求。
《Java程序设计》
课程实验报告(第 6 次)
学院:
姓名:
学号:
专业:软件工程
班级:2班
实验地点:分析测试中心6A-1
实验时间:
指导教师:
实验名称:Java Swing
所使用的开发工具及环境:JDK1.8+Eclpise
实验要求:
实验目的:
1、掌握窗口创建
2、掌握常用组件(JButton,JTextField,JFram,JPanel,JLabel,JScrollPane)
3、掌握常用布局(FlowLayout,CardLayout,GridLayout,BorderLayout,BoxLayout)
实验内容:
1.Jbutton,GridLayout,BorderLayout,Jpanel的用法
按钮的ActionEvent事件处理
实验要求:
编写一个Java应用程序,采用GridLayout实现如下计算机器的布局及做相应的事件处理
程序运行效果示例:
程序运行效果如下图所示:
computer类:
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.JPanel;
import javax.swing.JTextField;
public class Computer extends JFrame implements ActionListener{
JTextField computText;
JPanel numPanel,operPanel;
JButton numBtn[],operBtn[];
String expression="";
public void init(){
computText=new JTextField();
expression+=Integer.parseInt(nums[0])+Integer.parseInt(nums[1]); computText.setText(expression);
}else if(oper.equals("-")) {
expression+=Integer.parseInt(nums[0])-Integer.parseInt(nums[1]); computText.setText(expression);
}else if(oper.equals("*")) {
expression+=Integer.parseInt(nums[0])*Integer.parseInt(nums[1]); computText.setText(expression);
}else if(oper.equals("/")) {
expression+=Integer.parseInt(nums[0])/Integer.parseInt(nums[1]); computText.setText(expression);
}
expression="";
}
}
}
测试类:
public class Test {
public static void main(String[] args) {
new Computer();
}
}
2.Jbutton,CardLayout,BorderLayout,Jpanel,JLabel的用法按钮的ActionEvent事件处理
实验要求:
编写一个Java应用程序,实现如下图布局及做相应的事件处理程序运行效果示例:
程序运行效果如下图所示:
mywindow类:v
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public Mywindow(){
this.setVisible(true);
this.setBounds(40, 40, 400,300);
this.setLayout(new BorderLayout());
init();
this.validate();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==open1) {
card.show(p, "p1");
}else if(e.getSource()==open2) {
card.show(p, "p2");
}else if(e.getSource()==exitItem) {
System.exit(0);
}
}
}
Test:
public class Test {
public static void main(String[] args) {
new Mywindow();
}
}
结果与分析:。