网络编程技术第二次课Java Swing组件代码.doc_第1页
网络编程技术第二次课Java Swing组件代码.doc_第2页
网络编程技术第二次课Java Swing组件代码.doc_第3页
网络编程技术第二次课Java Swing组件代码.doc_第4页
网络编程技术第二次课Java Swing组件代码.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

1)GuessNumber.java/*编写一个“猜数”程序,该程序随机在1到100的范围内选择一个供用户猜测的整数,然后该程序显示提示信息,要求用户输入一个1到100之间的整数,根据输入数偏大、偏小、正确,程序将显示不同的文本提示信息。*/public class GuessNumber extends JFrame implements ActionListener/监听者是包容事件源的容器JFrameint randInt=0;/存储系统产生的随机整数int userInt=0;/存储用户通过文本框输入的整数JLabel label1;JLabel label2;JTextField numJtf;/用于用于输入猜想数字的单行文本框JButton okBtn;/确定按钮public void init()setLayout(new FlowLayout();/设置为流布局方式randInt=(int)(Math.random()*100);/系统产生随机整数/System.out.println(randInt=+randInt);label1=new JLabel(请输入一个1到100之间的整数);numJtf=new JTextField(10);label2=new JLabel(猜猜看!);okBtn=new JButton(确定);/定义JFrame中包含的组件add(label1);add(numJtf);add(okBtn);add(label2);/将组件加入到JFrame容器中okBtn.addActionListener(this);/注册动作事件numJtf.addActionListener(this);/注册动作事件setSize(300,150);/设置JFrame容器尺寸setVisible(true);/设置JFrame容器可见setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/按下关闭按钮时关闭窗口public void eventHandle()/触发动作事件时的处理过程tryuserInt=Integer.parseInt(numJtf.getText().trim();/提出用户在文本框中的信息输入catch(NumberFormatException ex)/如果用户在文本框中没有输入或者输入的内容不是数值型数据所做的出来JOptionPane.showMessageDialog(this,请将您猜得的整数输入到文本框中!);/弹出对话框提示用户请将您猜得的整数输入到文本框中!numJtf.setText();numJtf.requestFocus();/文本框清空并使得文本框重新获得焦点if(userInt100)/用户输入的数据在0-100范围之外所做的处理JOptionPane.showMessageDialog(this,请输入0到100之间的整数!);numJtf.setText();numJtf.requestFocus();if(userIntrandInt)/用户输入的数值偏大label2.setText(您猜的数值偏大!);numJtf.requestFocus();else if(userInt=randInt)/用户猜对了label2.setText(恭喜您,猜对了!);public void actionPerformed(ActionEvent e)/*监听者可以是包容事件源的容器JFrame,直接实现动作事件接口ActionListener所需要实现的方法actionPerformed(ActionEvent e)*/if(e.getSource()=okBtn)/如果触发动作事件的事件源是确定按钮okBtneventHandle();else if(e.getSource()=numJtf)/如果触发动作事件的事件源是文本框numJtfeventHandle();public static void main(String args)new GuessNumber().init();2)GuessNumber1.javapublic class GuessNumber1 extends JFrame /监听者是一个内部类public void init()setLayout(new FlowLayout();/设置为流布局方式okBtn.addActionListener(new ActionEventHandler();/注册动作事件numJtf.addActionListener(new ActionEventHandler();/注册动作事件setSize(300,150);/设置JFrame容器尺寸setVisible(true);/设置JFrame容器可见setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/按下关闭按钮时关闭窗口class ActionEventHandler implements ActionListener/监听者是一个内部类ActionEventHandlerpublic void actionPerformed(ActionEvent e)/*监听者可以是包容事件源的容器JFrame,直接实现动作事件接口ActionListener所需要实现的方法actionPerformed(ActionEvent e)*/if(e.getSource()=okBtn)/如果触发动作事件的事件源是确定按钮okBtneventHandle();else if(e.getSource()=numJtf)/如果触发动作事件的事件源是文本框numJtfeventHandle();public void eventHandle()/触发动作事件时的处理过程public static void main(String args)new GuessNumber1().init();5)GuessNumber3.javapublic class GuessNumber3 extends JFrame / 监听者是一个匿名内部类的情况public void init() setLayout(new FlowLayout();/ 设置为流布局方式okBtn = new JButton(确定);/ 定义JFrame中包含的组件okBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (e.getSource() = okBtn) / 如果触发动作事件的事件源是确定按钮okBtneventHandle(); else if (e.getSource() = numJtf) / 如果触发动作事件的事件源是文本框numJtfeventHandle(););/ 使用匿名内部内注册动作事件numJtf.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if (e.getSource() = okBtn) / 如果触发动作事件的事件源是确定按钮okBtneventHandle(); else if (e.getSource() = numJtf) / 如果触发动作事件的事件源是文本框numJtfeventHandle(););/ 使用匿名内部内注册动作事件setSize(300, 150);/ 设置JFrame容器尺寸setVisible(true);/ 设置JFrame容器可见setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/ 按下关闭按钮时关闭窗口public void eventHandle() / 触发动作事件时的处理过程public static void main(String args) new GuessNumber3().init();4)FontStyleAndColor.java/*建立两个单选按钮组,其中一个单选按钮组对Label的字体颜色进行控制,另外一个单选按钮对Label的字体效果进行控制。同时还有一个“退出”按钮。*/public class FontStyleAndColor extends JFrame implements ItemListener,ActionListener /继承了动作事件和项目事件接口ButtonGroup bg1, bg2;/ 两个单选按钮组JRadioButton redColor, blueColor, greenColor;/ 红色,蓝色,绿色三个按钮JRadioButton fontStyle1, fontStyle2, fontStyle3;/ 普通,黑体,斜体三个单选按钮JButton exitBtn;JLabel label1, label2;JRadioButton tempColor, tempStyle;Color color;/颜色类public void init() setLayout(new FlowLayout();/ 第一组单选按钮bg1 = new ButtonGroup();redColor = new JRadioButton(红色, true);blueColor = new JRadioButton(蓝色, false);greenColor = new JRadioButton(绿色, false);bg1.add(redColor);bg1.add(blueColor);bg1.add(greenColor);/将三个单选加到单选按钮组ButtonGroup中add(redColor);add(blueColor);add(greenColor);/将三个单项按钮加到容器JFrame中/ 注册选择事件redColor.addItemListener(this);blueColor.addItemListener(this);greenColor.addItemListener(this);/给单选按钮注册项目事件label2 = new JLabel( );add(label2);/ 第二组单选按钮bg2 = new ButtonGroup();fontStyle1 = new JRadioButton(普通, true);fontStyle2 = new JRadioButton(黑体, false);fontStyle3 = new JRadioButton(斜体, false);fontStyle1.addItemListener(this);fontStyle2.addItemListener(this);fontStyle3.addItemListener(this);bg2.add(fontStyle1);bg2.add(fontStyle2);bg2.add(fontStyle3);add(fontStyle1);add(fontStyle2);add(fontStyle3);/ 字体变化的labellabel1 = new JLabel(看看我的字体是如何变化的!);add(label1);/ 注册选择事件exitBtn = new JButton(退出);exitBtn.addActionListener(this);/ add(okBtn);add(exitBtn);setSize(400, 100);setVisible(true);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e) if (e.getSource() = exitBtn) System.exit(0);public void itemStateChanged(ItemEvent e) / 提取lable1字体的属性Font oldFont = label1.getFont();String name = oldFont.getName();int size = oldFont.getSize();/ 改变字体颜色tempColor = (JRadioButton) (e.getItemSelectable();if (tempColor.getText().equals(红色) color = Color.RED; else if (tempColor.getText().equals(蓝色) color = Color.BLUE; else if (tempColor.getText().equals(绿色) color = Color.GREEN;label1.setForeground(color);/将字体颜色设置为新选定的字体颜色/ 改变字体型号tempStyle = (JRadioButton) (e.getItemSelectable();if (tempStyle.getText().equals(普通) label1.setFont(new Font(name, Font.PLAIN, size); else if (tempStyle.getText().equals(斜体) label1.setFont(new Font(name, Font.ITALIC, size); else if (tempStyle.getText().equals(黑体) label1.setFont(new Font(name, Font.BOLD, size);public static void main(String args) new FontStyleAndColor().init();5)TraceOfMouseAndKeyboardEvent.java/设计一个JFrame窗口,在这个Frame中能够追踪上述各种键盘和鼠标事件。public class TraceOfMouseAndKeyboardEvent extends JFrameHashMap h=new HashMap();/用来存放事件类型的字符串为key,以TextField组件为valueString event=focusGained,focusLost,keyPressed,keyReleased,keyTyped,mouseClicked,mouseEntered,mouseExited,mousePressed,mouseReleased,mouseDragged,mouseMoved;/需要在JFrame中追踪的各种事件MyButton b1=new MyButton(Color.blue,test1);/MyButton的两个实例MyButton b2=new MyButton(Color.red,test2);class MyButton extends Button/内部类,Button类的子类MyButtonvoid report(String field,String msg)(JTextField)h.get(field).setText(msg);/*根据HashMap的key值取出其对应的value-JTextField组件,并将对应JTextField组件的text设置为相应事件有关的参数信息*/FocusListener fl=new FocusListener()/匿名内部类,焦点事件处理public void focusGained(FocusEvent e)report(focusGained,e.paramString();/e.paramString()方法:返回和相应事件有关的参数信息/组件获得焦点的事件处理public void focusLost(FocusEvent e)report(focusLost,e.paramString();/组建失去焦点的事件处理;e.paramString():输出所触发事件的参数;KeyListener kl=new KeyListener()/匿名内部类,键盘事件处理public void keyPressed(KeyEvent e)report(keyPressed,e.paramString();/键盘按键被按下的事件处理public void keyReleased(KeyEvent e)report(keyReleased,e.paramString();/键盘按键被放开的事件处理public void keyTyped(KeyEvent e)report(keyTyped,e.paramString();/键盘按键被敲击的事件处理;MouseListener ml=new MouseListener()/匿名内部类,鼠标事件处理public void mouseClicked(MouseEvent e)report(mouseClicked,e.paramString();/鼠标单击事件public void mouseEntered(MouseEvent e)report(mouseEntered,e.paramString();/鼠标进入事件public void mouseExited(MouseEvent e)report(mouseExited,e.paramString();/鼠标离开事件public void mousePressed(MouseEvent e)report(mousePressed,e.paramString();/鼠标按钮按下事件public void mouseReleased(MouseEvent e)report(mouseReleased,e.paramString();/鼠标按钮松开事件;MouseMotionListener mml=new MouseMotionListener()/鼠标移动事件处理public void mouseDragged(MouseEvent e)report(mouseDragged,e.paramString();/鼠标拖动一个组件的事件处理public void mouseMoved(

温馨提示

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

评论

0/150

提交评论