java实验十图形用户界面.doc_第1页
java实验十图形用户界面.doc_第2页
java实验十图形用户界面.doc_第3页
java实验十图形用户界面.doc_第4页
java实验十图形用户界面.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图形用户界面(1)张鑫2015-12-25Java语言程序设计实验报告(十)电子商务1302电子商务李鸿儒实验名称:_ 指导教师:_ 实验日期:_专 业:_ 班 级:_ 姓 名:_一、实验目的:1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法。2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。二、实验内容:一创建图形用户界面图形用户界面(Graphic User Interface ,简称GUI)是为方便用户使用设计的窗口界面,在图形用户界面中用户可以看到什么就操作什么,取代了在字符方式下知道是什么后才能操作什么的方式。组件(Component)是构成GUI 的基本要素,通过对不同事件的响应来完成和用户的交互或组件之间的交互。组件一般作为一个对象放置在容器(Container)内,容器是能容纳和排列组件的对象,如Applet、Panel(面板)、Frame(窗口)等。通过容器的add 方法把组件加入到容器中。二了解事件处理机制在图形用户界面中,程序和用户的交互是通过组件响应各种事件来实现的。例如,用户单击了一个按钮,意味着发生了按钮的单击事件;选中下拉框中的一个选项,意味着发生了一个选项事件。在Java 中能产生事件的组件叫做事件源,如按钮。如果希望对单击按钮事件进行处理,可给事件源(按钮)注册一个事件监听器(如包含按钮的容器),如同签订了一个委托合同,当事件源发生事件时,事件监听器就代替事件源对发生的事件进行处理,这就是所谓的委托事件处理机制。三建立独立运行的窗口界面并使用匿名类最常使用的包含组件的容器是窗口,在Java 中窗口由Frame 类生成。1创建一个窗口界面(1)程序功能:创建一个具有关闭功能的空白窗口。(2)编写LX10_1.java 程序文件,源代码如下。import java.awt.*;import java.awt.event.*;import javax.swing.JFrame;public class LX10_1 public static void main(String args) new LX10_1 ();LX10_1 ()JFrame f=new JFrame(初始窗口);/创建窗口对象f.setSize(350,200);/设置窗口大小f.setVisible(true);/设置窗口是可视的f.addWindowListener(new WindowAdapter() /为窗口添加窗口事件适配器public void windowClosing(WindowEvent e) /关闭窗口事件的方法System.exit(0);); (3)编译并运行程序,会出现一个界面窗口如图1所示。它可以最大化、最小化,单击按钮可以关闭该窗口。2在窗口中添加组件(1)程序功能:在窗口中添加组件。(2)编写LX10_2.java 程序文件,源代码如下。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.Border;public class LX10_2 extends JFrame implements ActionListener JButton btn1, btn2;JTextField f,tf1,tf2;JTextArea Area;LX10_2() super(添加组件的窗口);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););setSize(450,250); /设置窗口大小setLocation(200,200);/设置窗口显示位置setFont(new Font(Arial,Font.PLAIN,12); /设置字体this.getContentPane().setLayout(new FlowLayout();Area=new JTextArea (6,40);Border border=BorderFactory.createEtchedBorder();Area.setBorder(border);tf1=new JTextField(10); tf2=new JTextField(10);btn1=new JButton(显示); btn2=new JButton(退出);f=new JTextField(20);getContentPane().add(Area); getContentPane().add(new Label(用户名);getContentPane().add(tf1); getContentPane().add(new Label(电话);getContentPane().add(tf2); getContentPane().add(f); getContentPane().add(btn1); getContentPane().add(btn2);tf1.addActionListener(this); tf2.addActionListener(this);btn1.addActionListener(this); btn2.addActionListener(this);show();public static void main(String args) new LX10_2 ();public void actionPerformed(ActionEvent e) Object s=e.getSource();if(s instanceof JButton)JButton bt=(JButton)s;if (bt=btn1)f.setText(你按下了“ + e.getActionCommand() + ”按钮);elsedispose();else if(s instanceof JTextField)JTextField tf=(JTextField)s;if (tf=tf1)Area.append(用户名:+tf1.getText()+n);elseArea.append(电 话:+tf2.getText()+n);(3)编译并运行程序,界面窗口如图2 所示。3.单选按钮单选按钮(JRadioButton)通常成组(Group)使用,即若干个单选按钮构成一组,并且每次只能有一个按钮被选中,适用于从多个备选选项中选择一项的场合(如图3所示)。从完成的功能来看,类似于不可编辑的组合框。import javax.swing.*;import java.awt.event.*;import java.awt.*;public class RadioButtonExamplepublic static void main(String args)RadioButtonFrame f=new RadioButtonFrame();f.setSize(300,200);f.show();class RadioButtonFrame extends JFrameprivate JLabel labPic=new JLabel();private ActionListener listener=new RadioButtonListener();private ButtonGroup group=new ButtonGroup();private JPanel p=new JPanel();public RadioButtonFrame()p.setLayout(new GridLayout(4,0);JRadioButton radMSSQL=createRadioButton(MS SQL Server);JRadioButton radOracle=createRadioButton(ORACLE Server);JRadioButton radMysql=createRadioButton(MySQL Server);radMSSQL.setSelected(true); /选中radMSSQL单选按钮labPic.setText(当前使用的数据库服务器为+radMSSQL.getText();p.add(labPic);this.getContentPane().add(p,BorderLayout.WEST);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public JRadioButton createRadioButton(String text)JRadioButton rb=new JRadioButton(text);rb.addActionListener(listener);group.add(rb);p.add(rb);return rb;class RadioButtonListener implements ActionListenerpublic void actionPerformed(ActionEvent e)labPic.setText(当前使用的数据库服务器为 +(JRadioButton)e.getSource().getText();【完成实验项目】1.如下图所示,用了三个文本框,第一个文本框给用户输入商品单价,第二个则是给用户输入商品数量,第三个用于显示总金额。2.制作如下图所示的界面,当用户点击单选按钮时,会在一个标签上显示出当前所选定的数据库服务器类型。三、实验结果与结论:(经调试正确的源程序(核心部分)和程序的运行结果)LX10_1.java程序执行结果如图1所示:图1LX10_2.java程序执行结果如图2所示:图2RadioButtonExample.java程序执行结果如图3所示:图31.如下图所示,用了三个文本框,第一个文本框给用户输入商品单价,第二个则是给用户输入商品数量,第三个用于显示总金额。Test10_1.java源程序如下: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.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; import javax.swing.text.Document; public class Test10_1extends JFrame implements ActionListener public static void main(String args) test2_1good = new test2_1(); JTextField goodPriceTF; JTextField goodNumTF; JTextField sumPriceTF; JLabel goodPriceTipsLabel; JLabel goodNumTipsLabel; Boolean canCal = false; public test2_1() super(商品价格计算器); setSize(435, 135); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(getOwner();FlowLayout flo = new FlowLayout(); setLayout(flo); JLabel goodPriceLabel = new JLabel(商品价格,JLabel.RIGHT); JLabel goodNumLabel = new JLabel(商品数量,JLabel.RIGHT); JLabel sumPriceLabel = new JLabel(商品总额,JLabel.RIGHT); goodPriceTipsLabel = new JLabel(请输入价格,JLabel.RIGHT);goodNumTipsLabel = new JLabel(请输入数量,JLabel.RIGHT); goodPriceTF = new JTextField(20);goodPriceTF.getDocument().addDocumentListener(new DocumentListener() public void removeUpdate(DocumentEvent e) changeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText(); public void insertUpdate(DocumentEvent e) changeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText(); public void changedUpdate(DocumentEvent e) changeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText(); ); goodNumTF = new JTextField(20); goodNumTF.getDocument().addDocumentListener(new DocumentListener() public void removeUpdate(DocumentEvent e) changeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText(); public void insertUpdate(DocumentEvent e) changeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText(); public void changedUpdate(DocumentEvent e) changeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText(); ); sumPriceTF = new JTextField(20); sumPriceTF.setEditable(false); JButton btn = new JButton(计算); btn.addActionListener(this); add(goodPriceLabel); add(goodPriceTF); add(goodPriceTipsLabel); add(goodNumLabel); add(goodNumTF); add(goodNumTipsLabel); add(sumPriceLabel); add(sumPriceTF); add(btn); setVisible(true); public void actionPerformed(ActionEvent event)if (event.getActionCommand().equals(计算) if (!canCal) showMessage(输入的数据不合法); return; double sum = Double.parseDouble(goodPriceTF.getText() *Double.parseDouble(goodNumTF.getText(); sumPriceTF.setText(Double.toString(sum); public Boolean isNumber(String s) if (s.length() = 0) return false; Boolean isNumber = true; char c = s.toCharArray(); for (int i = 0;i s.length();i+) if (Character.isDigit(ci) = false) isNumber = false; break; return isNumber; public void showMessage(String s) JOptionPane.showMessageDialog(null, s, 警告, JOptionPane.PLAIN_MESSAGE); public void changeTFTipsLabel(JLabel aLabel,String str) if (isNumber(str) = false) canCal = false; aLabel.setText(X ); else if (isNumber(goodPriceTF.getText() | isNumber(goodNumTF.getText() if (isNumber(goodPriceTF.getText() & isNumber(goodNumTF.getText() canCal = true; aLabel.setText( ); else if (str.length() = 0) canCal = false; if (aLabel = goodPriceTipsLabel) aLabel.setText(请输入价格); else aLabel.setText(请输入数量); Test10_1.java程序执行结果如图4,图5所示:图4图52.制作如下图所示的界面,当用户点击单选按钮时,会在一个标签上显示出当前所选定的数据库服务器类型。Test10_2.java源程序如下:import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class Test10_2 implements ActionListener private JFrame frmAsdfasdf; JLabel label; public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try Test10_2 window = new Test10_2(); window.frmAsdfasdf.setVisible(true); catch (Exception e) e.printStackTrace(); ); public Test10_2 () initialize(); private void initialize() frmAsdfasdf = new JFrame(); frmAsdfasdf.setTitle(数据库); frmAsdfasdf.setBounds(100, 100, 251, 301);frmAsdfasdf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmAsdfasdf.getContentPane().setLayout(new GridLayout(0, 1, 0, 0); JRadioButton rdbtnNewRadioButton_1 = new JRadioButton(SQL);frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_1);rdbtnNewRadioButton_1.addActionListener(this); JRadioButt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论