![实验8 图形界面程序设计_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/4bef48b1-96af-4c3b-a375-9c35db8dd0bc/4bef48b1-96af-4c3b-a375-9c35db8dd0bc1.gif)
![实验8 图形界面程序设计_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/4bef48b1-96af-4c3b-a375-9c35db8dd0bc/4bef48b1-96af-4c3b-a375-9c35db8dd0bc2.gif)
![实验8 图形界面程序设计_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/4bef48b1-96af-4c3b-a375-9c35db8dd0bc/4bef48b1-96af-4c3b-a375-9c35db8dd0bc3.gif)
![实验8 图形界面程序设计_第4页](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/4bef48b1-96af-4c3b-a375-9c35db8dd0bc/4bef48b1-96af-4c3b-a375-9c35db8dd0bc4.gif)
![实验8 图形界面程序设计_第5页](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/4bef48b1-96af-4c3b-a375-9c35db8dd0bc/4bef48b1-96af-4c3b-a375-9c35db8dd0bc5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、山西大学计算机与信息技术学院实验报告姓 名 学 号专业班级课程名称 Java实验实验日期成 绩指导教师 批改日期实验名称实验 8 图形界面程序设计一、实验目的掌握常用GUI控制组件及其事件处理。二、实验内容1编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。程序代码:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class JButtonAndJLabel ex
2、tends JFrame private JButton click = new JButton(Click);private JLabel label = new JLabel();int a = 1;int b = 0;public JButtonAndJLabel() setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20);add(click);add(label);click.addActionListener(new ButtonListener();private class ButtonListener implements Act
3、ionListener public void actionPerformed(ActionEvent e) String s1 = 你好!;String s2 = 再见!;label.setText(a = b ? s1 : s2);int t = a;a = b;b = t;public static void main(String args) JButtonAndJLabel frame = new JButtonAndJLabel();frame.setTitle(Click);frame.setSize(200, 100);frame.setLocationRelativeTo(n
4、ull);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);运行结果贴图:2编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。程序代码:import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;final class JTextFieldAndJ
5、TextAreaSample extends JFrame public String getTitle() return JTextFieldAndJTextAreaSample;static private final Dimension size = new Dimension(600, 400);public Dimension getPreferredSize() return size;public Dimension getMaximumSize() return size;public Dimension getMinimumSize() return size;public
6、Dimension getSize() return size;private static JTextField textField;private static JTextArea textArea;private static JLabel positionLabel0 = new JLabel() public Dimension getPreferredSize() return new Dimension(300, 35);public Dimension getMinimumSize() return new Dimension(300, 35);public Dimension
7、 getSize() return new Dimension(300, 35);private void init() textField = new JTextField() public Color getForeground() return Color.blue; public boolean lineWrap() return true; ; textArea = new JTextArea(3,20) public Color getForeground() return Color.magenta; public boolean isEditable() return fals
8、e; ; private void attachListeners() setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);textField.getDocument().addDocumentListener(new TextFieldChangeListener();private void doLay() setLayout(new BorderLayout();add(textField,BorderLayout.NORTH);add(textArea,BorderLayout.CENTER);pack();setVisible(true);p
9、rivate static class TextFieldChangeListener implements DocumentListener public void changedUpdate(DocumentEvent e) echo(e.getDocument();public void insertUpdate(DocumentEvent e) echo(e.getDocument();public void removeUpdate(DocumentEvent e) echo(e.getDocument();private static void echo(Document d) t
10、ry textArea.setText(d.getText(0, d.getLength(); catch (BadLocationException e1) e1.printStackTrace();JTextFieldAndJTextAreaSample() throws HeadlessException init();attachListeners();doLay();public static void main(String args) new JTextFieldAndJTextAreaSample();运行结果贴图:3编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮
11、的背景色为青色,未选中时为灰色。程序代码:import java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class ButtonColor extends JFrame private JRadioButton radioButton = new JRadioButton(RadioButton);private static JButton button = n
12、ew JButton(Button);int a = 1;int b = 0;public ButtonColor() setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20);add(radioButton);add(button);radioButton.addActionListener(new ButtonListener();private class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) button.set
13、Background(a b ? Color.cyan : Color.gray);int t = a;a = b;b = t;public static void main(String args) ButtonColor frame = new ButtonColor();button.setBackground(Color.gray);frame.setTitle(Click);frame.pack();frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.
14、setVisible(true);运行结果贴图:4编程包含两个按钮和一个标签,将发生单击事件的按钮上的文本信息显示在标签中。提示:关键代码如下: b1.addActionListener(new B1(); b2.addActionListener(new B2(); class B1 implements ActionListener public void actionPerformed(ActionEvent e) who.setText(Button 1); class B2 implements ActionListener public void actionPerformed(A
15、ctionEvent e) who.setText(Button 2); 程序代码: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;public class JButtonAndJLabelText extends JFrame private JButton b1 = new JButton
16、(b1);private JButton b2 = new JButton(b2);private JLabel who = new JLabel();public JButtonAndJLabelText() setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20);who.setSize(80, 20);add(b1);add(who);add(b2);b1.addActionListener(new B1();b2.addActionListener(new B2();private class B1 implements ActionLis
17、tener public void actionPerformed(ActionEvent e) who.setText(Button 1);private class B2 implements ActionListener public void actionPerformed(ActionEvent e) who.setText(Button 2);public static void main(String args) JButtonAndJLabelText frame = new JButtonAndJLabelText();frame.setTitle(ClickButton);
18、frame.setSize(240, 100);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);运行结果贴图:5编程确定当前鼠标的位置坐标。程序代码:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MousePosition extends JFrame private JLabel jbl = new JLabel();
19、private JPanel p = new JPanel();public static void main(String args) MousePosition frame = new MousePosition();frame.setSize(300,300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);public MousePosition()p.add(jbl);add(p);p.addMouseMotionListener(new MouseMotionAdapter()p
20、ublic void mouseMoved(MouseEvent e) int x = e.getX(); int y = e.getY(); jbl.setBounds(x,y,60,20); jbl.setText(+x+,+y+););运行结果贴图:6编程使用BorderLayout布局方式放置5个按钮。程序代码:import java.awt.BorderLayout;import javax.swing.*;public class BorderLayoutButton extends JFrame public BorderLayoutButton() this.setLayout
21、(new BorderLayout(20, 20);add(new JButton(Button1), BorderLayout.NORTH);add(new JButton(Button2), BorderLayout.WEST);add(new JButton(Button3), BorderLayout.CENTER);add(new JButton(Button4), BorderLayout.EAST);add(new JButton(Button5), BorderLayout.SOUTH);public static void main(String args) BorderLa
22、youtButton frame = new BorderLayoutButton();frame.setTitle(BorderLayoutButton);frame.setSize(300, 200);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);运行结果贴图:7. 编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。移动到边界时从界面另一侧出现。移动过程中显示另一个图片,停止时恢复原来的图片。程序代码:imp
23、ort javax.swing.*;import java.awt.*;import java.awt.event.*;public class Move extends JFrame private Icon icon1 = new ImageIcon(E:/Ok1.jpg); private Icon icon2 = new ImageIcon(E:/Ok2.jpg); private JPanel p = new JPanel(); private JLabel jlb; public static void main(String args) Move frame = new Move
24、();frame.setSize(500,500);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true); public Move() jlb = new JLabel(icon1); jlb.setBounds(0,0,30,30); p.add(jlb); add(p); addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) jlb.setIcon(icon2); int x = jlb.getX(); int y
25、 = jlb.getY(); int w = p.getWidth(); int h = p.getHeight(); switch(e.getKeyCode() case KeyEvent.VK_DOWN: y+=10; jlb.setBounds(x,y,30,30); if(y=h) y=0; y+=10; jlb.setBounds(x,y,30,30); break; case KeyEvent.VK_UP: y-=100; if(y=0) y=h; jlb.setBounds(x,y,30,30); else jlb.setBounds(x,y,30,30); break; cas
26、e KeyEvent.VK_LEFT: x-=100; if(x=w) x=0; jlb.setBounds(x,y,30,30); else jlb.setBounds(x,y,30,30); break; default: int i= 0; public void keyReleased(KeyEvent e) jlb.setIcon(icon1); ); 运行结果贴图:停止运动:运动过程中:课后作业P344 12.1 12.3 12.4 2.5 12.7 12.12 P426 15.1 15.3P455 16.2 16.3 16.5 16.6 16.712.1java.awt.Componen是Java GUI组件类的根;容器类是Component的子类;JCompon
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《大学物理(上册)》课件-第1章
- 2025-2030全球车辆燃油油位计行业调研及趋势分析报告
- 2025-2030全球电积铜行业调研及趋势分析报告
- 2025年全球及中国直接空气捕获和储存(DACS)行业头部企业市场占有率及排名调研报告
- 2025-2030全球多层土壤传感器行业调研及趋势分析报告
- 2025年全球及中国阻燃塑料薄膜和片材行业头部企业市场占有率及排名调研报告
- 2025-2030全球医用手指康复训练仪行业调研及趋势分析报告
- 2025-2030全球化学谷物熏蒸剂行业调研及趋势分析报告
- 2025年全球及中国智慧教育公共服务平台行业头部企业市场占有率及排名调研报告
- 2025年全球及中国工业胶囊填充设备行业头部企业市场占有率及排名调研报告
- 2025年度院感管理工作计划(后附表格版)
- 励志课件-如何做好本职工作
- 化肥销售工作计划
- 2024浙江华数广电网络股份限公司招聘精英18人易考易错模拟试题(共500题)试卷后附参考答案
- 2024年山东省济南市中考英语试题卷(含答案解析)
- 2024年社区警务规范考试题库
- 2025中考英语作文预测:19个热点话题及范文
- 第10讲 牛顿运动定律的综合应用(一)(讲义)(解析版)-2025年高考物理一轮复习讲练测(新教材新高考)
- 静脉治疗护理技术操作标准(2023版)解读 2
- 2024年全国各地中考试题分类汇编(一):现代文阅读含答案
- GB/T 30306-2024家用和类似用途饮用水处理滤芯
评论
0/150
提交评论