实验10 图形界面.docx
实验10 图形界面班级: 姓名: 学号: 成绩 实验10(12,13,14):GUI程序设计 实验目的: 1. 掌握GUI开发的基本过程 2. 能够根据要求,选择合适的布局管理器和相应组件完成页面的布局 3. 理解事件响应机制,掌握事件编程 4. 了解MVC思想 实验数据记录及分析 1. 准备两个相同大小的图像文件。定义一个界面,放置一个带图标的按钮,当每次按下按钮时,按钮上的图标都会更换为另一个图像文件。 2. import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import .URL; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class PictureTran extends JFrame JButton button; int i = 0; Icon icon1, icon2; URL url1, url2; PictureTran setTitle("图片切换"); setBounds(100, 100, 300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); url1 = this.getClass.getResource("/images/Bird.gif"); / 图片放在src文件夹下images子文件中 url2 = this.getClass.getResource("/images/Cat.gif"); / 图片放在src文件夹下images子文件中 / url1 = this.getClass.getResource("/Bird.gif"); / 图片放在res资源源文件夹中 / url2 = this.getClass.getResource("/Cat.gif"); / 图片放在res资源源文件夹中 icon1 = new ImageIcon(url1); icon2 = new ImageIcon(url2); button = new JButton(icon1); add(button, BorderLayout.CENTER); button.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) i = (i + 1) % 2; if (i = 1) button.setIcon(icon2); else button.setIcon(icon1); ); public static void main(String args) new PictureTran.setVisible(true); 3. 设计一个有九个格子的人机交互游戏,准备两张小图片,一张为鱼,一张为猫,你来控制猫在哪个格子出现,程序控制鱼在哪个格子出现,两个动物出现在一个格子里,游戏结束。 import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import .URL; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class JGGFrame extends JFrame JButton bs = new JButton9; URL url1, url2; public JGGFrame setTitle("九宫格"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(200, 200, 300, 300); Container p = getContentPane; p.setLayout(new GridLayout(3, 0, 5, 5); url1 = this.getClass.getResource("/images/Cat.gif"); url2 = this.getClass.getResource("/images/Dog.gif"); for (int i = 0; i < bs.length; i+) bsi = new JButton("" + (i + 1); p.add(bsi); bsi.addActionListener(new ActionListener Override public void actionPerformed(ActionEvent e) for (int j = 0; j < bs.length; j+) bsj.setIcon(null); int rand = new Random.nextInt(9); bsrand.setIcon(new ImageIcon(url2); JButton b = (JButton) (e.getSource); b.setIcon(new ImageIcon(url1); if (b = bsrand) JOptionPane.showMessageDialog(null, "猫捉到狗!"); ); public static void main(String args) new JGGFrame.setVisible(true); 4. 请分别用纯代码编程实现如下的登陆框及计算器:用户名和密码设定为test及1234 登陆框: 计算器: (1) 纯代码编程 import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class LoginDialog extends JDialog JTextField name; JPasswordField pass; public LoginDialog setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setTitle("登录框"); setBounds(100, 100, 300, 150); Container c = getContentPane; c.setLayout(new GridLayout(3, 0, 5, 5); JLabel label1 = new JLabel("用户名", SwingConstants.CENTER); name = new JTextField; JLabel label2 = new JLabel("密 码", SwingConstants.CENTER); pass = new JPasswordField; JButton ok = new JButton("确定"); JButton cancel = new JButton("取消"); ok.addActionListener(new ActionListener Override public void actionPerformed(ActionEvent arg0) String n = name.getText; String p = new String(pass.getPassword); if (n.equals("test") && p.equals("123") LoginDialog.this.dispose; / 创建计算器框并显示; new JSQFrame.setVisible(true); else JOptionPane.showMessageDialog(null, "用户名或密码错误!"); pass.setText(""); pass.requestFocus; ); cancel.addActionListener(new ActionListener Override public void actionPerformed(ActionEvent arg0) System.exit(0); ); c.add(label1); c.add(name); c.add(label2); c.add(pass); c.add(ok); c.add(cancel); public static void main(String args) new LoginDialog.setVisible(true); import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; public class JSQFrame extends JFrame JRadioButton rbs; ButtonGroup bg = new ButtonGroup; JComboBox<Integer> cb; JSpinner sp; JLabel result; public JSQFrame setTitle("计算器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(200, 300, 400, 300); Container c = getContentPane; c.setLayout(new BorderLayout); JPanel p1, p2, p3; p1 = new JPanel; p1.setLayout(new GridLayout(1, 0, 3, 3); rbs = new JRadioButton4; rbs0 = new JRadioButton("+"); rbs1 = new JRadioButton("-"); rbs2 = new JRadioButton("*"); rbs3 = new JRadioButton("/"); rbs2.setSelected(true); for (int i = 0; i < rbs.length; i+) p1.add(rbsi); rbsi.setActionCommand(rbsi.getText); bg.add(rbsi); p2 = new JPanel; p2.setLayout(new GridLayout(2, 2, 5, 5); JLabel label1 = new JLabel("操作数1:", SwingConstants.CENTER); cb = new JComboBox<Integer> for (int i = 0; i <= 10; i+) cb.addItem(i * 10); cb.setSelectedIndex(5); JLabel label2 = new JLabel("操作数2:", SwingConstants.CENTER); SpinnerNumberModel model = new SpinnerNumberModel(50, 0, 100, sp = new JSpinner(model); p2.add(label1); p2.add(cb); p2.add(label2); p2.add(sp); p3 = new JPanel; JButton compute = new JButton("计算"); compute.addActionListener(new ActionListener public void actionPerformed(ActionEvent arg0) int num1 = Integer.parseInt( (String) cb.getSelectedItem); int num2 = (Integer) sp.getValue; 1); ); double r = 0.0; switch (bg.getSelection.getActionCommand) case "+": r = num1 + num2; break; case "-": r = num1 - num2; break; case "*": r = num1 * num2; break; case "/": r = (double) num1 / num2; break; default: break; result.setText("" + r); result = new JLabel("0.0"); p3.add(compute); p3.add(result); c.add(p1, BorderLayout.NORTH); c.add(p2); c.add(p3, BorderLayout.SOUTH); public static void main(String args) new JSQFrame.setVisible(true); 日期: 年 月 日