《Java基础入门第2版》--黑马程序员 课后答案及其详解 第8章 GUI(图形用户接口)

一、填空题

1、 GUI
2、 事件监听器
3、Swing
4、 WindowListener、windowClosing(WindowEvent e)
5、	JMenuBar、JMenu、JMenuItem

二、判断题

1、对  2、错   3、对  4、错  5、错

三、选择题

1、B    2、ABD     3、D     4、ABD     5、C  

四、简答题

1、(1)通过实现XxxListener接口或者继承XxxAdapter类实现一个事件监听器类,并对处理监听动作的方法进行重写
(2)创建事件源对象和事件监听器对象
(3)调用事件源的addXxxLisntener()方法,为事件源注册事件监听器对象

2、Swing工具在AWT的基础上提供了8种布局管理器,分别为BorderLayout(边界布局管理器)、BoxLayout(箱式布局管理器)、CardLayout(卡片布局管理器)、FlowLayout(流式布局管理器)、GridBagLayout(网格包布局管理器)、GridLayout(网格布局管理器)、GroupLayout(分组布局管理器)和SpringLayout(弹性布局管理器)。

3、(1)事件源(Event Source):事件发生的场所,通常就是产生事件的组件,例如窗口、按钮、菜单等。
(2)事件对象(Event):封装了GUI组件上发生的特定事件(通常就是用户的一次操作)。
(3)监听器(Listener):负责监听事件源上发生的事件,并对各种事件做出相应处理的对象(对象中包含事件处理器)。

五、编程题

1.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyMouseHandler extends JFrame {
    
    
	public MyMouseHandler() {
    
    
		final JLabel label = new JLabel("此处显示鼠标右键点击的坐标");
		label.setOpaque(true);
		label.setBackground(Color.PINK);
		this.add(label, BorderLayout.NORTH);
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addMouseListener(new MouseAdapter() {
    
    
			public void mouseClicked(MouseEvent e) {
    
    
				if (e.getButton() == MouseEvent.BUTTON1) {
    
    
					int x = e.getX();
					int y = e.getY();
					String banner = "鼠标当前点击位置的坐标是" + x + "," + y;
					label.setText(banner);
				}
			}
		});
		this.setVisible(true);
	}
	public static void main(String[] args) {
    
    
		new MyMouseHandler();
	}
}
2.import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Information extends JFrame {
    
    
	// 窗口NORTH部的JPanel面板 
	private JPanel panel = new JPanel();
	// 爱好标签
	private JLabel lb1 = new JLabel("兴趣");
	// 三个表示爱好的JCheckBox复选框
	private JCheckBox cb1 = new JCheckBox("羽毛球");
	private JCheckBox cb2 = new JCheckBox("乒乓球");
private JCheckBox cb3 = new JCheckBox("唱歌");
	// 性别标签
	private JLabel lb2 = new JLabel("性别");
	// 表示性别的JRadioButton单选框
	private JRadioButton rb1 = new JRadioButton("男");
	private JRadioButton rb2 = new JRadioButton("女");
	// ButtonGroup添加JRadioButton,实现单选功能
	private ButtonGroup bg = new ButtonGroup();
	// 文本域组件
	private JTextArea area = new JTextArea();
	// 窗口CENTER部的JScrollPane面板,其中放置area文本域 
	private JScrollPane pane = new JScrollPane(area);
	// Set集合存放选中的兴趣
	private Set<String> hobbies = new HashSet<String>();
	// gender选中的性别
	private String gender = "";
	// JCheckBox复选框的事件监听器
	private ActionListener listener1 = new ActionListener() {
    
    
		public void actionPerformed(ActionEvent e) {
    
    
			JCheckBox cb = (JCheckBox) e.getSource();
			// 选中的复选框把文本添加到Set集合中
			if (cb.isSelected()) {
    
    
				hobbies.add(cb.getText());
			//  反之从集合中移除
			} else {
    
    
				hobbies.remove(cb.getText());
			}
			print();
		}
	};
	// JRadioButton单选框的事件监听器
	private ActionListener listener2 = new ActionListener() {
    
    
		public void actionPerformed(ActionEvent e) {
    
    
			JRadioButton jb = (JRadioButton) e.getSource();
			gender = jb.getText();
			print();
		}
	};
    // 打印方法
	private void print() {
    
    
		// 清空文本域
		area.setText("");
		// 如果Set集合中有元素,打印兴趣
		if (hobbies.size() > 0)
area.append("你的兴趣爱好有: ");
		Iterator<String> it = hobbies.iterator();
		while (it.hasNext()) {
    
    
			area.append(it.next() + " ");
		}
		// 如果gender不为空字符串,打印性别
		if (!"".equals(gender))
			area.append("你的性别为: " + gender);
	}
	public Information() {
    
    
		//添加标签、单选和复选按钮
		panel.add(lb1);
		panel.add(cb1);
		panel.add(cb2);
		panel.add(cb3);
		panel.add(lb2);
		panel.add(rb1);
		panel.add(rb2);
		bg.add(rb1);
		bg.add(rb2);
		// 为单选和复选按钮添加事件监听器
		cb1.addActionListener(listener1);
		cb2.addActionListener(listener1);
		cb3.addActionListener(listener1);
		rb1.addActionListener(listener2);
		rb2.addActionListener(listener2);
		// 将JPanel面板和JScrollPane面板添加到JFrame容器中 
		Container container = this.getContentPane();
		container.add(panel, BorderLayout.NORTH);
		container.add(pane, BorderLayout.CENTER);
		this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
    
    
		new Information();
	}
}

六、原题及其解析

暂无。

猜你喜欢

转载自blog.csdn.net/hypertext123/article/details/109314860