第 11 章 多线程
-
简述线程的基本概念,程序、进程、线程的关系是什么。
线程与进程相似,但线程是一个比进程更小的执行单位。
程序是静态的代码,进程是程序的一次执行过程,是系统运行程序的基本单位;线程是进程的更小的划分 -
什么是多线程?为什么程序的多线程功能是必要的?
多线程就是同事执行一个以上的线程,一个线程的执行不必等待另一个线程执行完后才执行,所有线程都可以发生在同一时刻;单一的进程在执行任务会出现资源空闲,采用多线程可以让CPU在同一个时间之内执行一个程序中得好几个程序段来完成工作 -
多线程与多任务的差异是什么?
多任务是针对操作系统而言的 ,表示操作系统可以同事运行多个应用程序,而多线程是对一个进程而言的,表示在一个进程内部可以同时执行多个线程。 -
线程有哪些基本状态?这些状态是如何定义的?
新建状态、就绪状态、阻塞状态、运行状态、消亡状态。
新建状态:线程对象已经被分配了内存空间和其他资源,并已被初始化,但是该线程尚未被调度。
就绪状态:处于新建状态的线程被启动后,将进入线程队列排队等待CPU时间片,此时它以具备了运行的条件。
运行状态:线程正在运行,并且已经拥有了对CPU的控制权
阻塞状态:正在执行的线程如果在某些特殊情况下 ,将让出CPU并暂时中止自己的执行
消亡状态:线程不再具有继续运行的能力 -
Java程序实现多线程有哪两个途径?
继承Thread类,实现Runnable接口 -
在什么情况下必须以类实现Runnable接口来创建线程?
当类已经继承了一个类的时候,由于Java是单继承的,所以必须实现Runnable接口才能创建线程 -
什么是线程的同步?程序中为什么要实现线程的同步?是如何实现同步的?
当一个线程对共享的数据进行操作时, 应使之成为一个”原子操作“,即在没有完成相关操作之前,不允许其他线程打断它,否则,就会破坏数据的完整性,必然会得到错误的处理结果;线程间的数据共享会导致多个线程同时处理数据而使数据出现不一致,所以要实现线程的同步;通过线程间互斥访问临界资源来实现同步。 -
假设某家银行可接受顾客的汇款,每进行一次汇款,便可计算出汇款的总额。现有两名顾客,每人分三次、每次100元将钱汇入。试编程来模拟顾客的汇款操作。
public class Exercise { public static void main(String[] args) { Customer customer1 = new Customer(); Customer customer2 = new Customer(); customer1.start(); customer2.start(); } } class Customer extends Thread { @Override public void run() { for (int i = 0; i < 3; i++) { Bank.deposit(100); } } } class Bank { public static int balance = 0; public synchronized static void deposit(int b) { int temp = balance; temp += b; try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { } balance = temp; System.out.println("汇款之后" + balance); } }
第 12 章 泛型与容器类
-
什么是泛型的类型参数?泛型的主要优点是什么?在什么情况下使用泛型方法?泛型类与泛型方法的主要区别是什么?
泛型所操作的数据类型被指定为一个参数,这个参数称为类型参数;泛型的主要优点是能够在编译时而不是在运行时检测出错误; 返回值类型和至少一个参数类型是泛型的情况下使用泛型方法;对于泛型方法,不需要把实际的类型传递给泛型方法,但泛型类却恰恰相反,即必须把实际的类型参数传递给泛型类 -
已知Integer是Number的子类,GeneralType<Integer>是General<Number>的子类吗?Generla<Object>是General<T>的父类吗?
不是;不是 -
在泛型中,类型通配符的主要作用是什么?
一是用在泛型类创建泛型对象时;二是用在方法的参数中 -
分别件数LinkedList和ArrayList、HashSet与HashMap和TreeMap有何异同。
相同点:- LinkedList和ArrayList都实现了List接口
- HashSet和TreeSet都实现了Set接口
不同点:
- LinkedList采用链表结构保存对象,使用双链表实现List。这种结构向链表中任意位置插入、删除元素时不需要移动其他元素,链表的的大小可以动态增长或减小的,但不具有随机存取特性
- ArrayList数组列表类使用一维数组实现List,该类实现的是可变数组,允许所有元素,包括null。具有随机存取特性,插入、删除元素是需要移动其他元素,当元素很多时,插入、删除的速度较慢。在向ArrayList中添加元素时,其容量会自动增大,但不能自动缩小
- HasetSet是根据哈希码来存取集合中的元素,集合中的元素是无序的
- TreeSet还实现了SortedSet接口,集合中的元素都是有序的
-
将1~10之间的整数存放到一个线性表LinkedList的对象中,然后将其下标为4的元素从列表中删除。
import java.util.LinkedList; public class Exercise { public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList<>(); for (int i = 1; i <= 10; i++) { linkedList.add(i); } linkedList.remove(4); } }
-
利用ArrayList类创建一个对象,并向其添加若干个字符串型元素,然后随机选一个元素输出。
import java.util.ArrayList; public class Exercise { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("Hello"); arrayList.add("World"); arrayList.add("Java"); System.out.println(arrayList.get((int)(Math.random() * (arrayList.size() - 1)))); } }
-
集合A={1,2,3,4}和B={1,3,5,7,9,11},编程求A与B的交集、并集和差集。
import java.util.HashSet; import java.util.Set; public class Exercise { public static void main(String[] args) { Set<Integer> a = new HashSet<>(); Set<Integer> b = new HashSet<>(); for (int i = 1; i <= 4; i++) { a.add(i); } for (int i = 1; i <= 11; i += 2) { b.add(i); } a.retainAll(b); a.addAll(b); a.remove(b); } }
-
利用随机函数生成10个随机数,并将他们有序地存入到一个TreeSet对象中,然后利用迭代器有序地输出。
import java.util.Iterator; import java.util.TreeSet; public class Exercise { public static void main(String[] args) { TreeSet<Double> treeSet = new TreeSet<>(); for (int i = 0; i < 10; i++) { treeSet.add(Math.random()); } Iterator<Double> it = treeSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
-
利用HashMap类对象存储公司电话号码薄,其中包含公司的电话号码和公司名称,然后删除一个公司和查询一个公司的操作。
import java.util.HashMap; public class Exercise { public static void main(String[] args) { HashMap<String, String> number = new HashMap<>(); number.put("A", "11111111"); number.put("B", "22222222"); number.put("C", "33333333"); number.put("D", "44444444"); number.remove("A"); number.containsKey("C"); } }
第 13 章 图形界面设计
-
Swing组件分为哪三类?
容器类(container class)、辅助类(hepler class)和组件类(component class) -
Swing的顶层容器包含哪些窗格?大部分的可见组件都放在哪个窗格中?
JFrame、JApplet和JDialog;JFrame -
颜色类Color中提供了哪些表示颜色的变量?
BLACK、BLUE、CYAN、DARK_GRAY、GRAY、GREEN、LIGHT_GRAY、MAGENTA、ORANGE、PINK、RED、WHITE、YELLOW -
字体类Font的主要用途有哪些?
设置组件所用字体的样式、大小与字形等属性。 -
图标类ImageIcon的主要作用是什么?Java语言当前支持哪三种图像格式?
装饰组件;GIF、JPEG、PNG -
Swing主要用来处理文字输入的组件有几个?这几组件有何不同?
JTextField、JPasswordField、JTextArea
单行文本编辑组件JTextField、密码文本行组件JPasswordField、多行文本编辑组件JTextArea -
在将组件添加到窗口中时,为什么需将组件设置为不透明状态才能将其底色显示出来?
因为JFrame的框架一旦被创建,其中就已经包含了一个内容窗格,设置的JFrame背景颜色,仍然会被内容窗格遮盖,由框架内容窗格委托特性可知,在向JFrame中添加组件时,组件都加在了内容窗格中 -
如何设置才能将窗口的底色显示出来?
通过调用JFrame的成员方法getContentPane()获得,然后再设置内容窗格的背景色,这样才能显示出窗口的背景色 -
设计一个窗口,内含一个文本框、三个复选框、两个单选框、一个标签和一个按钮。各组件的位置、大小和其上的文字由用户自己设定。
import java.awt.FlowLayout; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JTextField; public class Exercise { public static void main(String[] args) { JFrame window = new JFrame("A Windows"); window.setLayout(new FlowLayout()); JTextField textField = new JTextField(20); window.add(textField); JCheckBox checkBox1 = new JCheckBox("CheckBox1"); JCheckBox checkBox2 = new JCheckBox("CheckBox2"); JCheckBox checkBox3 = new JCheckBox("CheckBox3"); window.add(checkBox1); window.add(checkBox2); window.add(checkBox3); JRadioButton radioButton1 = new JRadioButton("RadioButton1"); JRadioButton radioButton2 = new JRadioButton("RadioButton2"); ButtonGroup bg = new ButtonGroup(); bg.add(radioButton1); bg.add(radioButton2); window.add(radioButton1); window.add(radioButton2); JLabel label = new JLabel("This is a label"); window.add(label); JButton button = new JButton("Button"); window.add(button); window.pack(); window.setVisible(true); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
-
Java语言中常用的布局管理器有哪几个?它们各有什么特点?
awt五种布局管理器,swing3种(其中两种不介绍)
流式布局管理器FlowLayout:流式页面设计,组件按照加入容器的先后顺序从左向右排列,一行排满之后就自动地转到下一行继续从左向右排列,每一行中得组件默认设置为剧中排列
边界式布局管理器BorderLayout:将显示区域分为东、西、南、北、中5个区域,在将组件加入容器时,指定把组件加入到哪个区域中,默认为中间区域
网格式布局管理器GridLayout:将容器的空间划分为若干行与列的网格形式,每个网格称为单元格,在容器上添加组件时,它们会按从左向右、从上到下的顺序在网格中排列。
卡片式布局管理器CardLayout:把容器中得所有组件如同堆叠起来的一副“扑克牌”,每次只能显示最上面的一张
网格包布局管理器GridBagLayout:是在网格布局管理器的基础上发展而来的,但GridBagLayout布局管理器中允许组件占用不同行或者不同咧的多个单元格,这些被占用的单元格称为组件的显示区域
盒式布局管理器BoxLayout:在一行或者一列中摆放组件
第 14 章 事件处理
-
什么是事件?简述Java语言的委托事件模型。
所谓事件,就是用户使用鼠标或键盘对窗口中得组件进行交互时所发生的事情;委托事件模型就是组件将事件处理委托给外部的处理实体 -
若要处理事件,就必须要有事件监听者,通常哪有对象可以担任监听者?
包含“事件源”的容器对象、内部类对象和匿名内部类都可以担任监听者 -
Java语言中处理事件的具体方法是什么?
- 确认触发的事件,取得事件类的名字,如ActionEvent,去掉其中的“Event”字样,在剩下的部分加入“Listener”,这就是在类里需要实现的事件监听者接口
- 实现上述的接口,针对想要捕获的事件编写方法代码。如要捕获单击按钮事件,就要为ActionListener接口中的actionPerformed()方法编写代码
- 为事件监听者创建一个对象,让组件调用方法完成对它的注册,方法是在监听者接口的名字中加入一个前缀“add",如addActionListener()
-
在进行事件处理时,可以使用实现多个监听者接口的方式,也可以尽可能地使用继承适配器类的方式。使用适配器类的好处是什么?
当需要对某件事件进行处理时,只需让事件处理类继承事件所对应的适配器类,这样只需要覆盖本次操作用到的事件处理方法即可,而不必实现无关的事件处理方法 -
设计一个窗口,在窗口内放置一个按钮,当不断地单击该按钮时,在其上显示它被单击的次数。
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Exercise { public static void main(String[] args) { new Window(); } } class Window extends JFrame{ private static final long serialVersionUID = -2929177477951330739L; private JButton button; private int time; private JLabel label; public Window() { button = new JButton("Click Me"); label = new JLabel("0"); label.setHorizontalAlignment(JLabel.CENTER); time = 0; button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { time++; label.setText("" + time); } }); add(button, BorderLayout.NORTH); add(label, BorderLayout.SOUTH); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
-
设计一个窗口,在该窗口中添加一个JList组件,该组件中有5门课程名称的选项。然后再在窗口中添加一个文本区,当选择JList组件中的某个选项后,文本区中显示对该课程的介绍。
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Exercise { public static void main(String[] args) { new Window(); } } class Window extends JFrame { private static final long serialVersionUID = -2929177477951330739L; private JList<String> courses; public Window() { String[] model = { "English", "Chinese", "C Programming Language", "Java Programming Language", "Operation System" }; courses = new JList<>(model); JScrollPane scrollPane = new JScrollPane(courses); add(scrollPane, BorderLayout.CENTER); JTextArea textArea = new JTextArea(); textArea.setEditable(false); add(textArea, BorderLayout.SOUTH); courses.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { String s = courses.getSelectedValue(); switch (s) { case "English": textArea.setText("This is English"); break; case "Chinese": textArea.setText("This is Chinese"); break; case "C Programming Language": textArea.setText("This is C Programming Language"); break; case "Java Programming Language": textArea.setText("This is Java Programming Language"); break; case "Operation System": textArea.setText("This is Operation System"); break; } } }); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
-
设计一个窗口,其中包含一个文本区、两个复选框和三个横向滑动条,其中滑动条用来调整R、G、B三色的分量从0~255的变化;两个复选框分别用于设定把滑动条调出的颜色应用于文本区的前景色还是背景色。
import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JTextArea; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Exercise { public static void main(String[] args) { new Window(); } } class Window extends JFrame { private static final long serialVersionUID = -4682287151007612144L; JCheckBox frontGroundColor = new JCheckBox("frontGroundColor"); JCheckBox backGroundColor = new JCheckBox("backGroundColor"); JTextArea textArea = new JTextArea("选择前景色和背景色,滑动滑动条查看颜色变化", 20, 30); JSlider rJSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); JSlider gJSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); JSlider bJSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); public Window() { setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); add(textArea); JPanel select = new JPanel(); select.setLayout(new FlowLayout()); select.add(frontGroundColor); select.add(backGroundColor); add(select); JPanel colorPanel = new JPanel(); colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.Y_AXIS)); colorPanel.add(rJSlider); colorPanel.add(gJSlider); colorPanel.add(bJSlider); rJSlider.setMajorTickSpacing(30); rJSlider.setMinorTickSpacing(3); rJSlider.setPaintTicks(true); rJSlider.setPaintLabels(true); gJSlider.setMajorTickSpacing(30); gJSlider.setMinorTickSpacing(3); gJSlider.setPaintTicks(true); gJSlider.setPaintLabels(true); bJSlider.setMajorTickSpacing(30); bJSlider.setMinorTickSpacing(3); bJSlider.setPaintTicks(true); bJSlider.setPaintLabels(true); add(colorPanel); backGroundColor.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (backGroundColor.isSelected()) { textArea.setBackground(new Color(rJSlider.getValue(), gJSlider.getValue(), bJSlider.getValue())); } else { textArea.setBackground(new Color(255, 255, 255)); } } }); frontGroundColor.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (frontGroundColor.isSelected()) { textArea.setForeground(new Color(rJSlider.getValue(), gJSlider.getValue(), bJSlider.getValue())); } else { textArea.setForeground(new Color(0, 0, 0)); } } }); rJSlider.addChangeListener(new ColorListen()); gJSlider.addChangeListener(new ColorListen()); bJSlider.addChangeListener(new ColorListen()); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class ColorListen implements ChangeListener { @Override public void stateChanged(ChangeEvent e) { if (frontGroundColor.isSelected()) { textArea.setForeground(new Color(rJSlider.getValue(), gJSlider.getValue(), bJSlider.getValue())); } if (backGroundColor.isSelected()) { textArea.setBackground(new Color(rJSlider.getValue(), gJSlider.getValue(), bJSlider.getValue())); } } } }
-
编写一应用程序,在其窗口内包含一个菜单栏和一个文本区。菜单栏包括“设置”和“操作”两个菜单。“操作”菜单包括“退出”菜单项,当用户选择“退出”菜单项时,则关闭窗口退出整个应用程序的运行;“设置”菜单包括“字体”和“风格”两个菜单项和一个“只读”复选菜单项。“字体”菜单项包括“宋体”、“楷体”和“黑体”3个单选子菜单项,“风格”菜单项包括“普通”、“黑体”、“斜体”3个复选子菜单项。当“只读”菜单项未被选中时,用户可以了文本区内输入字符;当“只读”菜单项被选中时,用户不能在文本区内输入字符。当用户选择其它菜单项时,文本区内的文字随之变化
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.JTextArea; public class Exercise { public static void main(String[] args) { new Window(); } } class Window extends JFrame { private static final long serialVersionUID = -4682287151007612144L; JTextArea textArea = new JTextArea(20, 30); public Window() { setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); JMenuBar menuBar = new JMenuBar(); JMenu setting = new JMenu("Setting"); JMenu operating = new JMenu("Operating"); menuBar.add(setting); menuBar.add(operating); JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); operating.add(exit); JMenu style = new JMenu("Style"); JMenu font = new JMenu("Font"); ButtonGroup buttonGroup = new ButtonGroup(); JRadioButtonMenuItem ssti = new JRadioButtonMenuItem("宋体"); JRadioButtonMenuItem klti = new JRadioButtonMenuItem("楷体"); JRadioButtonMenuItem hzti = new JRadioButtonMenuItem("黑体"); buttonGroup.add(ssti); buttonGroup.add(klti); buttonGroup.add(hzti); font.add(ssti); font.add(klti); font.add(hzti); JCheckBoxMenuItem normal = new JCheckBoxMenuItem("正常"); JCheckBoxMenuItem black = new JCheckBoxMenuItem("黑体"); JCheckBoxMenuItem till = new JCheckBoxMenuItem("斜体"); style.add(normal); style.add(black); style.add(till); JRadioButtonMenuItem readOnly = new JRadioButtonMenuItem("Read-Only"); setting.add(style); setting.add(font); setting.add(readOnly); add(menuBar); add(textArea); readOnly.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (readOnly.isSelected()) { textArea.setEditable(false); } else { textArea.setEditable(true); } } }); addText(normal); addText(black); addText(till); addText(ssti); addText(klti); addText(hzti); addText(readOnly); setVisible(true); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addText(JMenuItem item) { item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textArea.append(item.getText() + "\n"); } }); } }
-
在上一题的基础上增加如下的功能:每当用户选中“只读”菜单项时,都将“字体”和“风格”两个菜单变成灰色,使之不能被选中;而每当“只读”菜单项未被选中时,再将“字体”和“风格”两个菜单项恢复成可选状态。
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.JTextArea; public class Exercise { public static void main(String[] args) { new Window(); } } class Window extends JFrame { private static final long serialVersionUID = -4682287151007612144L; JTextArea textArea = new JTextArea(20, 30); public Window() { setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); JMenuBar menuBar = new JMenuBar(); JMenu setting = new JMenu("Setting"); JMenu operating = new JMenu("Operating"); menuBar.add(setting); menuBar.add(operating); JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); operating.add(exit); JMenu style = new JMenu("Style"); JMenu font = new JMenu("Font"); ButtonGroup buttonGroup = new ButtonGroup(); JRadioButtonMenuItem ssti = new JRadioButtonMenuItem("宋体"); JRadioButtonMenuItem klti = new JRadioButtonMenuItem("楷体"); JRadioButtonMenuItem hzti = new JRadioButtonMenuItem("黑体"); buttonGroup.add(ssti); buttonGroup.add(klti); buttonGroup.add(hzti); font.add(ssti); font.add(klti); font.add(hzti); JCheckBoxMenuItem normal = new JCheckBoxMenuItem("正常"); JCheckBoxMenuItem black = new JCheckBoxMenuItem("黑体"); JCheckBoxMenuItem till = new JCheckBoxMenuItem("斜体"); style.add(normal); style.add(black); style.add(till); JRadioButtonMenuItem readOnly = new JRadioButtonMenuItem("Read-Only"); setting.add(style); setting.add(font); setting.add(readOnly); add(menuBar); add(textArea); readOnly.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (readOnly.isSelected()) { textArea.setEditable(false); style.setEnabled(false); font.setEnabled(false); } else { textArea.setEditable(true); style.setEnabled(true); font.setEnabled(true); } } }); addText(normal); addText(black); addText(till); addText(ssti); addText(klti); addText(hzti); addText(readOnly); setVisible(true); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addText(JMenuItem item) { item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textArea.append(item.getText() + "\n"); } }); } }
第 16 章 小程序设计
-
Java小程序的基本工作原理是什么?
将编译好的小程序字节码文件,即.class文件保存在特定的WWW服务器上,在同一个或另一个WWW服务器上保存着嵌入有该字节码文件名的HTML文件。当某一个浏览器向服务器请求下载嵌入了小程序的HTML文件时,该文件中得信息以一定的格式显示在用户屏幕上。当浏览器遇到HTML文件中嵌有小程序的标志时,浏览器会根据这个小程序的名字和位置自动把字节码文件从WWW服务器上下载到本地,并利用浏览器本身拥有的Java解释器直接执行该字节码文件。 -
init()、start()、stop()、destory()是小程序中非常重要的4个方法,请问它们各自的调用时机和功能是什么?
init()在小程序被创建时第一个调用的方法,它只运行一次,主要是用来对小程序设置初值用
start()调用完init()方法之后,就立即调用start()方法。只要小程序画面每出现一次,start()方法就会被调用一次。
stop()方法类似于start()方法的逆操作,当浏览器窗口失去焦点变为不活动状态、切换到其他网页浏览或是关闭浏览器时,需要停止小程序的运行,此时系统会自动调用stop()方法以暂停小程序的运行,所以stop()方法也可以被重复调用。
destory()当用户退出浏览器时,浏览器运行的小程序也将停止运行,释放内存。此时浏览器会自动调用小程序对象的destory()方法来完成一些释放资源、关闭连接之类的操作等。 -
如何向小程序传递参数?
由HTML文件中得一个专门标记<Param>来完成的 -
将Java的应用程序转换成小程序的主要步骤有哪些?
- 制作一个HTML网页文件,带有响应的标记,从而能够下载小程序的代码
- 声明一个类,使其继承JApplet类,并使其成为public类型
- 在应用程序中去掉main()方法。
- 在应用程序中,设置窗口的大小通过调用setSize()方法来实现
- 小程序在浏览器退出时,它会终止
- 如果在应用程序中使用setTitle()为窗口设置标题,那么在转换成小程序时此方法不能使用
- 用init()方法替换构造方法,在浏览器创建这个小程序类的一个对象时,它调用了init()方法
-
编写小程序,用paintComponent()方法显示一行字符串,小程序包含“方法”和“缩小”两个按钮。当单击“放大”按钮时,显示的字符串的字号方法一号;单击“缩小”按钮时,显示的字符串字号缩小一号。
import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JTextArea; public class Exercise extends JApplet { private static final long serialVersionUID = -6178049653652743932L; private String s; private JButton big; private JButton smaller; private int size = 16; private JTextArea textArea; @Override public void init() { s = "This is a string"; big = new JButton("放大"); smaller = new JButton("缩小"); textArea = new JTextArea(s); textArea.setFont(new Font(s, Font.ITALIC, size)); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(textArea); c.add(big); c.add(smaller); big.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { size++; textArea.setFont(new Font(s, Font.ITALIC, size)); } }); smaller.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { size--; textArea.setFont(new Font(s, Font.ITALIC, size)); } }); } }
第 17 章 Java数据库程序设计
-
写出在数据库StudentScore的Student表中查找所有年龄大于等于19的同学的SQL语句
SELECT * FROM StudentScore.Student WHERE age >= 19; -
写出姓名为“刘韵”的学生所学课程名称及成绩的SQL语句
SELECT cName, grade FROM Course, Score WHERE Course.cNo = Score.cNo AND sNo IN (SELECT sNo FROM Student WHERE sName = “刘韵”) -
描述JDBC中Driver、Connection、Statement和ResultSet接口的功能
类或接口 功能说明 Driver 驱动程序 Connection 数据库连接,负责与数据库键通讯,SQL执行以及食物处理都是在某个特定Connection环境下进行的。可以产生用以执行SQL的Statement对象 Statement 用以执行不含参数的静态SQL查询和更新,并返回执行结果 ResultSet 用以获得SQL查询结果 -
使用Statement接口和PreparedStatement接口有什么区别?
Statement接口用于执行不带参数的静态SQL语句;PreparedStatement是处理预编译语句的一个接口 -
归纳一下使用JDBC进行数据库连接的完整过程
加载驱动程序、建立与数据库的连接、创建执行方式语句、执行SQL语句、处理返回结果和关闭创建的各种对象 -
如何在结果集中返回字段的数据?如何在结果集中返回字段名?
使用ResultSet对象的getXXX()方法对结果集中得数据进行访问; -
编写一个应用程序,使其可以从StudentScore数据库的某个表中查询一个字段的所有信息
import java.sql.*; public class Exercise { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student", "user", "password"); String sql = "SELECT sNo FROM StudentScore.Student"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()) { String sno = resultSet.getString("sNo"); System.out.println(sno); } resultSet.close(); statement.close(); connection.close(); } catch (ClassNotFoundException e) { System.out.println("The Drive not found."); } catch (SQLException e) { e.printStackTrace(); } } }
-
创建一个名为Books的数据库,并在其中建立一个名为Book的表,字段包括书名、作者、出版社、出版时间和ISBN。编写一个应用程序,运用JDBC在该数据库中实现增加、删除和修改数据的功能。
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Exercise { public static void addRecord (Connection connection) { String bookName = "h"; String author = "j"; String publisher = "j"; String publisherTime = "j"; String isbn = "k"; String sql = "INSERT INTO Books.Book(bookName, author, publisher, publisherTime, isbn) " + "VALUES (?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = null; try { connection.prepareStatement(sql); preparedStatement.setString(1, bookName); preparedStatement.setString(2, author); preparedStatement.setString(3,publisher); preparedStatement.setString(4, publisherTime); preparedStatement.setString(5, isbn); preparedStatement.execute(); preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } } public static void removeRecord (Connection connection) { String bookName = "j"; String sql = "DROP FROM Books.Book WHERE bookName = ?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, bookName); preparedStatement.execute(); preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void updateRecode (Connection connection) { String bookName = "j"; String publisher = "k"; PreparedStatement preparedStatement = null; String sql = "UPDATE Books.Book SET publisher = ? WHERE bookName = ?"; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, publisher); preparedStatement.setString(2, bookName); preparedStatement.execute(); preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } }
-
假设在StudentScore数据库的Studnet表中,存在多个姓氏相同的人,根据这种情况建立查询,要求提供一个合适的图形界面,用户可以滚动查看查询记录
第 18 章 Java网络编程
-
什么是URL?URL地址由哪几部分组成?
URL是统一资源定位符的英文缩写,它表示Internet上某一资源的地址;URL地址由5个部分组成,格式如下:
传输协议://主机名:端口号/文件名#引用 -
什么是Socket?它与TCP/IP协议有何关系?
Socket是实现 客户与服务器模式的通信方式,它首先需要建立稳定的连接,然后以流的方式传输数据,实现网络通信;Socket在TCP/IP中定义,针对一个特定的连接 -
简述流式Socket的通信机制。它的最大特点是什么?为什么可以实现无差错通信?
建立两台计算机的连接,有一个程序向另一台计算机上的程序发送请求,建立通信; -
什么是端口号?服务器端和客户端分别如何使用端口号?
端口号是区分一台主机中得不同应用程序的一个数据标识符;服务器端和客户端必须为每个程序分配一个唯一的端口考,通过端口号指定要连接的那个程序 -
什么是套接字?其作用是什么?
套接字是实现客户与服务器模式的通信模式,它首先需要建立稳定的连接,然后以流的方式传输数据,实现网络通信;套接字是网络上运行的两个程序间双向通信的一端,既可以接受请求,也可以发送请求,利用它可以较方便地进行网络上的数据传输。 -
编写Java程序,使用InetAddress类实现根据域名自动到DNS(域名服务器)上查找IP地址的功能。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.UnknownHostException; public class Exercise { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String dns = bufferedReader.readLine(); bufferedReader.close(); InetAddress inetAddress = null; try { inetAddress = InetAddress.getByName(dns); System.out.println(inetAddress.getHostAddress()); } catch (UnknownHostException e) { System.out.println("Your Input Domain Can't Parse."); } } }
-
用Java程序实现流式Socket通信,需要使用那两个类?他们是如何定义的?应怎样使用?
Socket类和ServerSocket类;Socket类用在客户端,用户通过创建一个Socket对象来建立与服务器的连接,ServerSocket类的作用是实现客户机/服务器模式的通信方法下服务器的套接字;客户端的程序使用Socket类建立一服务器的套接字连接,服务器端的程序使用ServerSocket类建立接受客户套接字的服务器套接字; -
与流式Socket相比,数据报通信有何特点?
数据报通信是无序的,也不能确保绝对安全可靠,但它非常简单,也具有比较高的效率