




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、山西大学计算机与信息技术学院实验报告姓 名学 号 专业班级计算机科学与技术课程名称 Java实验实验日期2014/5/29成 绩指导教师 陈千批改日期实验名称实验 8 图形界面程序设计一、实验目的掌握常用GUI控制组件及其事件处理。二、实验内容1编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。程序代码:import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax
2、.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class ChangeGUI extends JFrame /* * */private static final long serialVersionUID = 1L;private JButton button;private JLabel label;public ChangeGUI() super("Say Hello");JPanel panel = new JPa
3、nel();JPanel panel2 = new JPanel();setLayout(new GridLayout(2, 1, 0, 5);button = new JButton("OK");button.setBackground(Color.ORANGE);button.setForeground(Color.RED);panel.add(button);button.addActionListener(new OKActionListener();label = new JLabel("你好");label.setForeground(Col
4、or.BLUE);panel2.add(label);add(panel2);add(panel);private class OKActionListener implements ActionListener public void actionPerformed(ActionEvent e) if (label.getText() = "你好") label.setText("再见"); else label.setText("你好");public static void main(String args) ChangeGUI
5、 change = new ChangeGUI();change.setSize(200, 100);change.setVisible(true);change.setLocationRelativeTo(null);change.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);运行结果贴图:图一2编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。程序代码:import java.awt.Color;import java.awt.GridLayout;import j
6、ava.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.border.TitledBorder;public class ShowText extends JFrame /* * */private static final long serialVersionUID = 1L;p
7、rivate JTextField text1;private JTextArea text2;public ShowText() super("Tetx Show");JPanel p1 = new JPanel();p1.setBackground(Color.PINK);p1.setBorder(new TitledBorder("文本框");text1 = new JTextField(10);text1.addKeyListener(new TextListener();p1.add(text1);JPanel p2 = new JPanel(
8、);p2.setBackground(Color.PINK);p2.setBorder(new TitledBorder("文本区域");text2 = new JTextArea("原文本", 10, 10);text2.setLineWrap(true);text2.setEditable(false);p2.add(text2);setLayout(new GridLayout(2, 1, 0, 5);add(p1);add(p2);setSize(200, 200);setVisible(true);this.setLocationRelativ
9、eTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);private class TextListener implements KeyListener public void keyPressed(KeyEvent e) public void keyReleased(KeyEvent e) if (e.getKeyChar() != KeyEvent.VK_ENTER)text2.setText(text1.getText();public void keyTyped(KeyEvent e) if (e.getKeyChar()
10、= KeyEvent.VK_ENTER)text2.setText(null);public static void main(String args) JFrame frame = new ShowText();运行结果贴图:图二3编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为青色,未选中时为灰色。程序代码:import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swi
11、ng.JButton;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JPanel;public class ChangeButtonColor extends JFrame /* * */private static final long serialVersionUID = 1L;private JButton button;private JCheckBox checkBox;public ChangeButtonColor() super("改变按钮颜色");JPan
12、el p1 = new JPanel();p1.setBackground(Color.PINK);setLayout(new GridLayout(2, 1);button = new JButton("Hello");button.setSize(20, 20);button.setBackground(Color.GRAY);p1.add(button);JPanel p2 = new JPanel();p2.setBackground(Color.PINK);checkBox = new JCheckBox();checkBox.addItemListener(ne
13、w checkBoxListener();p2.add(checkBox);add(p1);add(p2);setSize(200, 200);setVisible(true);this.setLocationRelativeTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);private class checkBoxListener implements ItemListener public void itemStateChanged(ItemEvent e) if (checkBox.isSelected()button.se
14、tBackground(Color.CYAN);elsebutton.setBackground(Color.GRAY);public static void main(String args) ChangeButtonColor b = new ChangeButtonColor();运行结果贴图:图三4编程包含两个按钮和一个标签,将发生单击事件的按钮上的文本信息显示在标签中。提示:关键代码如下: b1.addActionListener(new B1();
15、160; b2.addActionListener(new B2(); class B1 implements ActionListener public void actionPerformed(ActionEvent e)
16、0; who.setText("Button 1"); class B2 implements ActionListener
17、 public void actionPerformed(ActionEvent e) who.setText("Button 2");
18、160; 程序代码:import java.awt.Color;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.JLabel;import javax.swing.JPanel;public class ShowButtonText extends
19、 JFrame private JButton b1;private JButton b2;private JLabel label;public ShowButtonText() super("显示选中按钮信息");setLayout(new GridLayout(2, 1);JPanel p1 = new JPanel();p1.setBackground(Color.PINK);label = new JLabel("标签");label.setSize(20, 10);label.setBackground(Color.BLUE);p1.add(
20、label);add(p1);JPanel p2 = new JPanel();p2.setBackground(Color.PINK);b1 = new JButton("你好");b2 = new JButton("再见");ButtonListener b = new ButtonListener();b1.addActionListener(b);b2.addActionListener(b);p2.add(b1);p2.add(b2);add(p2);setSize(200, 200);setVisible(true);this.setLoca
21、tionRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);private class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) if (e.getSource() = b1)label.setText(b1.getText();else if (e.getSource() = b2)label.setText(b2.getText();public static void main(
22、String args) ShowButtonText s = new ShowButtonText();运行结果贴图:图四5编程确定当前鼠标的位置坐标。程序代码:import java.awt.Color;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import javax.swing.JButton;import javax.swing.JFrame;public class LocateMouse extends JFrame private JButton location;pub
23、lic LocateMouse() super("寻找鼠标");location = new JButton("显示鼠标位置");location.setSize(20, 10);add(location);location.addMouseMotionListener(new MouseMotionListener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) location.setText("鼠标现在位于(" + e.g
24、etX() + "," + e.getY() + ")"););setSize(300, 200);setLocationRelativeTo(null);setVisible(true);location.setBackground(Color.PINK);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public static void main(String args) LocateMouse mouse = new LocateMouse();运行结果贴图:图五6编程使用BorderLayout布局
25、方式放置5个按钮。程序代码:import java.awt.BorderLayout;import java.awt.Color;import javax.swing.JButton;import javax.swing.JFrame;public class TestBorderLayout extends JFrame private JButton nButton = new JButton("北部");private JButton sButton = new JButton("南部");private JButton wButton = new
26、 JButton("西部");private JButton eButton = new JButton("东部");private JButton cButton = new JButton("中部");public TestBorderLayout() setLayout(new BorderLayout(5, 5);add(nButton, BorderLayout.NORTH);add(sButton, BorderLayout.SOUTH);add(wButton, BorderLayout.WEST);add(eButto
27、n, BorderLayout.EAST);add(cButton, BorderLayout.CENTER);nButton.setBackground(Color.PINK);sButton.setBackground(Color.PINK);wButton.setBackground(Color.PINK);eButton.setBackground(Color.PINK);cButton.setBackground(Color.PINK);public static void main(String args) TestBorderLayout t = new TestBorderLa
28、yout();t.setSize(250, 200);t.setVisible(true);t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);t.setLocationRelativeTo(null);运行结果贴图:图六7. 编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。移动到边界时从界面另一侧出现。移动过程中显示另一个图片,停止时恢复原来的图片。程序代码:import java.awt.Color;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;im
29、port javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class MoveImage extends JFrame private ImageIcon oneIcon = new ImageIcon("image/happy.jpg");private ImageIcon twoIcon = new ImageIcon("image/hello.jpg");private JLabel label;JPanel p;public MoveImage() super("Image Move");setSize(500, 500);setLocationRelativeTo(null);label = new JLabel(oneIcon);p = new JPanel();setContentPane(p);p.setLayout(null);this.addKeyListener(new PanelListener();label.setBounds(0, 0, 100, 100);p.add(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论