《实验8-图形界面程序设计(共8页).doc》由会员分享,可在线阅读,更多相关《实验8-图形界面程序设计(共8页).doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上山西大学计算机与信息技术学院实验报告姓 名 郭彩峰学 号 专业班级软一课程名称 Java实验实验日期2014.12.11成 绩指导教师 批改日期实验名称实验 8 图形界面程序设计一、实验目的掌握常用GUI控制组件及其事件处理。二、实验内容1编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。程序代码:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChangeGUI extends JFrame private JButton bu
2、tton; private JLabel label; public ChangeGUI() super(Say Hello); JPanel panel = new JPanel(); JPanel panel2 = new JPanel(); setLayout(new GridLayout(2,1,0,5); button = new JButton(OK); button.setBackground(Color.gray); button.setForeground(Color.RED); panel.add(button); button.addActionListener(new
3、OKActionListener(); label = new JLabel(你好); label.setForeground(Color.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
4、 void main(String args) ChangeGUI change = new ChangeGUI(); change.setSize(200, 100); change.setVisible(true); change.setLocationRelativeTo(null); change.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 运行结果贴图: 2编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。程序代码:import java.awt.*; i
5、mport javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class ShowText extends JFrame private JTextField text1; private JTextArea text2; public ShowText() super(Tetx Show); JPanel p1 = new JPanel(); p1.setBackground(Color.BLUE); p1.setBorder(new TitledBorder(文本框); text1 = n
6、ew JTextField(10); text1.addKeyListener(new TextListener(); p1.add(text1); JPanel p2 = new JPanel(); p2.setBackground(Color.YELLOW); p2.setBorder(new TitledBorder(文本区域); text2 = new JTextArea(原文本,10,10); text2.setLineWrap(true); text2.setEditable(false); p2.add(text2); setLayout(new GridLayout(2,1,0
7、,5); add(p1); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(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)
8、text2.setText(text1.getText(); public void keyTyped(KeyEvent e) if(e.getKeyChar()=KeyEvent.VK_ENTER) text2.setText(null); public static void main(String args) JFrame frame = new ShowText(); 运行结果贴图: 3编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为青色,未选中时为灰色。程序代码:import java.awt.*; import javax.swing.*; import ja
9、va.awt.event.*; public class ChangeButtonColor extends JFrame private JButton button; private JCheckBox checkBox; public ChangeButtonColor() super(改变按钮颜色); JPanel p1 = new JPanel(); p1.setBackground(Color.RED); setLayout(new GridLayout(2,1); button = new JButton(Hello); button.setSize(20, 20); butto
10、n.setBackground(Color.GREEN); p1.add(button); JPanel p2 = new JPanel(); p2.setBackground(Color.CYAN); checkBox = new JCheckBox(); checkBox.addItemListener(new checkBoxListener(); p2.add(checkBox); add(p1); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(null); setDefaultClose
11、Operation(JFrame.EXIT_ON_CLOSE); private class checkBoxListener implements ItemListener public void itemStateChanged(ItemEvent e) if(checkBox.isSelected() button.setBackground(Color.CYAN); else button.setBackground(Color.GREEN); public static void main(String args) ChangeButtonColor b = new ChangeBu
12、ttonColor(); 运行结果贴图: 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(Acti
13、onEvent e) who.setText(Button 2); 程序代码:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowButtonText extends JFrame private JButton b1; private JButton b2; private JLabel label; public ShowButtonText() super(显示选中按钮信息); setLayout(new GridLayout(2,1); JPanel p1 = new JP
14、anel(); p1.setBackground(Color.WHITE); label = new JLabel(标签); label.setSize(20, 10); label.setBackground(Color.BLUE); p1.add(label); add(p1); JPanel p2 = new JPanel(); p2.setBackground(Color.WHITE); b1 = new JButton(你好); b2 = new JButton(再见); ButtonListener b = new ButtonListener(); b1.addActionLis
15、tener(b); b2.addActionListener(b); p2.add(b1); p2.add(b2); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSou
16、rce()=b1) label.setText(b1.getText(); else if(e.getSource()=b2) label.setText(b2.getText(); public static void main(String args) ShowButtonText s = new ShowButtonText(); 运行结果贴图: 5编程确定当前鼠标的位置坐标。程序代码:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LocateMouse extends JFr
17、ame private JButton location; public 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.get
18、X()+,+e.getY()+); ); setSize(300,200); setLocationRelativeTo(null); setVisible(true); location.setBackground(Color.RED); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) LocateMouse mouse = new LocateMouse(); 运行结果贴图: 6编程使用BorderLayout布局方式放置5个按钮。程序代码:import java.aw
19、t.*; import javax.swing.*; import java.awt.event.*; public class TestBorderLayout extends JFrame private JButton nButton = new JButton(北); private JButton sButton = new JButton(南); private JButton wButton = new JButton(西); private JButton eButton = new JButton(东); private JButton cButton = new JButt
20、on(中); public TestBorderLayout() setLayout(new BorderLayout(5,5); add(nButton,BorderLayout.NORTH); add(sButton,BorderLayout.SOUTH); add(wButton,BorderLayout.WEST); add(eButton,BorderLayout.EAST); add(cButton,BorderLayout.CENTER); nButton.setBackground(Color.PINK); sButton.setBackground(Color.PINK);
21、wButton.setBackground(Color.PINK); eButton.setBackground(Color.PINK); cButton.setBackground(Color.PINK); public static void main(String args) TestBorderLayout t = new TestBorderLayout(); t.setSize(250,200); t.setVisible(true); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setLocationRelativeTo
22、(null); 运行结果贴图:7. 编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。移动到边界时从界面另一侧出现。移动过程中显示另一个图片,停止时恢复原来的图片。程序代码:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MoveImage extends JFrame private ImageIcon oneIcon = new ImageIcon(image/happy.jpg); private ImageIcon twoIcon = new ImageIcon(im
23、age/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(la
24、bel); p.setBackground(Color.PINK); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class PanelListener implements KeyListener public void keyPressed(KeyEvent e) label.setIcon(twoIcon); int x = label.getX(); int y = label.getY(); int w = p.getWidth(); int h = p.getHeight();
25、if(e.getKeyCode()=KeyEvent.VK_UP) -y; if(y=h) y = 0; label.setBounds(x, y, 100, 100); else if(e.getKeyCode()=KeyEvent.VK_LEFT) x-; if(x=w) x = 0; label.setBounds(x, y, 100, 100); public void keyReleased(KeyEvent e) if(e.getKeyCode()=KeyEvent.VK_UP|e.getKeyCode()=KeyEvent.VK_DOWN |e.getKeyCode()=KeyEvent.VK_LEFT|e.getKeyCode()=KeyEvent.VK_RIGHT) label.setIcon(oneIcon); public void keyTyped(KeyEvent e) public static void main(String args) MoveImage m = new MoveImage(); 运行结果贴图: 专心-专注-专业