当前位置:   article > 正文

JAVA程序开发参考手册

by8835换哪了
  1. <!--包装数据类型 ->
  2. //javalangInteger——整数类
  3. //bitCount方法——获取二进制补码中位的数量
  4. int i = 10;
  5. int count = Integer.bitCount(i);
  6. System.out.print(i + "的二进制补码表示形式的1位的数量");
  7. System.out.println(count);
  8. //byteValue方法——获取byte类型的值
  9. Integer tobyte = new Integer("125");
  10. byte byte1 = tobyte.byteValue();
  11. System.out.println(byte1);
  12. //compareTo方法——比较整数
  13. Integer int1 = new Integer("100");
  14. Integer int2 = new Integer("100");
  15. int str = int1.compareTo(int2);
  16. System.out.println(str);
  17. //decode方法——将字符串解码为int类型
  18. String str = new String("100");
  19. Integer a = Integer.decode(str);
  20. System.out.println(a);
  21. //doubleValue方法——返回double数值
  22. Integer todouble = new Integer(123);
  23. double str = todouble.doubleValue();
  24. System.out.println(str);
  25. //equals方法——比较整数对象是否相等
  26. Integer i = new Integer(123);
  27. Integer j = new Integer(123);
  28. boolean b = i.equals(j);
  29. System.out.println(b);
  30. //floatValue方法——返回float值
  31. Integer floats = new Integer(123);
  32. float str = floats.floatValue();
  33. System.out.println(str);
  34. //getInteger方法——获取整数的系统属性值
  35. String nm = "sun.arch.data.model";
  36. Integer i = Integer.getInteger(nm);
  37. System.out.println(i);
  38. //hashCode方法——生成整数的哈希码
  39. Integer i = new Integer(10);
  40. int j = i.hashCode();
  41. System.out.println(j);
  42. //highestOneBit方法——获取整数二进制最高位的索引
  43. for (int i = 0; i < 10; i++) {
  44. int j = Integer.highestOneBit(i);
  45. System.out.println(j);
  46. //intValue方法——获取int值
  47. Integer ints = new Integer("12");
  48. int str = ints.intValue();
  49. System.out.println(str);
  50. //longValue方法——获取long值
  51. Integer longs = new Integer("12");
  52. long str = longs.longValue();
  53. System.out.println(str);
  54. //lowestOneBit方法——获取整数二进制最低位的索引
  55. for (int i = 0; i < 10; i++) {
  56. int j = Integer.lowestOneBit(i);
  57. System.out.println(j);
  58. }
  59. //parseInt方法——将字符串解析为int值
  60. String stri = new String("100");
  61. int str = Integer.parseInt(stri);
  62. System.out.println(str);
  63. //reverse方法—反转整数二进制补码的位顺序
  64. int i;
  65. int j = 10;
  66. i = Integer.reverse(j);
  67. System.out.println(i);
  68. //reverseBytes方法—反转整数字节的顺序
  69. int i;
  70. int j = 10;
  71. i = Integer.reverseBytes(j);
  72. System.out.println(i);
  73. //shortValue方法—获取short值
  74. int i = 10;
  75. Integer shorts = new Integer(i);
  76. short str = shorts.shortValue();
  77. System.out.println(str);
  78. //signum方法—获取整数符号
  79. int i = 10;
  80. System.out.println(Integer.signum(i));
  81. //toBinaryString方法—生成整数的二进制字符串
  82. int i = 10;
  83. Integer stri = new Integer(i);
  84. String str = Integer.toBinaryString(stri);
  85. System.out.println(str);
  86. //toHexString方法—生成整数的十六进制字符串
  87. Integer stri = new Integer(10);
  88. String str = Integer.toHexString(stri);
  89. System.out.println(str);
  90. //toOctalString方法—生成整数的八进制字符串
  91. Integer stri = new Integer(10);
  92. String str = Integer.toOctalString(stri);
  93. System.out.println(str);
  94. //toString方法—生成整数的十进制字符串
  95. Integer stri = new Integer(10);
  96. String str = Integer.toString(stri);
  97. System.out.println(str);
  98. //valueOf方法—创建Integer对象
  99. Integer stri = new Integer(10);
  100. Integer str = Integer.valueOf(stri);
  101. System.out.println(str);
  102. //javalangString——字符串
  103. //charAt方法——提取指定字符
  104. String strCom = "I LIKE JAVA";
  105. char strLower = strCom.charAt(4);
  106. System.out.println(strLower);
  107. //codePointAt方法—提取索引字符代码点
  108. String strCom = "I LIKE JAVA";
  109. int strLower = strCom.codePointAt(8);
  110. System.out.println(strLower);
  111. //codePointBefore方法——获取索引前一个字符的代码点
  112. String strCom = "I LIKE JAVA";
  113. int strLower = strCom.codePointBefore(8);
  114. System.out.println(strLower);
  115. //codePointCount方法——获取指定范围文本代码点数
  116. String strCom = "I LIKE JAVA";
  117. int strLower = strCom.codePointCount(2, 3);
  118. System.out.println(strLower);
  119. //compareTo方法——比较两个字符串
  120. String strCom = "I LIKE JAVA";
  121. String strCom1 = "I LIKE PHP";
  122. int strLower = strCom.compareTo(strCom1);
  123. System.out.println(strLower);
  124. //compareToIgnoreCase方法——忽略大小写比较字符串
  125. String strCom = "I LIKE JAVA";
  126. String strCom1 = "I LIKE PHP";
  127. int strLower = strCom.compareToIgnoreCase(strCom1);
  128. System.out.println(strLower);
  129. //concat方法——字符串结尾连接
  130. String strCom = "I LIKE JAVA";
  131. String strCom1 = "I LIKE PHP";
  132. String str = strCom.concat(strCom1);
  133. System.out.println(str);
  134. //contains方法——判断是否包含指定字符
  135. String strCom = "I LIKE JAVA";
  136. boolean str = strCom.contains("JAVA");
  137. System.out.println(str);
  138. //copyValueOf方法——字符数组生成字符串
  139. char[] array = {'科','技'};
  140. String str = String.copyValueOf(array);
  141. System.out.println(str);
  142. //endsWith方法——判断后缀字符串
  143. String strCom = "string.java";
  144. boolean str = strCom.endsWith(".java");
  145. System.out.println(str);
  146. //equals方法——判断字符串相等
  147. String strCom1 = "MN";
  148. String strCom2 = "mn";
  149. boolean strB = strCom1.equals(strCom2);
  150. System.out.println(strB);
  151. //equalsIgnoreCase方法——忽略大小写判断字符串相等
  152. String strCom1 = "MN";
  153. String strCom2 = "mn";
  154. boolean strB = strCom1.equalsIgnoreCase(strCom2);
  155. System.out.println(strB);
  156. //format方法——格式化字符串
  157. String str = String.format("%d", 400/2);
  158. System.out.println(str);
  159. //getBytes方法——获取字符串的字节数组
  160. String strCom = "java";
  161. byte[] str = strCom.getBytes();
  162. for (int i = 0; i < str.length; i++) {
  163. System.out.println(str[i]);
  164. }
  165. //getChars方法——获取字符数组
  166. String str = "HELLO WORLD";
  167. char[] ch = new char[10];
  168. str.getChars(0, 10, ch, 0);
  169. for (int i = 0; i < ch.length; i++) {
  170. System.out.println(ch[i]);
  171. }
  172. //hashCode方法——生成字符串哈希表
  173. String str = "I LIKE JAVA";
  174. System.out.println(str.hashCode());
  175. //indexOf方法——获取字符第一个索引
  176. String str = "Hello World";
  177. int index = str.indexOf(5);
  178. System.out.println(index);
  179. //intern方法——获取规范化字符串
  180. String str1 = new String("helloworld");
  181. String str2 = new String("helloworld");
  182. System.out.println(str1.equals(str2));
  183. System.out.println(str1 == str2);
  184. System.out.println(str1.intern() == str2.intern());
  185. //isEmpty方法——判断字符串是否为空
  186. String str = "hello world";
  187. boolean b = str.isEmpty();
  188. System.out.println(b);
  189. //lastIndexOf方法——获取字符最后的索引
  190. String strCom = "abcdefg gfdecba";
  191. int index = strCom.lastIndexOf("a");
  192. System.out.println(index);
  193. //length方法——获取字符串的长度
  194. String str = "I like Java";
  195. int length = str.length();
  196. System.out.println(length);
  197. //matches方法——匹配正则表达式
  198. String regex = "1234";
  199. System.out.println(regex.matches("\\d{4}"));
  200. //offsetByCodePoints方法——获取索引偏移后指定代码点的索引
  201. String strCom = "I LIKE JAVA";
  202. int i = strCom.offsetByCodePoints(2, 3);
  203. System.out.println(i);
  204. //regionMatches方法——测试两个字符串区域是否相等
  205. String strCom = "ABLIKE";
  206. String subStr = "CDLIKE";
  207. boolean b = subStr.regionMatches(true, 2, strCom, 2, 3);
  208. System.out.println(b);
  209. //replace方法——替换字符序列
  210. String str = "图书";
  211. str.replace("图书", "参考");
  212. System.out.println(str);
  213. //replaceAll方法——正则表达式匹配替换所有字符串
  214. String str = "成功者找方法,";
  215. str = str.replaceAll(",", "失败者找理由");
  216. System.out.println(str);
  217. //replaceFirst方法——正则表达式替换第一个匹配的字符串
  218. String str = "1词典,1图书,1软件";
  219. str = str.replaceFirst("1", "JAVA");
  220. System.out.println(str);
  221. //split方法——字符串分割成数组
  222. String str = "公司名称:xxx科技!公司所在地:XXX市。公司电话:11122";
  223. String[] info = null;
  224. info = str.split("!|。");
  225. for (int i = 0; i < info.length; i++) {
  226. System.out.println(info[i]);
  227. }
  228. //startsWith方法——判断前缀字符串
  229. String strCom1 = "I like Java";
  230. boolean strB = strCom1.startsWith("I l",0);
  231. System.out.println(strB);
  232. //subSequence方法——获取子字符串序列
  233. String strCom = "I IIKE JAVA";
  234. System.out.println(strCom.subSequence(2, 6));
  235. //subString方法——获取子字符串
  236. String strCom = "I IIKE JAVA";
  237. String strResult = strCom.substring(3);
  238. System.out.println(strResult);
  239. //toCharArray方法——字符串变字符数组
  240. String strCom = "JAVA参考大全";
  241. char[] str = strCom.toCharArray();
  242. for (int i = 0; i < str.length; i++) {
  243. System.out.println(str[i]);
  244. }
  245. //toLowerCase方法——转换成小写字符串
  246. String strCom = "I LIKE JAVA";
  247. String strLower = strCom.toLowerCase();
  248. System.out.println(strLower);
  249. //toUpperCase方法——转换成大写字符串
  250. String strCom = "i like java";
  251. String strLower = strCom.toUpperCase();
  252. System.out.println(strLower);
  253. //trim方法——去除空格
  254. String strCom = " JAVA参考大全 ";
  255. String str = strCom.trim();
  256. System.out.println(str);
  257. //valueOf方法——基本数据类型转成字符串
  258. boolean strCom = true;
  259. String str = String.valueOf(strCom);
  260. System.out.println(str);
  261. //javalangLong——长整型类
  262. //bitCount方法——获取二进制补码中的数量
  263. for (int i = 0; i < 10; i++) {
  264. int count = Long.bitCount(i);
  265. System.out.println(count);
  266. }
  267. //byteValue方法——获取byte值
  268. Long bytes = new Long("125");
  269. byte str = bytes.byteValue();
  270. System.out.println(str);
  271. //compareTo方法——比较长整数
  272. Long l1 = new Long("125");
  273. Long l2 = new Long("125");
  274. int str = l1.compareTo(l2);
  275. System.out.println(str);
  276. //decode方法——字符串解码为long类型
  277. String stri = new String("100");
  278. Long str = Long.decode(stri);
  279. System.out.println(str);
  280. //doubleValue方法——返回double数值
  281. Long doubles = new Long(123);
  282. double str = doubles.doubleValue();
  283. System.out.println(str);
  284. //equals方法——判断长整数对象相等
  285. Long i = new Long(123);
  286. Long j = new Long(123);
  287. boolean b = i.equals(j);
  288. System.out.println(b);
  289. //floatValue方法——获取float数值
  290. Long floats = new Long(123);
  291. float str = floats.floatValue();
  292. System.out.println(str);
  293. //getLong方法——获取长整数的系统属性值
  294. String nm = "sun.arch.data.mode1";
  295. Long i = Long.getLong(nm);
  296. System.out.println(i);
  297. //hashCode方法——生成长整数的哈希码
  298. Long i = new Long(10);
  299. int j = i.hashCode();
  300. System.out.println(j);
  301. //highestOneBit方法——获取长整数二进制最高位的索引
  302. Long l = new Long(10);
  303. long j = Long.highestOneBit(l);
  304. System.out.println(j);
  305. //intValue方法——获取int值
  306. Long ints = new Long(22456L);
  307. int str = ints.intValue();
  308. System.out.println(str);
  309. //longValue方法——获取long值
  310. Long longs = new Long("12");
  311. long str = longs.longValue();
  312. System.out.println(str);
  313. //lowestOneBit方法——获取长整数二进制最低位的索引
  314. Long l = new Long(10);
  315. long j = Long.lowestOneBit(l);
  316. System.out.println(j);
  317. //parseLong方法——将字符串解析为long值
  318. String str[] = {"15","20","45","35"};
  319. long sum = 0;
  320. for (int i = 0; i < str.length; i++) {
  321. long mylong = Long.parseLong(str[i]);
  322. sum += mylong;
  323. }
  324. System.out.println("数组求和为:"+sum);
  325. //reverse方法——反转长整数二进制补码的位顺序
  326. long i;
  327. i = Long.reverse(10);
  328. System.out.println(i);
  329. //reverseBytes方法——反转长整数字节的顺序
  330. long i;
  331. i = Long.reverseBytes(10);
  332. System.out.println(i);
  333. //shortValue方法——获取short值
  334. Long shorts = new Long(12L);
  335. short str = shorts.shortValue();
  336. System.out.println(str);
  337. //signum方法——获取长整数符号
  338. long i = 10;
  339. System.out.println(Long.signum(i));
  340. //toBinaryString方法——生成长整数的二进制字符串
  341. Long stri = new Long(10L);
  342. String str = Long.toBinaryString(stri);
  343. System.out.println(str);
  344. //toHexString方法——生成长整数的十六进制字符串
  345. Long stri = new Long(456);
  346. String str = Long.toHexString(stri);
  347. System.out.println(str);
  348. //toOctalString方法——生成长整数的八进制字符串
  349. Long stri = new Long(456);
  350. String str = Long.toOctalString(stri);
  351. System.out.println(str);
  352. //toString方法——生成长整数的十进制字符串
  353. Long stri = new Long(10);
  354. String str = Long.toString(stri);
  355. System.out.println(str);
  356. //valueOf方法——创建Long对象
  357. Long stri = new Long(10L);
  358. long str = Long.valueOf(stri);
  359. System.out.println(str);
  360. //javalangShort——短整型类
  361. //compareTo方法——比较短整数
  362. Short com = new Short("125");
  363. Short com1 = new Short("125");
  364. int str = com.compareTo(com1);
  365. System.out.println(str);
  366. //decode方法——字符串解码为short类型
  367. String stri = new String("100");
  368. Short str = Short.decode(stri);
  369. System.out.println(str);
  370. //doubleValue方法——返回double数值
  371. short shor = 125;
  372. Short doubles = new Short(shor);
  373. double str = doubles.doubleValue();
  374. System.out.println(str);
  375. //equals方法——判断短整数对象相等
  376. Short i = new Short("123");
  377. Short j = new Short("123");
  378. boolean b = i.equals(j);
  379. System.out.println(b);
  380. //floatValue方法——获取float数值
  381. Short shor = 125;
  382. Short floats = new Short(shor);
  383. float str = floats.floatValue();
  384. System.out.println(str);
  385. //hashCode方法——生成短整数的哈希码
  386. Short i = new Short("10");
  387. int j = i.hashCode();
  388. System.out.println(j);
  389. //intValue方法——获取int值
  390. short shor = 125;
  391. Short ints = new Short(shor);
  392. int str = ints.intValue();
  393. System.out.println(str);
  394. //longValue方法——获取long值
  395. Short Shorts = new Short("12345");
  396. long str = Shorts.longValue();
  397. System.out.println(str);
  398. //parseShort方法——将字符串解析为十进制short值
  399. String str[] = {"15","20","45","35"};
  400. short sum = 0;
  401. for (int i = 0; i < str.length; i++) {
  402. Short mylong = Short.parseShort(str[i]);
  403. sum += mylong;
  404. }
  405. System.out.println("数组求和为:"+sum);
  406. //reverseBytes方法——反转短整数字节的顺序
  407. short i = 10;
  408. short j = Short.reverseBytes(i);
  409. System.out.println(j);
  410. //shortValue方法——获取short值
  411. Short i = 10;
  412. short j = i.shortValue();
  413. System.out.println(j);
  414. //toString方法——生成短整数的十进制字符串
  415. Short stri = new Short("10");
  416. String str = Short.toString(stri);
  417. System.out.println(str);
  418. //valueOf方法——创建Short对象
  419. Short stri = new Short("100");
  420. Short str = Short.valueOf(stri);
  421. System.out.println(str);
  422. //javalangBoolean——布尔类
  423. //booleanValue方法——获取boolean值
  424. Boolean bool = new Boolean("true");
  425. boolean b = bool.booleanValue();
  426. System.out.println(b);
  427. //compareTo方法——比较布尔值
  428. Boolean b1 = true;
  429. Boolean b2 = true;
  430. int i;
  431. i = b1.compareTo(b2);
  432. System.out.println(i);
  433. //equals方法——判断相等
  434. Boolean b1 = true;
  435. Boolean b2 = false;
  436. boolean b;
  437. b = b1.equals(b2);
  438. System.out.println(b);
  439. //getBoolean方法——获取布尔类型的系统属性值
  440. String nm = "sun.arch.date.model";
  441. boolean b = Boolean.getBoolean(nm);
  442. System.out.println(b);
  443. //hashCode方法——生成布尔对象的哈希码
  444. Boolean b = true;
  445. int i = b.hashCode();
  446. System.out.println(i);
  447. //parseBoolean方法——将字符串解析成boolean值
  448. String s = "true";
  449. boolean b = Boolean.parseBoolean(s);
  450. System.out.println(b);
  451. //toString方法——生成布尔值的字符串
  452. Boolean b = false;
  453. String s = b.toString();
  454. System.out.println(s);
  455. //valueOf方法——创建布尔对象
  456. Boolean i = true;
  457. Boolean b = Boolean.valueOf(i);
  458. System.out.println(b);
  459. //javalangByte——字节对象
  460. //compareTo方法——比较字节对象
  461. Byte b1 = new Byte("10");
  462. Byte b2 = new Byte("20");
  463. int i;
  464. i = b1.compareTo(b2);
  465. System.out.println(i);
  466. //decode方法——将字符串解码为byte值
  467. String s = "0x64";
  468. Byte b = Byte.decode(s);
  469. System.out.println(b);
  470. //doubleValue方法——获取double值
  471. Byte b = 100;
  472. double d = b.doubleValue();
  473. System.out.println(d);
  474. //equals方法——判断字节相等
  475. Byte b1 = 10;
  476. Byte b2 = 20;
  477. boolean b;
  478. b = b1.equals(b2);
  479. System.out.println(b);
  480. //floatValue方法——获取float值
  481. Byte b1 = 10;
  482. float f = b1.floatValue();
  483. System.out.println(f);
  484. //hashCode方法——生成字节对象的哈希码
  485. Byte b1 = 10;
  486. int i = b1.hashCode();
  487. System.out.println(i);
  488. //intValue方法——获取int值
  489. byte by = 123;
  490. Byte b = new Byte(by);
  491. int it = b.intValue();
  492. System.out.println(it);
  493. //longValue方法——获取long值
  494. Byte b1 = 100;
  495. long i = b1.longValue();
  496. System.out.println(i);
  497. //parseByte方法——将字符串解析为byte值
  498. String str = new String("12");
  499. byte it = Byte.parseByte(str);
  500. System.out.println(it);
  501. //shortValue方法——获取short值
  502. Byte b1 = 10;
  503. short i = b1.shortValue();
  504. System.out.println(i);
  505. //toString方法——生成字节值的十进制字符串
  506. Byte str = new Byte("125");
  507. String it = Byte.toString(str);
  508. System.out.println(it);
  509. //valueOf方法——创建Byte对象
  510. Byte b1 = 10;
  511. Byte b = Byte.valueOf(b1);
  512. System.out.println(b);
  513. //javalangCharacter——字符类
  514. //charCount方法——计算指定字符代码点的数量
  515. int c1 = 0x10000;
  516. int cha = Character.charCount(c1);
  517. System.out.println(cha);
  518. //charValue方法——获取char值
  519. Character c1 = new Character('a');
  520. char cha = c1.charValue();
  521. System.out.println(cha);
  522. //codePointAt方法——获取字符数组元素的代码点
  523. char[] c1 = {'科','技'};
  524. int cha = Character.codePointAt(c1, 1);
  525. System.out.println(cha);
  526. //codePointBefore方法——获取字符数组索引前一个元素的代码点
  527. char[] c1 = {'科','技'};
  528. int cha = Character.codePointBefore(c1, 1);
  529. System.out.println(cha);
  530. //codePointCount方法——返回字符数组的子数组中代码点的数量
  531. char[] c1 = {'科','技'};
  532. int cha = Character.codePointCount(c1,0,1);
  533. System.out.println(cha);
  534. //compareTo方法——比较字符对象
  535. Character c1 = new Character('2');
  536. Character c2 = new Character('1');
  537. int cha = c1.compareTo(c2);
  538. System.out.println(cha);
  539. //equals方法——判断字符对象相等
  540. Character c = new Character('a');
  541. boolean b = c.equals(new Character('a'));
  542. System.out.println(b);
  543. //getNumericValue方法——返回字符表示的int值
  544. char c = '\u216C';
  545. int i = Character.getNumericValue(c);
  546. System.out.println(i);
  547. //getType方法——返回一个指示字符的常规类别的值
  548. char ch = 'a';
  549. System.out.println(Character.getType(ch));
  550. //hashCode方法——生成字符对象的哈希码
  551. Character c = new Character('a');
  552. int i = c.hashCode();
  553. System.out.println(i);
  554. //isDefined方法——判断是否为Unicode字符
  555. boolean b = Character.isDefined(11);
  556. System.out.println(b);
  557. //isDigit方法——判断是否为数字字符
  558. char c2 = '科';
  559. boolean cha = Character.isDigit(c2);
  560. System.out.println(cha);
  561. //isLetter方法——判断是否为字母字符
  562. boolean bool = Character.isLetter('*');
  563. System.out.println(bool);
  564. //isLowerCase方法——判断是否为小写字符
  565. char c1 = 'a';
  566. boolean bool = Character.isLowerCase(c1);
  567. System.out.println(bool);
  568. //isUpperCase方法——判断是否为大写字符
  569. char c1 = 'L';
  570. boolean bool = Character.isUpperCase(c1);
  571. System.out.println(bool);
  572. //toLowerCase方法——转换为小写字符
  573. char c1 = 'L';
  574. char cha = Character.toLowerCase(c1);
  575. System.out.println(cha);
  576. //toUpperCase方法——转换为大写字符
  577. char c1 = 'a';
  578. char cha = Character.toUpperCase(c1);
  579. System.out.println(cha);
  580. //javalangDouble——双精度数字类
  581. //byteValue方法——获取byte值
  582. Double d1 = new Double(58.78);
  583. byte dou = d1.byteValue();
  584. System.out.println(dou);
  585. //compare方法——比较双精度数字对象
  586. double d1 = 45.23;
  587. double d2 = 45.22;
  588. int dou = Double.compare(d1, d2);
  589. System.out.println(dou);
  590. //compareTo方法——比较两个Double对象
  591. Double d1 = 45.23;
  592. Double d2 = 45.22;
  593. int dou = d1.compareTo(d2);
  594. System.out.println(dou);
  595. //intValue方法——将double值以int形式返回
  596. Double d1 = new Double(45.23F);
  597. int dou = d1.intValue();
  598. System.out.println(dou);
  599. //doubleToLongBits方法——返回指定浮点值的表示形式
  600. double d = 123.456;
  601. long l = Double.doubleToLongBits(d);
  602. System.out.println(l);
  603. //doubleToRawLongBits方法——保留NaN值返回指定浮点值的表示形式
  604. double d = 123.456;
  605. long l = Double.doubleToRawLongBits(d);
  606. System.out.println(l);
  607. //doubleValue方法——获取double值
  608. Double d1 = new Double(123.456);
  609. double d2 = d1.doubleValue();
  610. System.out.println(d2);
  611. //equals方法——判断Double对象是否相等
  612. Double d1 = new Double(123.456);
  613. Double d2 = new Double(123.456);
  614. boolean b = d1.equals(d2);
  615. System.out.println(b);
  616. //floatValue方法——获取float值
  617. Double d1 = new Double(123.456);
  618. float f = d1.floatValue();
  619. System.out.println(f);
  620. //hashCode方法——生成Double对象的哈希码
  621. Double d1 = new Double(123.456);
  622. int i = d1.hashCode();
  623. System.out.println(i);
  624. //isInfinite方法——判断double值的大小是否是无穷大
  625. Double d1 = new Double(45.23F);
  626. boolean dou = d1.isInfinite();
  627. System.out.println(dou);
  628. //isNaN方法——判断double值是否是一个非数字值
  629. Double d1 = new Double(45.23F);
  630. boolean dou = d1.isNaN();
  631. System.out.println(dou);
  632. //longBitsToDouble方法——返回给定位表示形式的double值
  633. double d1 = 123.456;
  634. long l = Double.doubleToLongBits(d1);
  635. System.out.println(l);
  636. //longValue方法——获取long值
  637. Double d1 = new Double(45.23F);
  638. long dou = d1.longValue();
  639. System.out.println(dou);
  640. //parseDouble方法——将字符串解析为double值
  641. String d1 = new String("124");
  642. double dou = Double.parseDouble(d1);
  643. System.out.println(dou);
  644. //shortValue方法——获取short值
  645. Double d1 = new Double(45.23F);
  646. short dou = d1.shortValue();
  647. System.out.println(dou);
  648. //toHexString方法——生成双精度数字的十六进制字符串
  649. Double d1 = new Double(0.25);
  650. String dou = Double.toHexString(d1);
  651. System.out.println(dou);
  652. //toString方法——生成双精度数字的十进制字符串
  653. Double d1 = new Double(0.25F);
  654. String dou = Double.toString(d1);
  655. System.out.println(dou);
  656. //valueOf方法——创建Double对象
  657. Double d1 = new Double(0.25F);
  658. Double dou = Double.valueOf(d1);
  659. System.out.println(dou);
  660. //javalangFloat——浮点类
  661. //byteValue方法——获取byte值
  662. Float f = new Float(123.456f);
  663. byte b = f.byteValue();
  664. System.out.println(b);
  665. //compare方法——比较Float对象
  666. float f1 = 456.23F;
  667. float f2 = 456.22F;
  668. int f = Float.compare(f1, f2);
  669. System.out.println(f);
  670. //compareTo方法——比较两个Float对象所表示的数值
  671. Float f1 = 456.23F;
  672. Float f2 = 456.22F;
  673. int f = f1.compareTo(f2);
  674. System.out.println(f);
  675. //doubleValue方法——获取double值
  676. Float f = new Float(123.456F);
  677. double d = f.doubleValue();
  678. System.out.println(d);
  679. //equals方法——判断Float对象相等
  680. Float f1 = new Float(123.456f);
  681. Float f2 = new Float(123.456f);
  682. boolean b = f1.equals(f2);
  683. System.out.println(b);
  684. //floatToIntBits方法——返回浮点值的表示形式
  685. float f = 123.456f;
  686. int i = Float.floatToIntBits(f);
  687. System.out.println(i);
  688. //floatToRawIntBits方法——保留非数字值返回指定浮点值的表示形式
  689. float f = 123.456f;
  690. int i = Float.floatToRawIntBits(f);
  691. System.out.println(i);
  692. //floatValue方法——获取float值
  693. Float f = new Float(123.456f);
  694. float ff = f.floatValue();
  695. System.out.println(ff);
  696. //hashCode方法——返回Float对象的哈希码
  697. Float f = new Float(123.456f);
  698. int i = f.hashCode();
  699. System.out.println(i);
  700. //intBitsToFloat方法——返回指定位表示形式的float值
  701. float f = 123.456f;
  702. int i = Float.floatToIntBits(f);
  703. System.out.println(i);
  704. //intValue方法——获取int值
  705. Float f1 = new Float(456.23F);
  706. int fol = f1.intValue();
  707. System.out.println(fol);
  708. //isInfinite方法——判断float值的大小是否是无穷大
  709. Float f1 = new Float(456.23F);
  710. boolean b = f1.isInfinite();
  711. System.out.println(b);
  712. //isNaN方法——判断float值是否是一个非数字值
  713. Float f = new Float(11.23F);
  714. boolean b = f.isNaN();
  715. System.out.println(b);
  716. //longValue方法——获取long值
  717. Float f = new Float(456.23F);
  718. long l = f.longValue();
  719. System.out.println(l);
  720. //parseFloat方法——将字符串解析成float值
  721. String s = new String("124");
  722. float f = Float.parseFloat(s);
  723. System.out.println(f);
  724. //shortValue方法——获取short值
  725. Float f = new Float(35.23F);
  726. short sh = f.shortValue();
  727. System.out.println(sh);
  728. //toHexString方法——生成浮点数的十六进制字符串
  729. Float f = new Float(0.25);
  730. String s = Float.toHexString(f);
  731. System.out.println(s);
  732. //toString方法——生成浮点数的十进制字符串
  733. Float f = new Float(0.25F);
  734. String s = Float.toString(f);
  735. System.out.println(s);
  736. //valueOf方法——创建浮点数对象
  737. Float f1 = new Float(0.25F);
  738. Float f2 = Float.valueOf(f1);
  739. System.out.println(f2);
  740. <!--集合操作 ->
  741. //javautilList——有序集合类
  742. //add方法——向列表中插入元素
  743. ArrayList<String> list = new ArrayList<String>();
  744. list.add("学生");
  745. list.add(null);
  746. list.add("老师");
  747. for (int i = 0; i < list.size(); i++) {
  748. System.out.println(i+":"+list.get(i));
  749. }
  750. //addAll方法——将指定collection添加到列表中
  751. List<String> list = new ArrayList<String>();
  752. list.add("保护环境");
  753. list.add("爱护地球");
  754. list.add("从我做起");
  755. List<String> list_ad = new ArrayList<String>();
  756. list_ad.add("公益广告");
  757. boolean flag = list.addAll(list_ad);
  758. System.out.println(flag);
  759. System.out.println(list);
  760. //clear方法——从列表中移除所有元素
  761. List<String> list = new ArrayList<String>();
  762. list.add("保护环境");
  763. list.add("爱护地球");
  764. list.add("从我做起");
  765. System.out.println(list);
  766. list.clear();
  767. System.out.println(list);
  768. //contains方法——判断列表中是否包含指定元素
  769. List<String> list = new ArrayList<String>();
  770. list.add("保护环境");
  771. list.add("爱护地球");
  772. list.add("从我做起");
  773. String o = "保护环境";
  774. System.out.println(list.contains(o));
  775. //containsAll方法——判断列表中是否包含指定collection的所有元素
  776. List<String> list = new ArrayList<String>();
  777. list.add("保护环境");
  778. list.add("爱护地球");
  779. list.add("从我做起");
  780. String o = "保护环境";
  781. List<String> list_ad = new ArrayList<String>();
  782. list_ad.addAll(list);
  783. list_ad.add(0,"公益广告");
  784. System.out.println(list_ad.containsAll(list));
  785. //equals方法——比较指定的对象与列表是否相等
  786. List<String> list = new ArrayList<String>();
  787. list.add("保护环境");
  788. list.add("爱护地球");
  789. list.add("从我做起");
  790. List<String> list_ad = new ArrayList<String>();
  791. list_ad.add("保护环境");
  792. list_ad.add("爱护地球");
  793. list_ad.add("从我做起");
  794. boolean ret = list_ad.equals(list);
  795. if(ret){
  796. System.out.println("两个相同");
  797. }else{
  798. System.out.println("两个不同");
  799. }
  800. //get方法——获取列表指定位置的元素
  801. List<String> list = new ArrayList<String>();
  802. list.add("保护环境");
  803. list.add("爱护地球");
  804. list.add("从我做起");
  805. String ret = list.get(1);
  806. System.out.println(ret);
  807. //set方法——替换列表中指定位置的元素
  808. List<String> list = new ArrayList<String>();
  809. list.add("保护环境");
  810. list.add("爱护地球");
  811. list.add("从我做起");
  812. String ret = list.set(1, "少用塑料袋");
  813. System.out.println(ret);;
  814. System.out.println(list);
  815. //hashCode方法——返回列表的哈希码值
  816. List<String> list = new ArrayList<String>();
  817. list.add("保护环境");
  818. list.add("爱护地球");
  819. list.add("从我做起");
  820. int ret = list.hashCode();
  821. System.out.println(ret);
  822. //indexOf方法——返回第一次出现指定元素的位置
  823. List<String> list = new ArrayList<String>();
  824. list.add("保护环境");
  825. list.add("爱护地球");
  826. list.add("从我做起");
  827. int index = list.indexOf("从我做起");
  828. System.out.println(index);
  829. //lastIndexOf方法——返回最后一次出现指定元素的位置
  830. List<String> list = new ArrayList<String>();
  831. list.add("保护环境");
  832. list.add("爱护地球");
  833. list.add("从我做起");
  834. int index = list.lastIndexOf("从我做起");
  835. System.out.println(index);
  836. //isEmpty方法——判断集合是否为空
  837. List<String> list = new ArrayList<String>();
  838. list.add("保护环境");
  839. list.add("爱护地球");
  840. list.add("从我做起");
  841. boolean empty = list.isEmpty();
  842. System.out.println(empty);
  843. //iterator方法——返回迭代器
  844. List<String> list = new ArrayList<String>();
  845. list.add("保护环境");
  846. list.add("爱护地球");
  847. list.add("从我做起");
  848. Iterator<String> it = list.iterator();
  849. while (it.hasNext()) {
  850. System.out.println(it.next());
  851. }
  852. //listIterator方法——返回列表迭代器
  853. ArrayList list = new ArrayList();
  854. for (int i = 0; i < 5; i++) {
  855. list.add("JAVA参考大全"+i);
  856. }
  857. ListIterator li = list.listIterator();
  858. while (li.hasNext()) {
  859. System.out.println(li.next());
  860. }
  861. //remove方法——移出列表中的指定元素
  862. List<String> list = new ArrayList<String>();
  863. list.add("保护环境");
  864. list.add("爱护地球");
  865. list.add("从我做起");
  866. String str = list.remove(1);
  867. System.out.println(str);
  868. Iterator<String> it = list.iterator();
  869. while (it.hasNext()) {
  870. System.out.println(it.next());
  871. }
  872. //removeAll方法——从列表中移除指定collection中包含的所有元素
  873. List<String> list = new ArrayList<String>();
  874. list.add("保护环境");
  875. list.add("爱护地球");
  876. list.add("从我做起");
  877. List<String> list1 = new ArrayList<String>();
  878. list1.add("保护环境");
  879. list1.add("爱护地球");
  880. boolean ret = list1.removeAll(list);
  881. System.out.println(ret);
  882. //retainAll方法——保留指定collection中包含的所有元素
  883. List<String> list = new ArrayList<String>();
  884. list.add("第一个");
  885. list.add("第二个");
  886. list.add("第三个");
  887. List<String> list1 = new ArrayList<String>();
  888. list1.add("第一个");
  889. list1.add("第三个");
  890. boolean ret = list.retainAll(list1);
  891. System.out.println(ret);
  892. //size方法——返回列表中元素的个数
  893. List<String> list = new ArrayList<String>();
  894. list.add("第一个");
  895. list.add("第二个");
  896. list.add("第三个");
  897. int listSize = list.size();
  898. System.out.println(listSize);
  899. //subList方法——获取列表中指定范围的子列表
  900. List<String> list = new ArrayList<String>();
  901. list.add("第一个");
  902. list.add("第二个");
  903. list.add("第三个");
  904. list.add("第四个");
  905. list.add("第五个");
  906. List<String> subList = list.subList(3, 5);
  907. Iterator<String> iterator = subList.iterator();
  908. while (iterator.hasNext()) {
  909. System.out.println(iterator.next());
  910. }
  911. //toArray方法——返回所有元素的数组
  912. List<String> list = new ArrayList<String>();
  913. list.add("第一个");
  914. list.add("第二个");
  915. list.add("第三个");
  916. list.add("第四个");
  917. list.add("第五个");
  918. Object[] arr = list.toArray();
  919. for (int i = 0; i < arr.length; i++) {
  920. System.out.println(arr[i]);
  921. }
  922. //javautilMap——映射集合类
  923. //clear方法——移除所有映射关系
  924. Map map = new HashMap();
  925. map.put("昨天", "定制目录");
  926. map.put("今天", "开始写作");
  927. map.put("明天", "当然继续写作");
  928. System.out.println(map.size());
  929. map.clear();
  930. System.out.println(map.size());
  931. //containsKey方法——判断是否包含指定的键名
  932. Map map = new HashMap();
  933. map.put("apple", "新鲜苹果");
  934. map.put("compiter", "配置优良的计算机");
  935. map.put("book", "推挤成山的图书");
  936. String key = "book";
  937. boolean contains = map.containsKey(key);
  938. System.out.println(contains);
  939. //containsValue方法——判断是否包含指定的键值
  940. Map map = new HashMap();
  941. map.put("apple", "新鲜苹果");
  942. map.put("compiter", "配置优良的计算机");
  943. map.put("book", "推挤成山的图书");
  944. String value = "新鲜苹果";
  945. boolean contains = map.containsValue(value);
  946. System.out.println(contains);
  947. //equals方法——判断是否与指定的对象相同
  948. Map map = new HashMap();
  949. map.put("apple", "新鲜苹果");
  950. map.put("compiter", "配置优良的计算机");
  951. map.put("book", "推挤成山的图书");
  952. Map map2 = new HashMap();
  953. map2.put("apple", "新鲜苹果");
  954. map2.put("compiter", "配置优良的计算机");
  955. map2.put("book", "推挤成山的图书");
  956. boolean contains = map.equals(map2);
  957. System.out.println(contains);
  958. //get方法——返回指定键所映射的值
  959. Map map = new HashMap();
  960. map.put("apple", "新鲜苹果");
  961. map.put("compiter", "配置优良的计算机");
  962. map.put("book", "推挤成山的图书");
  963. Object get = map.get("apple");
  964. System.out.println(get);
  965. //isEmpty方法——判断是否为空
  966. Map map = new HashMap();
  967. System.out.println("是否为空"+map.isEmpty());
  968. map.put("apple", "新鲜苹果");
  969. map.put("compiter", "配置优良的计算机");
  970. map.put("book", "推挤成山的图书");
  971. System.out.println("是否为空"+map.isEmpty());
  972. //keySet方法——获取Map集合的所有key
  973. Map map = new HashMap();
  974. map.put("apple", "新鲜苹果");
  975. map.put("compiter", "配置优良的计算机");
  976. map.put("book", "推挤成山的图书");
  977. Set keySet = map.keySet();
  978. for (Object object : keySet) {
  979. System.out.println(object);
  980. }
  981. //put方法——向指定索引位置添加对象
  982. Map map = new HashMap();
  983. map.put("apple", "新鲜苹果");
  984. map.put("compiter", "配置优良的计算机");
  985. map.put("book", "推挤成山的图书");
  986. int size = map.size();
  987. System.out.println(size);
  988. //putAll方法——追加另一个Map对象到当前Map集合
  989. Map map = new HashMap();
  990. map.put("apple", "新鲜苹果");
  991. map.put("compiter", "配置优良的计算机");
  992. map.put("book", "推挤成山的图书");
  993. Map map1 = new HashMap();
  994. map1.put("apple1", "新鲜苹果");
  995. map1.put("compiter1", "配置优良的计算机");
  996. map1.put("book1", "推挤成山的图书");
  997. map1.putAll(map);
  998. System.out.println(map1);
  999. //remove方法——移除Map集合中指定键名的内容
  1000. Map map = new HashMap();
  1001. map.put("apple", "新鲜苹果");
  1002. map.put("compiter", "配置优良的计算机");
  1003. map.put("book", "推挤成山的图书");
  1004. map.remove("apple");
  1005. System.out.println(map);
  1006. //size方法——获取Map集合类的大小
  1007. Map map = new HashMap();
  1008. map.put("apple", "新鲜苹果");
  1009. map.put("compiter", "配置优良的计算机");
  1010. map.put("book", "推挤成山的图书");
  1011. System.out.println(map.size());
  1012. //values方法——获取Map集合中的所有键值对象
  1013. Map map = new HashMap();
  1014. map.put("apple", "新鲜苹果");
  1015. map.put("compiter", "配置优良的计算机");
  1016. map.put("book", "推挤成山的图书");
  1017. Collection values = map.values();
  1018. for (Object object : values) {
  1019. System.out.println(object);
  1020. }
  1021. //javautilSet——无重复元素集合类
  1022. //add方法——向Set集合中添加对象
  1023. HashSet set = new HashSet();
  1024. set.add("a");
  1025. set.add("b");
  1026. set.add("c");
  1027. int size = set.size();
  1028. System.out.println(size);
  1029. //addAll方法——向Set集合添加另一个集合的所有内容
  1030. HashSet set = new HashSet();
  1031. set.add("a");
  1032. set.add("b");
  1033. set.add("c");
  1034. HashSet set1 = new HashSet();
  1035. set1.add("a1");
  1036. set1.add("b1");
  1037. set1.add("c1");
  1038. set1.addAll(set);
  1039. System.out.println(set1);
  1040. //clear方法——从Set集合中移除所有内容
  1041. HashSet set = new HashSet();
  1042. set.add("a");
  1043. set.add("b");
  1044. set.add("c");
  1045. int size = set.size();
  1046. System.out.println(size);
  1047. set.clear();
  1048. size = set.size();
  1049. System.out.println(size);
  1050. //contains方法——判断Set集合是否包含指定对象
  1051. HashSet set = new HashSet();
  1052. set.add("a");
  1053. set.add("b");
  1054. set.add("c");
  1055. boolean contains = set.contains("a");
  1056. System.out.println(contains);
  1057. //containsAll方法——判断Set集合是否包含另一个集合中的全部对象
  1058. HashSet set = new HashSet();
  1059. set.add("a");
  1060. set.add("b");
  1061. set.add("c");
  1062. ArrayList list = new ArrayList();
  1063. list.add("a");
  1064. list.add("b");
  1065. boolean contains = set.containsAll(list);
  1066. System.out.println(contains);
  1067. //equals方法——比较指定对象与Set集合对象是否相等
  1068. HashSet set = new HashSet();
  1069. set.add("a");
  1070. set.add("b");
  1071. set.add("c");
  1072. HashSet set1 = new HashSet();
  1073. set1.add("a");
  1074. set1.add("b");
  1075. set1.add("c");
  1076. boolean contains = set1.equals(set);
  1077. System.out.println(contains);
  1078. //isEmpty方法——判断Set集合是否为空
  1079. HashSet set = new HashSet();
  1080. System.out.println("是否为空"+set.isEmpty());
  1081. set.add("a");
  1082. set.add("b");
  1083. set.add("c");
  1084. System.out.println("是否为空"+set.isEmpty());
  1085. //iterator方法——获取Set集合的迭代器
  1086. HashSet set = new HashSet();
  1087. set.add("a");
  1088. set.add("b");
  1089. set.add("c");
  1090. Iterator iterator = set.iterator();
  1091. while (iterator.hasNext()) {
  1092. System.out.println(iterator.next());
  1093. }
  1094. //remove方法——移除Set集合中的指定对象
  1095. HashSet set = new HashSet();
  1096. set.add("a");
  1097. set.add("b");
  1098. set.add("c");
  1099. System.out.println(set.size());
  1100. set.remove("a");
  1101. System.out.println(set.size());
  1102. //removeAll方法——移除另一个集合所包含的所有内容
  1103. HashSet set = new HashSet();
  1104. set.add("a");
  1105. set.add("b");
  1106. set.add("c");
  1107. System.out.println(set.size());
  1108. set.removeAll(set);
  1109. System.out.println(set.size());
  1110. //retainAll方法——保留另一个集合所包含的所有内容
  1111. HashSet set = new HashSet();
  1112. set.add("a");
  1113. set.add("b");
  1114. set.add("c");
  1115. System.out.println(set.size());
  1116. set.retainAll(set);
  1117. System.out.println(set.size());
  1118. //size方法——获取Set集合类的大小
  1119. HashSet set = new HashSet();
  1120. set.add("a");
  1121. set.add("b");
  1122. set.add("c");
  1123. System.out.println(set.size());
  1124. //toArray方法——用Set集合的所有对象创建数组
  1125. HashSet set = new HashSet();
  1126. set.add("a");
  1127. set.add("b");
  1128. set.add("c");
  1129. Object[] toArray = set.toArray();
  1130. System.out.println(toArray.length);
  1131. <!--日期与时间 ->
  1132. //javautilDate——日期/时间类
  1133. //after方法——测试当前日期是否在指定的日期之后
  1134. Date beforeDate = new Date(302672606563L);
  1135. Date nowDate = new Date();
  1136. boolean flag = nowDate.after(beforeDate);
  1137. System.out.println(flag);
  1138. //before方法——测试当前日期是否在指定的日期之前
  1139. Date beforeDate = new Date(302672606563L);
  1140. Date nowDate = new Date();
  1141. boolean flag = nowDate.before(beforeDate);
  1142. System.out.println(flag);
  1143. //getTime方法——获得毫秒数
  1144. Date date = new Date();
  1145. long value = date.getTime();
  1146. System.out.println(value);
  1147. //setTime方法——设置当前Date对象所表示的日期/时间值
  1148. Date date = new Date();
  1149. date.setTime(302676633308L);
  1150. System.out.println(date);
  1151. //javautilLocale——语言环境相关类
  1152. //getAvailableLocales方法——获得所有已安装语言环境的数组
  1153. Locale[] locales = Locale.getAvailableLocales();
  1154. for (int i = 0; i < locales.length; i++) {
  1155. System.out.println(locales[i]);
  1156. }
  1157. //getCountry方法——获得当前语言环境的国家/地区代码
  1158. Locale[] locales = Locale.getAvailableLocales();
  1159. for (int i = 0; i < locales.length; i++) {
  1160. System.out.println(locales[i]+":"+locales[i].getCountry());
  1161. }
  1162. //getDefault方法——获得默认语言环境
  1163. Locale locale = Locale.getDefault();
  1164. System.out.println(locale);
  1165. //getDisplayCountry方法——获得语言环境国家/地区名
  1166. Locale locale = Locale.getDefault();
  1167. String displayCountry = locale.getDisplayCountry();
  1168. System.out.println(displayCountry);
  1169. //getDisplayLanguage方法——获取语言环境的语言名
  1170. Locale locale = Locale.getDefault();
  1171. String displayLanguage = locale.getDisplayLanguage();
  1172. System.out.println(displayLanguage);
  1173. //getDisplayName方法——获取语言环境名
  1174. Locale locale = Locale.getDefault();
  1175. String displayName = locale.getDisplayName();
  1176. System.out.println(displayName);
  1177. //javatextDateFormat——格式化时间类
  1178. //getDateInstance方法——获取日期格式器
  1179. Date date = new Date();
  1180. DateFormat df = DateFormat.getDateInstance();
  1181. String formatDate = df.format(date);
  1182. System.out.println(formatDate);
  1183. //getDateTimeInstance方法——获取日期/时间格式器
  1184. Date date = new Date();
  1185. DateFormat df = DateFormat.getDateTimeInstance();
  1186. String formatDate = df.format(date);
  1187. System.out.println(formatDate);
  1188. //getInstance方法——获取默认日期/时间格式器
  1189. Date date = new Date();
  1190. DateFormat df = DateFormat.getInstance();
  1191. String formatDate = df.format(date);
  1192. System.out.println(formatDate);
  1193. //parse方法——将字符串类型的日期/时间解析为Date类型
  1194. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1195. String stringDate = "2011-3-14 9:6:20";
  1196. Date date = df.parse(stringDate);
  1197. System.out.println(date);
  1198. <!--文件处理 ->
  1199. //javaioFile——文件类
  1200. //File构造方法
  1201. File file = new File("c:/test.txt");
  1202. File folder = new File("c:/test1");
  1203. //canExecute方法——判断文件是否可以运行
  1204. File file = new File("c:/test.txt");
  1205. System.out.println(file.canExecute());
  1206. //canRead方法——判断文件是否可以读取
  1207. File file = new File("c:/test.txt");
  1208. System.out.println(file.canRead());
  1209. //canWrite方法——判断文件是否可以写入
  1210. File file = new File("c:/test.txt");
  1211. System.out.println(file.canWrite());
  1212. //compareTo方法——比较文件或文件夹名称的关系
  1213. File file = new File("c:/test.txt");
  1214. File file1 = new File("c:/test1.txt");
  1215. System.out.println(file.compareTo(file1));
  1216. //createNewFile方法——创建空文件或者空文件夹
  1217. File file = new File("c:/test.txt");
  1218. file.createNewFile();
  1219. //createTempFile方法——创建临时文件
  1220. File tempFile = File.createTempFile("abc", null);
  1221. System.out.println(tempFile);
  1222. //delete方法——删除文件或者空文件夹
  1223. File tempFile = File.createTempFile("abc", null);
  1224. tempFile.delete();
  1225. //deleteOnExit方法——虚拟机正常终止时逆序删除文件
  1226. File tempFile = File.createTempFile("abc", null);
  1227. tempFile.deleteOnExit();
  1228. //equals方法——判断两个文件的路径名是否相同
  1229. File file = new File("c:/test.txt");
  1230. File file1 = new File("c:/test.txt");
  1231. System.out.println(file.equals(file1));
  1232. //exists方法——测试文件或者文件夹是否存在
  1233. File file = new File("c:/test.txt");
  1234. System.out.println(file.exists());
  1235. //getAbsoluteFile方法——获得使用绝对路径创建的文件对象
  1236. File file = new File("c:/test.txt");
  1237. System.out.println(file.getAbsoluteFile());
  1238. //getAbsolutePath方法——获得表示文件绝对路径的字符串
  1239. File file = new File("c:/test.txt");
  1240. System.out.println(file.getAbsolutePath());
  1241. //getCanonicalFile方法——获得使用规范形式的文件路径创建的文件对象
  1242. File file = new File("c:/test.txt");
  1243. System.out.println(file.getCanonicalFile());
  1244. //getCanonicalPath方法——获得表示文件规范路径的字符串
  1245. File file = new File("c:/test.txt");
  1246. System.out.println(file.getCanonicalPath());
  1247. //getFreeSpace方法——获取指定的分区中未分配的字节数
  1248. File file = new File("c:");
  1249. long freeSize = file.getFreeSpace();
  1250. long gb = freeSize / 1000000000;
  1251. long mb = (freeSize-gb*1000000000)/1000000;
  1252. System.out.println(gb+"GB"+mb+"MB");
  1253. //getName方法——获得文件或者文件夹的名称
  1254. File file = new File("c:/test.txt");
  1255. System.out.println(file.getName());
  1256. //getParent方法——获取父文件夹的路径名字符串
  1257. File file = new File("c:/test.txt");
  1258. System.out.println(file.getParent());
  1259. //getParentFile方法——获取父文件夹的抽象路径名
  1260. File file = new File("c:/test.txt");
  1261. System.out.println(file.getParentFile());
  1262. //getPath方法——获取文件路径
  1263. File file = new File("c:/test.txt");
  1264. System.out.println(file.getPath());
  1265. //getTotalSpace方法——获取路径名指定的分区大小
  1266. File file = new File("c:/test.txt");
  1267. System.out.println(file.getTotalSpace());
  1268. //getUsableSpace方法——获取分区上的可用字节数
  1269. File file = new File("c:/test.txt");
  1270. System.out.println(file.getUsableSpace());
  1271. //hashCode方法——获取路径名的哈希码
  1272. File file = new File("c:/test.txt");
  1273. System.out.println(file.hashCode());
  1274. //isAbsolute方法——判断路径名是否为绝对路径名
  1275. File file = new File("c:/test.txt");
  1276. System.out.println(file.isAbsolute());
  1277. //isDirectory方法——判断是否为文件夹
  1278. File file = new File("c:/test.txt");
  1279. System.out.println(file.isDirectory());
  1280. //isFile方法——判断是否是一个标准文件
  1281. File file = new File("c:/test.txt");
  1282. System.out.println(file.isFile());
  1283. //isHidden方法——判断是否是隐藏文件
  1284. File file = new File("c:/test.txt");
  1285. System.out.println(file.isHidden());
  1286. //lastModified方法——获取文件的最后修改时间
  1287. File file = new File("c:/test.txt");
  1288. long time = file.lastModified();
  1289. Date date = new Date(time);
  1290. System.out.println(date);
  1291. //length方法——获取文件的长度
  1292. File file = new File("c:/test.txt");
  1293. System.out.println(file.length());
  1294. //list方法——返回字符串数组
  1295. File root = new File("c:");
  1296. String[] files = root.list();
  1297. for (String string : files) {
  1298. System.out.println(string);
  1299. }
  1300. //listFiles方法——获取路径名数组
  1301. File root = new File("c:");
  1302. File[] files = root.listFiles(new FileFilter() {
  1303. @Override
  1304. public boolean accept(File pathname) {
  1305. if(pathname.isHidden()){
  1306. return true;
  1307. }
  1308. return false;
  1309. }
  1310. });
  1311. for (File file : files) {
  1312. System.out.println(file);
  1313. }
  1314. //listRoots方法——列出可用的文件系统根
  1315. File[] roots = File.listRoots();
  1316. for (File file : roots) {
  1317. System.out.println(file);
  1318. }
  1319. //mkdir方法——创建文件夹
  1320. File file = new File("c:/test");
  1321. file.mkdir();
  1322. //mkdirs方法——创建文件夹
  1323. File file = new File("c:/test/test");
  1324. file.mkdirs();
  1325. //renameTo方法——重新命名文件
  1326. File file = new File("c:/test");
  1327. file.renameTo(new File("c:/test1"));
  1328. //setExecutable方法——设置执行权限
  1329. File file = new File("c:/test.txt");
  1330. file.setExecutable(false);
  1331. //setLastModified方法——设置文件或者文件夹的最后修改时间
  1332. File file = new File("c:/test.txt");
  1333. GregorianCalendar calendar = new GregorianCalendar(2000,0,1);
  1334. Date date = calendar.getTime();
  1335. file.setLastModified(date.getTime());
  1336. //setReadable方法——设置读权限
  1337. File file = new File("c:/test.txt");
  1338. file.setReadable(true,true);
  1339. //setReadOnly方法——设置文件或文件夹为只读
  1340. File file = new File("c:/test.txt");
  1341. file.setReadOnly();
  1342. //setWritable方法——设置文件的可写属性
  1343. File file = new File("c:/test.txt");
  1344. file.setWritable(true);
  1345. //toString方法——返回路径名的字符串形式
  1346. File file = new File("c:/test.txt");
  1347. System.out.println(file.toString());
  1348. //toURI方法——获取文件的URI
  1349. File file = new File("c:/test.txt");
  1350. System.out.println(file.toURI());
  1351. //javaioFileInputStream类——文件输入流
  1352. //available方法——估计剩余的字节数
  1353. File file = new File("C:/test.txt");
  1354. FileInputStream in = new FileInputStream(file);
  1355. System.out.println(in.available());
  1356. //close方法——关闭文件输入流
  1357. File file = new File("C:/test.txt");
  1358. FileInputStream in = new FileInputStream(file);
  1359. for (int i = 0; i < file.length(); i++) {
  1360. System.out.println(in.read());
  1361. }
  1362. in.close();
  1363. //getChannel方法——返回FileChannel对象
  1364. File file = new File("C:/test.txt");
  1365. FileInputStream in = new FileInputStream(file);
  1366. FileChannel channel = in.getChannel();
  1367. in.close();
  1368. //getFD方法——返回FileDescriptor对象
  1369. File file = new File("C:/test.txt");
  1370. FileInputStream in = new FileInputStream(file);
  1371. FileDescriptor fd = in.getFD();
  1372. in.close();
  1373. //read方法——从文件字节输入流中读取数据
  1374. File file = new File("C:/test.txt");
  1375. FileInputStream in = new FileInputStream(file);
  1376. int temp = 0;
  1377. while((temp=in.read())!=-1){
  1378. System.out.println(temp);
  1379. }
  1380. in.close();
  1381. //skip方法——从输入流中跳过并丢弃n字节的数据
  1382. File file = new File("C:/test.txt");
  1383. FileInputStream in = new FileInputStream(file);
  1384. in.skip(10);
  1385. int length = 0;
  1386. while((length = in.read())!=-1){
  1387. System.out.println(length);
  1388. }
  1389. in.close();
  1390. //javaioFileOutputStream类——文件输出流
  1391. //close方法——关闭文件输出流
  1392. File file = new File("C:/test.txt");
  1393. FileOutputStream out = new FileOutputStream(file);
  1394. out.write("JAVA参考手册".getBytes());
  1395. out.close();
  1396. //getChannel方法——返回FileChannel对象
  1397. File file = new File("C:/test.txt");
  1398. FileOutputStream out = new FileOutputStream(file);
  1399. FileChannel channel = out.getChannel();
  1400. out.close();
  1401. //getFD方法——返回FileDescriptor对象
  1402. File file = new File("C:/test.txt");
  1403. FileOutputStream out = new FileOutputStream(file);
  1404. out.getFD();
  1405. out.close();
  1406. //write方法——向文件中写入数据
  1407. File inputfile = new File("C:/test.txt");
  1408. File outputfile = new File("C:/test1.txt");
  1409. FileInputStream in = new FileInputStream(inputfile);
  1410. FileOutputStream out = new FileOutputStream(outputfile);
  1411. byte[] b = new byte[1024];
  1412. in.read(b);
  1413. out.write(b);
  1414. out.close();
  1415. in.close();
  1416. //javaioFileReader类——文件的字符输入流
  1417. //close方法——关闭字符输入流
  1418. File file = new File("C:/test.txt");
  1419. FileReader reader = new FileReader(file);
  1420. char[] data = new char[512];
  1421. int rs = 0;
  1422. while((rs = reader.read(data))>0){
  1423. System.out.println(new String(data,0,rs));
  1424. }
  1425. reader.close();
  1426. //read方法——读取字符数据
  1427. File file = new File("C:/test.txt");
  1428. FileReader reader = new FileReader(file);
  1429. char[] data = new char[512];
  1430. int rs = 0;
  1431. while((rs = reader.read(data))>0){
  1432. System.out.println(new String(data,0,rs));
  1433. }
  1434. reader.close();
  1435. //javaioFileWriter类——文件的字符输出流
  1436. //close方法——关闭字符输出流
  1437. File file = new File("C:/test.txt");
  1438. FileWriter writer = new FileWriter(file);
  1439. writer.write("JAVA参考手册");
  1440. writer.close();
  1441. //flush方法——刷新缓冲区
  1442. File file = new File("C:/test.txt");
  1443. String[] contents = {"JAVA","PHP","C"};
  1444. FileWriter writer = new FileWriter(file);
  1445. for (String string : contents) {
  1446. writer.write(string);
  1447. }
  1448. writer.flush();
  1449. writer.close();
  1450. //write方法——向字符输出流写数据
  1451. File file = new File("C:/test.txt");
  1452. FileWriter writer = new FileWriter(file);
  1453. writer.write("JAVA".toCharArray());
  1454. writer.flush();
  1455. writer.close();
  1456. <!--图片处理 ->
  1457. //javaximageioImageIO——图像输入/输出类
  1458. //createImageInputStream方法——创建一个ImageInputStream对象
  1459. File file = new File("src/img.gif");
  1460. ImageInputStream iis = ImageIO.createImageInputStream(file);
  1461. JOptionPane.showMessageDialog(null, "创建成功");
  1462. iis.close();
  1463. //getImageReaders方法——获得包含ImageReader的迭代器对象
  1464. File file = new File("src/img.gif");
  1465. ImageInputStream iis = ImageIO.createImageInputStream(file);
  1466. Iterator<ImageReader> it = ImageIO.getImageReaders(file);
  1467. if(it.hasNext()){
  1468. JOptionPane.showMessageDialog(null, "成功");
  1469. }else{
  1470. JOptionPane.showMessageDialog(null, "失败");
  1471. }
  1472. iis.close();
  1473. //getImageReadersByMIMEType方法——获得可以解码MIME类型的迭代器
  1474. Iterator<ImageReader> it = ImageIO.getImageReadersByMIMEType("image/jpg");
  1475. boolean isRead = it.hasNext();
  1476. System.out.println(isRead);
  1477. //getImageReadersBySuffix方法——获得可以解码指定文件后缀的迭代器
  1478. String suffix = "gif";
  1479. Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(suffix);
  1480. JOptionPane.showMessageDialog(null, "后缀"+ suffix + "解码成功");
  1481. //getImageWritersByFormatName方法——获得可以解码指定格式的迭代器
  1482. String formatName = "jpeg";
  1483. Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(formatName);
  1484. ImageWriter imageWriter = (ImageWriter)it.next();
  1485. JOptionPane.showMessageDialog(null, "格式"+formatName+"解码成功");
  1486. //read方法——读取数据到BufferedImage对象
  1487. //从指定文件对象读取数据获得一个BufferedImage对象
  1488. //write方法——将图像以文件的形式写入磁盘
  1489. //用于将图像以文件的形式写入磁盘
  1490. //javaximageioImageReader——图像的字符输入流
  1491. //getFormatName方法——获得文件的格式名称
  1492. File file = new File("src/img.gif");
  1493. ImageInputStream iis = ImageIO.createImageInputStream(file);
  1494. Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
  1495. if(it.hasNext()){
  1496. ImageReader reader = (ImageReader)it.next();
  1497. System.out.println("格式"+reader.getFormatName());
  1498. }
  1499. iis.close();
  1500. //getNumImages方法——获得当前输入源中可用的图像数
  1501. String fileName = "src/img.gif";
  1502. FileInputStream fin = new FileInputStream(fileName);
  1503. Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("gif");
  1504. ImageReader imageReader = readers.next();
  1505. ImageInputStream iis = ImageIO.createImageInputStream(fin);
  1506. imageReader.setInput(iis,false);
  1507. int num = imageReader.getNumImages(true);
  1508. JOptionPane.showMessageDialog(null, "当前输入源"+num+"有可用图像");
  1509. iis.close();
  1510. fin.close();
  1511. //read方法——获得一个BufferedImage对象
  1512. String fileName = "src/img.gif";
  1513. FileInputStream fin = new FileInputStream(fileName);
  1514. Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("gif");
  1515. ImageReader imageReader = readers.next();
  1516. ImageInputStream iis = ImageIO.createImageInputStream(fin);
  1517. imageReader.setInput(iis,false);
  1518. BufferedImage bufferedImage = imageReader.read(0);
  1519. iis.close();
  1520. fin.close();
  1521. //setInput方法——设置ImageReader的输入源
  1522. String fileName = "src/img.gif";
  1523. FileInputStream fin = new FileInputStream(fileName);
  1524. Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("gif");
  1525. ImageReader imageReader = readers.next();
  1526. ImageInputStream iis = ImageIO.createImageInputStream(fin);
  1527. imageReader.setInput(iis,false);
  1528. BufferedImage bufferedImage = imageReader.read(0);
  1529. iis.close();
  1530. fin.close();
  1531. //javaximageioImageWriter——图像输出流
  1532. //setOutput方法——设置输出目标
  1533. Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpg");
  1534. ImageWriter imageWriter = it.next();
  1535. File file = new File("src/img.jpg");
  1536. ImageOutputStream ios = ImageIO.createImageOutputStream(file);
  1537. imageWriter.setOutput(ios);
  1538. Object obj = imageWriter.getOutput();
  1539. if(obj!=null){
  1540. JOptionPane.showMessageDialog(null, "成功");
  1541. }
  1542. //write方法——将完整图像流添加到输出流中
  1543. BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
  1544. Graphics2D g2 = image.createGraphics();
  1545. g2.setColor(Color.BLUE);
  1546. g2.fillRect(20, 20, 90, 90);
  1547. g2.setColor(Color.RED);
  1548. g2.fillOval(95, 95, 90, 90);
  1549. Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpg");
  1550. ImageWriter imageWriter = it.next();
  1551. File file = new File("src/img.jpg");
  1552. ImageOutputStream ios = ImageIO.createImageOutputStream(file);
  1553. imageWriter.setOutput(ios);
  1554. imageWriter.write(image);
  1555. ios.close();
  1556. <!--窗体和桌面面板 ->
  1557. //javaxswingJFrame窗体
  1558. //JFrame构造方法
  1559. JFrame frame = new JFrame();
  1560. frame.setBounds(400,320,300,200);
  1561. frame.setVisible(true);
  1562. //getContentPane方法——获得JFrame窗体的内容窗格
  1563. JFrame frame = new JFrame("使用getContentPane");
  1564. Container c = frame.getContentPane();
  1565. c.setLayout(new FlowLayout());
  1566. JButton btn_1 = new JButton("按钮一");
  1567. JButton btn_2 = new JButton("按钮二");
  1568. c.add(btn_1);
  1569. c.add(btn_2);
  1570. frame.setBounds(400,320,300,200);
  1571. frame.setVisible(true);
  1572. //getDefaultCloseOperation方法——获得指示窗体关闭操作的整数
  1573. JFrame frame = new JFrame("窗体默认关闭操作");
  1574. System.out.println("DO_NOTHING_ON_CLOSE的值 \t"+WindowConstants.DO_NOTHING_ON_CLOSE);
  1575. System.out.println("HIDE_ON_CLOSE的值 \t"+WindowConstants.HIDE_ON_CLOSE);
  1576. System.out.println("DISPOSE_ON_CLOSE的值\t"+WindowConstants.DISPOSE_ON_CLOSE);
  1577. System.out.println("EXIT_ON_CLOSE的值 \t"+WindowConstants.EXIT_ON_CLOSE);
  1578. System.out.println();
  1579. System.out.println("默认关闭操作整数\t"+frame.getDefaultCloseOperation());
  1580. frame.dispose();
  1581. //getGlassPane方法——返回当前窗体的玻璃窗格对象
  1582. JFrame frame = new JFrame("在玻璃面板上添加控件");
  1583. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1584. JPanel glassPanel = new JPanel();
  1585. glassPanel.setLayout(new FlowLayout());
  1586. JButton btn_1 = new JButton("按钮一");
  1587. JButton btn_2 = new JButton("按钮二");
  1588. glassPanel.add(btn_1);
  1589. glassPanel.add(btn_2);
  1590. frame.setGlassPane(glassPanel);
  1591. Component c = frame.getGlassPane();
  1592. c.setVisible(true);
  1593. frame.setBounds(400,320,300,200);
  1594. frame.setVisible(true);
  1595. //getGraphics方法——创建绘图上下文对象
  1596. JFrame frame = new JFrame("绘制文本和图形");
  1597. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1598. frame.setBounds(400,320,300,200);
  1599. frame.setVisible(true);
  1600. Thread.sleep(1000);
  1601. Graphics g = frame.getGraphics();
  1602. g.drawString("graphics对象绘制文本", 70, 70);
  1603. g.drawOval(100, 100, 80, 80);
  1604. //getRootPane方法——获得当前窗体的根窗格对象
  1605. JFrame frame = new JFrame("设置窗体的默认按钮");
  1606. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1607. JButton ok = new JButton("确定");
  1608. JButton cancel = new JButton("退出");
  1609. JPanel panel = new JPanel();
  1610. panel.add(ok);
  1611. panel.add(cancel);
  1612. cancel.addActionListener(new ActionListener() {
  1613. @Override
  1614. public void actionPerformed(ActionEvent e) {
  1615. System.exit(0);
  1616. }
  1617. });
  1618. JRootPane root = frame.getRootPane();
  1619. root.setDefaultButton(cancel);
  1620. Container content = frame.getContentPane();
  1621. content.add(panel);
  1622. frame.setBounds(400,320,300,220);
  1623. frame.setVisible(true);
  1624. //remove方法——从当前窗体中移除指定的控件
  1625. final JFrame frame = new JFrame("移除窗体上的指定控件");
  1626. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1627. JButton centerButton = new JButton("移除面板的设置按钮");
  1628. JButton removeButton = new JButton("移除窗体上方的控件");
  1629. final JPanel centerPanel = new JPanel();
  1630. JPanel southPanel = new JPanel();
  1631. centerPanel.add(centerButton);
  1632. southPanel.add(removeButton);
  1633. removeButton.addActionListener(new ActionListener() {
  1634. @Override
  1635. public void actionPerformed(ActionEvent e) {
  1636. frame.remove(centerPanel);
  1637. frame.pack();
  1638. }
  1639. });
  1640. Container content = frame.getContentPane();
  1641. content.add(centerPanel);
  1642. content.add(southPanel,BorderLayout.SOUTH);
  1643. frame.setBounds(400,320,300,220);
  1644. frame.setVisible(true);
  1645. //setContentPane方法——将指定容器设置为当前窗体的内容窗格
  1646. final JFrame frame = new JFrame("指定船体的内容窗格");
  1647. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1648. JLabel label = new JLabel("内容");
  1649. JTextField text = new JTextField(20);
  1650. JButton btn_1 = new JButton("确定");
  1651. JButton btn_2 = new JButton("取消");
  1652. JPanel panel = new JPanel();
  1653. panel.add(label);
  1654. panel.add(text);
  1655. panel.add(btn_1);
  1656. panel.add(btn_2);
  1657. frame.setContentPane(panel);
  1658. frame.setBounds(400,320,300,220);
  1659. frame.setVisible(true);
  1660. //setDefaultCloseOperation方法——设置close事件默认执行的操作
  1661. JFrame frame = new JFrame("设置窗体默认的关闭方式");
  1662. frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1663. frame.setBounds(400,320,300,220);
  1664. frame.setVisible(true);
  1665. //setGlassPane方法——设置当前窗体的玻璃窗格
  1666. JPanel glassPanel = new JPanel();
  1667. glassPanel.setLayout(new FlowLayout());
  1668. JLabel label = new JLabel("设置窗体玻璃窗格");
  1669. label.setFont(new Font("宋体", Font.BOLD, 26));
  1670. JButton btn_1 = new JButton("测 试");
  1671. JButton btn_2 = new JButton("退出");
  1672. glassPanel.add(label);
  1673. glassPanel.add(btn_1);
  1674. glassPanel.add(btn_2);
  1675. glassPanel.setVisible(true);
  1676. glassPanel.setBounds(400, 320, 300, 200);
  1677. //setJMenuBar方法——设置当前窗体的菜单栏
  1678. JFrame frame = new JFrame("设置窗体的菜单栏");
  1679. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1680. JMenuBar menubar = new JMenuBar();
  1681. JMenu menu = new JMenu("系统");
  1682. JMenuItem menuItem = new JMenuItem("退出");
  1683. menu.add(menuItem);
  1684. menubar.add(menu);
  1685. frame.setJMenuBar(menubar);
  1686. frame.setBounds(400,320,300,200);
  1687. frame.setVisible(true);
  1688. //setLayout方法——设置当前窗体的布局管理器
  1689. JFrame frame = new JFrame("使用setLayout方法");
  1690. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1691. Container c = frame.getContentPane();
  1692. frame.setLayout(new FlowLayout());
  1693. JButton btn_1 = new JButton("按钮一");
  1694. JButton btn_2 = new JButton("按钮二");
  1695. c.add(btn_1);
  1696. c.add(btn_2);
  1697. frame.setBounds(400,320,300,200);
  1698. frame.setVisible(true);
  1699. //javaxswingJDialog对话框窗体
  1700. //JDialog构造方法——创建对话框窗体
  1701. JDialog dialog = new JDialog();
  1702. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1703. dialog.setBounds(400,320,300,200);
  1704. dialog.setVisible(true);
  1705. //getContentPane方法——获得JDialog对话框窗体的内容窗格
  1706. JDialog dialog = new JDialog();
  1707. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1708. Container c = dialog.getContentPane();
  1709. c.setLayout(new FlowLayout());
  1710. JButton btn_ok = new JButton("OK");
  1711. JButton btn_canel = new JButton("Cancel");
  1712. c.add(btn_ok);
  1713. c.add(btn_canel);
  1714. dialog.setBounds(400,320,300,200);
  1715. dialog.setVisible(true);
  1716. //getDefaultCloseOperation方法——获得发起close事件时执行的操作
  1717. JDialog dialog = new JDialog();
  1718. System.out.println("DO_NOTHING_ON_CLOSE的值 \t"+WindowConstants.DO_NOTHING_ON_CLOSE);
  1719. System.out.println("HIDE_ON_CLOSE的值 \t"+WindowConstants.HIDE_ON_CLOSE);
  1720. System.out.println("DISPOSE_ON_CLOSE的值 \t"+WindowConstants.DISPOSE_ON_CLOSE);
  1721. System.out.println();
  1722. System.out.println("对话框默认关闭操作的整数 \t"+dialog.getDefaultCloseOperation());
  1723. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1724. System.out.println("对话框默认关闭操作的整数 \t"+dialog.getDefaultCloseOperation());
  1725. dialog.setBounds(400,260,300,200);
  1726. dialog.setVisible(true);
  1727. //getGlassPane方法——返回当前对话框窗体的玻璃窗格对象
  1728. JDialog dialog = new JDialog();
  1729. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1730. URL url = javaTest.class.getResource("src/img.gif");
  1731. ImageIcon icon = new ImageIcon(url);
  1732. JLabel label_icon = new JLabel(icon);
  1733. JPanel glassPanel = new JPanel();
  1734. glassPanel.setLayout(new BorderLayout());
  1735. glassPanel.add(label_icon);
  1736. dialog.setGlassPane(glassPanel);
  1737. Component c = dialog.getGlassPane();
  1738. c.setVisible(true);
  1739. JLabel label_finish = new JLabel("已完成");
  1740. label_finish.setFont(new Font("宋体",Font.BOLD,50));
  1741. dialog.getContentPane().add(label_finish);
  1742. dialog.setBounds(400,320,300,200);
  1743. dialog.setVisible(true);
  1744. Thread.sleep(1000);
  1745. c.setVisible(false);
  1746. //getGraphics方法——创建一个绘图上下文对象
  1747. JDialog dialog = new JDialog();
  1748. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1749. dialog.setBounds(400,320,300,200);
  1750. dialog.setVisible(true);
  1751. Thread.sleep(1000);
  1752. Graphics g = dialog.getGraphics();
  1753. g.drawOval(40, 50, 80, 80);
  1754. g.drawOval(110, 50, 80, 80);
  1755. g.drawOval(180, 50, 80, 80);
  1756. g.drawOval(80, 110, 80, 80);
  1757. g.drawOval(150, 110, 80, 80);
  1758. //remove方法——从当前对话框窗体中移除指定的控件
  1759. final JDialog dialog = new JDialog();
  1760. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1761. final JButton centerButton = new JButton("将要被移除放到对话框中心的按钮");
  1762. JButton southButton = new JButton("单击我可以移除对话框中心的按钮");
  1763. southButton.addActionListener(new ActionListener() {
  1764. @Override
  1765. public void actionPerformed(ActionEvent e) {
  1766. dialog.remove(centerButton);
  1767. dialog.repaint();
  1768. }
  1769. });
  1770. Container c = dialog.getContentPane();
  1771. c.add(centerButton,BorderLayout.CENTER);
  1772. c.add(southButton,BorderLayout.SOUTH);
  1773. dialog.setBounds(400,320,300,220);
  1774. dialog.setVisible(true);
  1775. dialog.setBounds(400,320,300,200);
  1776. dialog.setVisible(true);
  1777. //setContentPane方法——将指定容器设置为当前窗体的内容窗格
  1778. final JDialog dialog = new JDialog();
  1779. dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  1780. final JButton centerButton = new JButton("将要被移除放到对话框中心的按钮");
  1781. JButton southButton = new JButton("单击我可以移除对话框中心的按钮");
  1782. southButton.addActionListener(new ActionListener() {
  1783. @Override
  1784. public void actionPerformed(ActionEvent e) {
  1785. dialog.remove(centerButton);
  1786. dialog.repaint();
  1787. }
  1788. });
  1789. Container c = dialog.getContentPane();
  1790. c.add(centerButton,BorderLayout.CENTER);
  1791. c.add(southButton,BorderLayout.SOUTH);
  1792. dialog.setBounds(400,320,300,220);
  1793. dialog.setVisible(true);
  1794. dialog.setBounds(400,320,300,200);
  1795. dialog.setVisible(true);
  1796. //setDefaultCloseOperation方法——设置发起close事件时默认执行的操作
  1797. final JDialog dialog = new JDialog();
  1798. dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  1799. dialog.addWindowListener(new WindowAdapter() {
  1800. @Override
  1801. public void windowClosing(WindowEvent e) {
  1802. System.exit(0);
  1803. }
  1804. });
  1805. dialog.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,10,30));
  1806. dialog.getContentPane().add(new JButton("确定"));
  1807. dialog.getContentPane().add(new JButton("退出"));
  1808. dialog.setBounds(400,320,300,220);
  1809. dialog.setVisible(true);
  1810. dialog.setBounds(400,320,300,200);
  1811. dialog.setVisible(true);
  1812. //setGlassPane方法——设置当前对话框窗体的玻璃窗格
  1813. //设置当前对话框的玻璃窗格
  1814. //setJMenuBar方法——设置当前对话框窗体的菜单栏
  1815. //设置当前对话框窗体的菜单栏
  1816. //javaxswingJDesktopPane桌面面板
  1817. //JDesktopPane构造方法
  1818. //创建桌面面板对象,对象是内部窗体的容器
  1819. //getAllFrames方法——返回桌面中显示的所有JInternalFrame
  1820. //以数组的形式返回桌面中当前显示的所有JInternalFrame
  1821. //getSelectedFrame方法——获得当前被选择的内部窗体
  1822. //用于获得桌面面板中当前活动的JInternalFrame,当前被选中的内部窗体
  1823. //remove方法——移除指定的JInternalFrame
  1824. //用于从桌面面板中移除位于指定索引位置处的JInternalFrame
  1825. //removeAll方法——移除所有的JInternalFrame
  1826. //用于从桌面面板中移除所有的JInternalFrame
  1827. //selectFrame方法——选择下一个JInternalFrame
  1828. //用于选择此桌面面板中的下一个JInternalFrame
  1829. //setSelectedFrame方法——设置当前活动的JInternalFrame
  1830. //用于设置桌面面板当前获得JInternalFrame
  1831. //javaxswingJInternalFrame内部窗体
  1832. //JInternalFrame构造方法
  1833. //用于创建内部窗体,该窗体不能移除其所在桌面面板
  1834. //dispose方法——关闭内部窗体
  1835. //用于是内部窗体不可见、取消选定并关闭该内部窗体
  1836. //getContentPane方法——获得JInternalFrame窗体的内容窗格
  1837. //用于获得JInternalFrame窗体的内容窗格
  1838. //getFrameIcon方法——获取JInternalFrame标题栏上显示的图标
  1839. //用于获取JInternalFrame标题栏上显示的图标
  1840. //getTitle方法——获得内部窗体标题栏上显示的文本
  1841. //用于获得内部窗体标题了上显示的文本
  1842. //setClosable方法——设置是否可以关闭内部窗体
  1843. //用于设置是否可以通过某个操作关闭内部的窗体
  1844. //setFrameIcon方法——设置窗体标题栏上显示的图像
  1845. //用于设置要在此内部窗体标题栏上显示的图像
  1846. //setIconifiable方法——设置是否可以使内部窗体图标化
  1847. //用于设置是否可以通过某个操作使内部窗体图标化
  1848. //setMaximizable方法——设置是否可以使内部窗体最大化
  1849. //设置是否可以使内部窗体最大化
  1850. //setResizable方法——设置是否可以改变内部窗体的大小
  1851. //用于设置是否可以通过某个操作改变内部窗体的大小
  1852. //setSelected方法——选定或取消选定内部窗体
  1853. //用于选定或取消选定内部窗体
  1854. //setTitle方法——设置内部窗体标题栏上显示的文本
  1855. //用于设置内部窗体标题栏上显示的文本
  1856. //toBack方法——将内部窗体发送到后台
  1857. //用于将内部窗体发送到后台
  1858. //toFront方法——将内部窗体置于前端
  1859. //用于将内部窗体发送到后台
  1860. <!--常用面板 ->
  1861. //javaxswingJPanel面板
  1862. //JPanel构造方法
  1863. JFrame frame = new JFrame("使用JPanel面板");
  1864. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1865. JPanel panel = new JPanel();
  1866. panel.setBackground(Color.ORANGE);
  1867. frame.getContentPane().add(panel);
  1868. frame.setBounds(400,320,300,200);
  1869. frame.setVisible(true);
  1870. //add方法——在面板容器中添加控件
  1871. JPanel panel = new JPanel();
  1872. panel.add(new JLabel("编号:"));
  1873. JTextField tf_id = new JTextField(15);
  1874. panel.add(tf_id);
  1875. panel.add(new JLabel("姓名:"));
  1876. JTextField tf_name = new JTextField(15);
  1877. panel.add(tf_name);
  1878. JButton btn_exit = new JButton("退 出");
  1879. panel.add(btn_exit);
  1880. JFrame frame = new JFrame("使用JPanel面板");
  1881. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1882. frame.getContentPane().add(panel);
  1883. frame.setBounds(400,320,300,200);
  1884. frame.setVisible(true);
  1885. //paintComponent方法——在面板容器中绘制内容
  1886. public static void main(String[] args) throws Throwable {
  1887. JFrame frame = new JFrame("在面板上绘制");
  1888. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1889. frame.setBounds(400,320,400,240);
  1890. JPanel panel = new PanelBack();
  1891. frame.getContentPane().add(panel);
  1892. frame.setVisible(true);
  1893. }
  1894. static class PanelBack extends JPanel{
  1895. protected void paintComponet(Graphics g){
  1896. g.setFont(new Font("宋体",Font.BOLD,26));
  1897. g.drawString("重写方法", 20, 70);
  1898. g.drawString("面板绘制文本内容", 20, 120);
  1899. }
  1900. }
  1901. //setLayout方法——设置面板容器所使用的布局管理器
  1902. //设置布局管理器
  1903. //javaxswingJScrollPane滚动面板
  1904. //JScrollPane构造方法
  1905. JFrame frame = new JFrame();
  1906. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1907. frame.setBounds(400,320,400,240);
  1908. JScrollPane scrollPane = new JScrollPane();
  1909. JTextArea ta = new JTextArea();
  1910. scrollPane.setViewportView(ta);
  1911. frame.getContentPane().add(scrollPane);
  1912. frame.setVisible(true);
  1913. //setViewportView方法——设置滚动面板上显示的视图控件
  1914. JFrame frame = new JFrame();
  1915. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1916. frame.setBounds(400,320,400,240);
  1917. JScrollPane scrollPane = new JScrollPane();
  1918. JTextArea ta = new JTextArea();
  1919. scrollPane.setViewportView(ta);
  1920. frame.getContentPane().add(scrollPane);
  1921. frame.setVisible(true);
  1922. //javaxswingJSplitPane分割面板
  1923. //JSplitPane构造方法
  1924. JFrame frame = new JFrame();
  1925. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1926. frame.setBounds(400,320,400,240);
  1927. JSplitPane splitPane = new JSplitPane();
  1928. frame.getContentPane().add(splitPane);
  1929. frame.setVisible(true);
  1930. //setBottomComponent方法——将组件设置到分隔条的下方或右侧
  1931. JFrame frame = new JFrame();
  1932. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1933. frame.setBounds(400,320,400,240);
  1934. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  1935. splitPane.setDividerLocation(60);
  1936. splitPane.setBottomComponent(new JButton("下方的按钮"));
  1937. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1938. frame.setVisible(true);
  1939. //setContinuousLayout方法——设置分割面板的重绘方式
  1940. JFrame frame = new JFrame();
  1941. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1942. frame.setBounds(400,320,400,240);
  1943. JSplitPane splitPane = new JSplitPane();
  1944. splitPane.setContinuousLayout(true);
  1945. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1946. frame.setVisible(true);
  1947. //setDividerLocation方法——设置分隔条的绝对位置
  1948. JFrame frame = new JFrame();
  1949. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1950. frame.setBounds(400,320,400,240);
  1951. JSplitPane splitPane = new JSplitPane();
  1952. splitPane.setDividerLocation(120);
  1953. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1954. frame.setVisible(true);
  1955. //setDividerSize方法——设置分隔条的大小
  1956. JFrame frame = new JFrame();
  1957. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1958. frame.setBounds(400,320,400,240);
  1959. JSplitPane splitPane = new JSplitPane();
  1960. splitPane.setDividerLocation(120);
  1961. splitPane.setDividerSize(25);
  1962. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1963. frame.setVisible(true);
  1964. //setLeftComponent方法——将组件添加到分隔条的左侧或上方
  1965. JFrame frame = new JFrame();
  1966. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1967. frame.setBounds(400,320,400,240);
  1968. JSplitPane splitPane = new JSplitPane();
  1969. splitPane.setDividerLocation(120);
  1970. splitPane.setLeftComponent(new JButton("左侧的按钮"));
  1971. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1972. frame.setVisible(true);
  1973. //setOneTouchExpandable方法——设置分割面板是否提供UI小部件
  1974. JFrame frame = new JFrame();
  1975. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1976. frame.setBounds(400,320,400,240);
  1977. JSplitPane splitPane = new JSplitPane();
  1978. splitPane.setDividerLocation(90);
  1979. splitPane.setLeftComponent(new JButton("左侧的按钮"));
  1980. splitPane.setBottomComponent(new JButton("右侧的按钮"));
  1981. splitPane.setOneTouchExpandable(true);
  1982. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1983. frame.setVisible(true);
  1984. //setOrientation方法——设置分割面板的分割方向
  1985. JFrame frame = new JFrame();
  1986. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1987. frame.setBounds(400,320,400,240);
  1988. JSplitPane splitPane = new JSplitPane();
  1989. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  1990. splitPane.setDividerLocation(90);
  1991. splitPane.setLeftComponent(new JButton("上方的按钮"));
  1992. splitPane.setBottomComponent(new JButton("下方的按钮"));
  1993. splitPane.setOneTouchExpandable(true);
  1994. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  1995. frame.setVisible(true);
  1996. //setRightComponent方法——将组件添加到分隔条的右侧或下方
  1997. JFrame frame = new JFrame();
  1998. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  1999. frame.setBounds(400,320,400,240);
  2000. JSplitPane splitPane = new JSplitPane();
  2001. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  2002. splitPane.setDividerLocation(100);
  2003. splitPane.setRightComponent(new JButton("右侧按钮"));
  2004. splitPane.setOneTouchExpandable(true);
  2005. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  2006. frame.setVisible(true);
  2007. //setTopComponent方法——将组件添加到分隔条的上方或左侧
  2008. JFrame frame = new JFrame();
  2009. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2010. frame.setBounds(400,320,400,240);
  2011. JSplitPane splitPane = new JSplitPane();
  2012. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  2013. splitPane.setDividerLocation(100);
  2014. splitPane.setTopComponent(new JButton("上方的按钮"));
  2015. frame.getContentPane().add(splitPane,BorderLayout.CENTER);
  2016. frame.setVisible(true);
  2017. //javaxswingJTabbedPane选项卡面板
  2018. //JTabbedPane构造方法
  2019. JFrame frame = new JFrame();
  2020. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2021. frame.setBounds(400,320,400,240);
  2022. JTabbedPane tabbedPane = new JTabbedPane();
  2023. JPanel panel = new JPanel();
  2024. panel.setLayout(null);
  2025. tabbedPane.addTab("标签一", null,panel,null);
  2026. JButton button = new JButton();
  2027. button.setBounds(77,51,155,28);
  2028. button.setText("标签一中的按钮");
  2029. panel.add(button);
  2030. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2031. frame.setVisible(true);
  2032. //addChangeListener方法——将ChangeListener添加到选项卡面板中
  2033. JFrame frame = new JFrame();
  2034. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2035. frame.setBounds(400,320,400,240);
  2036. final JTabbedPane tabbedPane = new JTabbedPane();
  2037. tabbedPane.addChangeListener(new ChangeListener() {
  2038. @Override
  2039. public void stateChanged(ChangeEvent e) {
  2040. if(true){
  2041. int index = tabbedPane.getSelectedIndex();
  2042. String title = tabbedPane.getTitleAt(index);
  2043. JOptionPane.showMessageDialog(null, title);
  2044. }
  2045. }
  2046. });
  2047. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2048. frame.setVisible(true);
  2049. //addTab方法——为选项卡面板添加选项卡
  2050. JFrame frame = new JFrame();
  2051. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2052. frame.setBounds(400,320,400,240);
  2053. final JTabbedPane tabbedPane = new JTabbedPane();
  2054. JPanel panel = new JPanel();
  2055. JPanel panel1 = new JPanel();
  2056. tabbedPane.addTab("第一", panel);
  2057. tabbedPane.addTab("第二", panel1);
  2058. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2059. frame.setVisible(true);
  2060. //getSelectedIndex方法——返回选择的选项卡标签索引
  2061. JFrame frame = new JFrame();
  2062. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2063. frame.setBounds(400,320,400,240);
  2064. final JTabbedPane tabbedPane = new JTabbedPane();
  2065. JButton btn_showIndex = new JButton();
  2066. btn_showIndex.addActionListener(new ActionListener() {
  2067. @Override
  2068. public void actionPerformed(ActionEvent e) {
  2069. int index = tabbedPane.getSelectedIndex();
  2070. JOptionPane.showMessageDialog(null, "索引值"+index);
  2071. }
  2072. });
  2073. btn_showIndex.setText("显示索引值");
  2074. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2075. frame.setVisible(true);
  2076. //getTabCount方法——获得选项卡面板拥有选项卡的数量
  2077. JFrame frame = new JFrame();
  2078. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2079. frame.setBounds(400,320,400,240);
  2080. final JTabbedPane tabbedPane = new JTabbedPane();
  2081. JButton btn_showNum = new JButton();
  2082. btn_showNum.addActionListener(new ActionListener() {
  2083. @Override
  2084. public void actionPerformed(ActionEvent e) {
  2085. int count = tabbedPane.getTabCount();
  2086. JOptionPane.showMessageDialog(null, "选项卡面板共有"+count);
  2087. }
  2088. });
  2089. btn_showNum.setText("选项卡数量");
  2090. tabbedPane.add(btn_showNum);
  2091. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2092. frame.setVisible(true);
  2093. //getTitleAt方法——获得选项卡标签的标题文本
  2094. JFrame frame = new JFrame();
  2095. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2096. frame.setBounds(400,320,400,240);
  2097. final JTabbedPane tabbedPane = new JTabbedPane();
  2098. JButton btn_showTitle = new JButton();
  2099. btn_showTitle.addActionListener(new ActionListener() {
  2100. @Override
  2101. public void actionPerformed(ActionEvent e) {
  2102. int count = tabbedPane.getTabCount();
  2103. JOptionPane.showMessageDialog(null, "共有"+count+"个");
  2104. }
  2105. });
  2106. btn_showTitle.setText("选项卡数量");
  2107. tabbedPane.add(btn_showTitle);
  2108. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2109. frame.setVisible(true);
  2110. //insertTab方法——在指定索引位置处插入选项卡标签
  2111. JFrame frame = new JFrame();
  2112. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2113. frame.setBounds(400,320,400,240);
  2114. final JTabbedPane tabbedPane = new JTabbedPane();
  2115. JButton btn_insertTitle = new JButton();
  2116. btn_insertTitle.addActionListener(new ActionListener() {
  2117. @Override
  2118. public void actionPerformed(ActionEvent e) {
  2119. tabbedPane.insertTab("aaa", null, new JButton("测试"), "测试", 1);
  2120. }
  2121. });
  2122. btn_insertTitle.setText("插入选项卡标签");
  2123. tabbedPane.add(btn_insertTitle);
  2124. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2125. frame.setVisible(true);
  2126. //setDisabledIconAt方法——设置选项卡标签禁用时显示的图标
  2127. JFrame frame = new JFrame();
  2128. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2129. frame.setBounds(400,320,400,240);
  2130. final JTabbedPane tabbedPane = new JTabbedPane();
  2131. JPanel panel = new JPanel();
  2132. tabbedPane.addTab("A", panel);
  2133. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2134. frame.setVisible(true);
  2135. //setEnabledAt方法——设置指定选项卡标签是否可用
  2136. JFrame frame = new JFrame();
  2137. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2138. frame.setBounds(400,320,400,240);
  2139. final JTabbedPane tabbedPane = new JTabbedPane();
  2140. JButton btn_enabled = new JButton();
  2141. btn_enabled.addActionListener(new ActionListener() {
  2142. @Override
  2143. public void actionPerformed(ActionEvent e) {
  2144. tabbedPane.setEnabledAt(0, true);
  2145. }
  2146. });
  2147. btn_enabled.setText("启用A");
  2148. tabbedPane.add(btn_enabled);
  2149. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2150. frame.setVisible(true);
  2151. //setSelectedIndex方法——使指定的选项卡标签被选中
  2152. JFrame frame = new JFrame();
  2153. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2154. frame.setBounds(400,320,400,240);
  2155. final JTabbedPane tabbedPane = new JTabbedPane();
  2156. JPanel panel = new JPanel();
  2157. tabbedPane.addTab("选项卡A", panel);
  2158. JPanel panel_1 = new JPanel();
  2159. tabbedPane.addTab("选项卡B", panel_1);
  2160. tabbedPane.setSelectedIndex(1);
  2161. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2162. frame.setVisible(true);
  2163. //setTabLayoutPolicy方法——设置选项卡标签的布局方式
  2164. JFrame frame = new JFrame();
  2165. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2166. frame.setBounds(400,320,400,240);
  2167. final JTabbedPane tabbedPane = new JTabbedPane();
  2168. JButton btn_wrap = new JButton();
  2169. btn_wrap.addActionListener(new ActionListener() {
  2170. @Override
  2171. public void actionPerformed(ActionEvent e) {
  2172. tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
  2173. }
  2174. });
  2175. btn_wrap.setText("限制布局");
  2176. tabbedPane.add(btn_wrap);
  2177. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2178. frame.setVisible(true);
  2179. //setTabPlacement方法——设置选项卡标签的显示位置
  2180. JFrame frame = new JFrame();
  2181. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2182. frame.setBounds(400,320,400,240);
  2183. final JTabbedPane tabbedPane = new JTabbedPane();
  2184. JButton btn_top = new JButton();
  2185. btn_top.addActionListener(new ActionListener() {
  2186. @Override
  2187. public void actionPerformed(ActionEvent e) {
  2188. tabbedPane.setTabPlacement(JTabbedPane.TOP);
  2189. }
  2190. });
  2191. btn_top.setText("上方");
  2192. tabbedPane.add(btn_top);
  2193. frame.getContentPane().add(tabbedPane,BorderLayout.CENTER);
  2194. frame.setVisible(true);
  2195. <!--基本布局管理器 ->
  2196. //javaawtFlowLayout流布局
  2197. //FlowLayout构造方法
  2198. JFrame frame = new JFrame();
  2199. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2200. frame.setBounds(400,320,400,240);
  2201. frame.getContentPane().setLayout(new FlowLayout());
  2202. frame.getContentPane().add(new JButton("按钮A"));
  2203. frame.getContentPane().add(new JButton("按钮B"));
  2204. frame.getContentPane().add(new JButton("按钮C"));
  2205. frame.getContentPane().add(new JButton("按钮D"));
  2206. frame.setVisible(true);
  2207. //setAlignment方法——设置流布局管理器使用的对齐方式
  2208. JFrame frame = new JFrame();
  2209. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2210. frame.setBounds(400,320,400,240);
  2211. FlowLayout flowLayout = new FlowLayout();
  2212. flowLayout.setAlignment(FlowLayout.LEFT);
  2213. frame.getContentPane().setLayout(flowLayout);
  2214. frame.getContentPane().add(new JButton("按钮A"));
  2215. frame.getContentPane().add(new JButton("按钮B"));
  2216. frame.getContentPane().add(new JButton("按钮C"));
  2217. frame.getContentPane().add(new JButton("按钮D"));
  2218. frame.setVisible(true);
  2219. //setHgap方法——设置水平间隙
  2220. JFrame frame = new JFrame();
  2221. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2222. frame.setBounds(400,320,400,240);
  2223. FlowLayout flowLayout = new FlowLayout();
  2224. flowLayout.setHgap(25);
  2225. frame.getContentPane().setLayout(flowLayout);
  2226. frame.getContentPane().add(new JButton("按钮A"));
  2227. frame.getContentPane().add(new JButton("按钮B"));
  2228. frame.getContentPane().add(new JButton("按钮C"));
  2229. frame.getContentPane().add(new JButton("按钮D"));
  2230. frame.setVisible(true);
  2231. //setVgap方法——设置垂直间隙
  2232. JFrame frame = new JFrame();
  2233. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2234. frame.setBounds(400,320,400,240);
  2235. FlowLayout flowLayout = new FlowLayout();
  2236. flowLayout.setVgap(20);
  2237. frame.getContentPane().setLayout(flowLayout);
  2238. frame.getContentPane().add(new JButton("按钮A"));
  2239. frame.getContentPane().add(new JButton("按钮B"));
  2240. frame.getContentPane().add(new JButton("按钮C"));
  2241. frame.getContentPane().add(new JButton("按钮D"));
  2242. frame.setVisible(true);
  2243. //javaawtBorderLayout边界布局
  2244. //BorderLayout构造方法
  2245. JFrame frame = new JFrame();
  2246. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2247. frame.setBounds(400,320,400,240);
  2248. JPanel panel = new JPanel();
  2249. panel.setLayout(new BorderLayout());
  2250. frame.getContentPane().add(panel,BorderLayout.CENTER);
  2251. JButton button = new JButton("中");
  2252. panel.add(button,BorderLayout.CENTER);
  2253. JButton button1 = new JButton("北");
  2254. panel.add(button1,BorderLayout.NORTH);
  2255. JButton button2 = new JButton("西");
  2256. panel.add(button2,BorderLayout.WEST);
  2257. JButton button3 = new JButton("南");
  2258. panel.add(button3,BorderLayout.SOUTH);
  2259. JButton button4 = new JButton("东");
  2260. panel.add(button4,BorderLayout.EAST);
  2261. frame.setVisible(true);
  2262. //setHgap方法——设置水平间距
  2263. JFrame frame = new JFrame();
  2264. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2265. frame.setBounds(400,320,400,240);
  2266. JPanel panel = new JPanel();
  2267. BorderLayout borderLayout = new BorderLayout();
  2268. panel.setLayout(borderLayout);
  2269. borderLayout.setHgap(20);
  2270. frame.getContentPane().add(panel,BorderLayout.CENTER);
  2271. JButton button = new JButton("中");
  2272. panel.add(button,BorderLayout.CENTER);
  2273. JButton button1 = new JButton("北");
  2274. panel.add(button1,BorderLayout.NORTH);
  2275. JButton button2 = new JButton("西");
  2276. panel.add(button2,BorderLayout.WEST);
  2277. JButton button3 = new JButton("南");
  2278. panel.add(button3,BorderLayout.SOUTH);
  2279. JButton button4 = new JButton("东");
  2280. panel.add(button4,BorderLayout.EAST);
  2281. frame.setVisible(true);
  2282. //setVgap方法——设置垂直间距
  2283. JFrame frame = new JFrame();
  2284. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2285. frame.setBounds(400,320,400,240);
  2286. JPanel panel = new JPanel();
  2287. BorderLayout borderLayout = new BorderLayout();
  2288. panel.setLayout(borderLayout);
  2289. borderLayout.setVgap(15);
  2290. frame.getContentPane().add(panel,BorderLayout.CENTER);
  2291. JButton button = new JButton("中");
  2292. panel.add(button,BorderLayout.CENTER);
  2293. JButton button1 = new JButton("北");
  2294. panel.add(button1,BorderLayout.NORTH);
  2295. JButton button2 = new JButton("西");
  2296. panel.add(button2,BorderLayout.WEST);
  2297. JButton button3 = new JButton("南");
  2298. panel.add(button3,BorderLayout.SOUTH);
  2299. JButton button4 = new JButton("东");
  2300. panel.add(button4,BorderLayout.EAST);
  2301. frame.setVisible(true);
  2302. //javaawtGridLayout网格布局
  2303. //GridLayout构造方法
  2304. JFrame frame = new JFrame();
  2305. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2306. frame.setBounds(400,320,400,240);
  2307. GridLayout gridLayout = new GridLayout(2, 3);
  2308. frame.getContentPane().setLayout(gridLayout);
  2309. JButton button = new JButton("1");
  2310. frame.getContentPane().add(button);
  2311. JButton button1 = new JButton("2");
  2312. frame.getContentPane().add(button1);
  2313. JButton button2 = new JButton("3");
  2314. frame.getContentPane().add(button2);
  2315. JButton button3 = new JButton("4");
  2316. frame.getContentPane().add(button3);
  2317. JButton button4 = new JButton("5");
  2318. frame.getContentPane().add(button4);
  2319. frame.setVisible(true);
  2320. //setColumns方法——设置网格布局管理器的网格列数
  2321. JFrame frame = new JFrame();
  2322. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2323. frame.setBounds(400,320,400,240);
  2324. GridLayout gridLayout = new GridLayout(0, 4);
  2325. gridLayout.setColumns(2);
  2326. frame.getContentPane().setLayout(gridLayout);
  2327. JButton button = new JButton("1");
  2328. frame.getContentPane().add(button);
  2329. JButton button1 = new JButton("2");
  2330. frame.getContentPane().add(button1);
  2331. JButton button2 = new JButton("3");
  2332. frame.getContentPane().add(button2);
  2333. JButton button3 = new JButton("4");
  2334. frame.getContentPane().add(button3);
  2335. JButton button4 = new JButton("5");
  2336. frame.getContentPane().add(button4);
  2337. frame.setVisible(true);
  2338. //setHgap方法——设置水平间距
  2339. JFrame frame = new JFrame();
  2340. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2341. frame.setBounds(400,320,400,240);
  2342. GridLayout gridLayout = new GridLayout(0, 4);
  2343. gridLayout.setHgap(10);
  2344. frame.getContentPane().setLayout(gridLayout);
  2345. JButton button = new JButton("1");
  2346. frame.getContentPane().add(button);
  2347. JButton button1 = new JButton("2");
  2348. frame.getContentPane().add(button1);
  2349. JButton button2 = new JButton("3");
  2350. frame.getContentPane().add(button2);
  2351. JButton button3 = new JButton("4");
  2352. frame.getContentPane().add(button3);
  2353. JButton button4 = new JButton("5");
  2354. frame.getContentPane().add(button4);
  2355. frame.setVisible(true);
  2356. //setRows方法——设置网格布局管理器的网格行数
  2357. JFrame frame = new JFrame();
  2358. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2359. frame.setBounds(400,320,400,240);
  2360. GridLayout gridLayout = new GridLayout(0, 4);
  2361. gridLayout.setRows(2);
  2362. frame.getContentPane().setLayout(gridLayout);
  2363. JButton button = new JButton("1");
  2364. frame.getContentPane().add(button);
  2365. JButton button1 = new JButton("2");
  2366. frame.getContentPane().add(button1);
  2367. JButton button2 = new JButton("3");
  2368. frame.getContentPane().add(button2);
  2369. JButton button3 = new JButton("4");
  2370. frame.getContentPane().add(button3);
  2371. JButton button4 = new JButton("5");
  2372. frame.getContentPane().add(button4);
  2373. frame.setVisible(true);
  2374. //setVgap方法——设置垂直间距
  2375. JFrame frame = new JFrame();
  2376. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2377. frame.setBounds(400,320,400,240);
  2378. GridLayout gridLayout = new GridLayout(0, 4);
  2379. gridLayout.setVgap(20);
  2380. frame.getContentPane().setLayout(gridLayout);
  2381. JButton button = new JButton("1");
  2382. frame.getContentPane().add(button);
  2383. JButton button1 = new JButton("2");
  2384. frame.getContentPane().add(button1);
  2385. JButton button2 = new JButton("3");
  2386. frame.getContentPane().add(button2);
  2387. JButton button3 = new JButton("4");
  2388. frame.getContentPane().add(button3);
  2389. JButton button4 = new JButton("5");
  2390. frame.getContentPane().add(button4);
  2391. frame.setVisible(true);
  2392. //javaawtCardLayout卡片布局
  2393. //CardLayout构造方法
  2394. JFrame frame = new JFrame();
  2395. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2396. frame.setBounds(400,320,400,240);
  2397. CardLayout cardLayout = new CardLayout(10,15);
  2398. frame.getContentPane().setLayout(cardLayout);
  2399. frame.getContentPane().add("one",new JButton("第一个"));
  2400. frame.getContentPane().add("two",new JButton("第二个"));
  2401. frame.setVisible(true);
  2402. //first方法——显示容器中的第一张卡片
  2403. JFrame frame = new JFrame();
  2404. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2405. frame.setBounds(400,320,400,240);
  2406. final CardLayout cardLayout = new CardLayout(10,15);
  2407. frame.getContentPane().setLayout(cardLayout);
  2408. final JPanel cardPanel = new JPanel();
  2409. JButton button = new JButton();
  2410. button.addActionListener(new ActionListener() {
  2411. @Override
  2412. public void actionPerformed(ActionEvent e) {
  2413. cardLayout.first(cardPanel);
  2414. }
  2415. });
  2416. cardPanel.add(button);
  2417. button.setText("第一张");
  2418. frame.setVisible(true);
  2419. //last方法——显示容器中的最后一张卡片
  2420. JFrame frame = new JFrame();
  2421. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2422. frame.setBounds(400,320,400,240);
  2423. final CardLayout cardLayout = new CardLayout(10,15);
  2424. frame.getContentPane().setLayout(cardLayout);
  2425. final JPanel cardPanel = new JPanel();
  2426. JButton button = new JButton();
  2427. button.addActionListener(new ActionListener() {
  2428. @Override
  2429. public void actionPerformed(ActionEvent e) {
  2430. cardLayout.last(cardPanel);
  2431. }
  2432. });
  2433. cardPanel.add(button);
  2434. button.setText("第一张");
  2435. frame.setVisible(true);
  2436. //next方法——显示容器中当前卡片的下一张卡片
  2437. JFrame frame = new JFrame();
  2438. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2439. frame.setBounds(400,320,400,240);
  2440. final CardLayout cardLayout = new CardLayout(10,15);
  2441. frame.getContentPane().setLayout(cardLayout);
  2442. final JPanel cardPanel = new JPanel();
  2443. JButton button = new JButton();
  2444. button.addActionListener(new ActionListener() {
  2445. @Override
  2446. public void actionPerformed(ActionEvent e) {
  2447. cardLayout.next(cardPanel);
  2448. }
  2449. });
  2450. cardPanel.add(button);
  2451. button.setText("第一张");
  2452. frame.setVisible(true);
  2453. //previous方法——显示容器中当前卡片的前一张卡片
  2454. JFrame frame = new JFrame();
  2455. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2456. frame.setBounds(400,320,400,240);
  2457. final CardLayout cardLayout = new CardLayout(10,15);
  2458. frame.getContentPane().setLayout(cardLayout);
  2459. final JPanel cardPanel = new JPanel();
  2460. JButton button = new JButton();
  2461. button.addActionListener(new ActionListener() {
  2462. @Override
  2463. public void actionPerformed(ActionEvent e) {
  2464. cardLayout.previous(cardPanel);
  2465. }
  2466. });
  2467. cardPanel.add(button);
  2468. button.setText("第一张");
  2469. frame.setVisible(true);
  2470. //show方法——显示容器中用户指定的卡片
  2471. JFrame frame = new JFrame();
  2472. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2473. frame.setBounds(400,320,400,240);
  2474. final CardLayout cardLayout = new CardLayout(10,15);
  2475. frame.getContentPane().setLayout(cardLayout);
  2476. final JPanel cardPanel = new JPanel();
  2477. JButton button = new JButton();
  2478. button.addActionListener(new ActionListener() {
  2479. @Override
  2480. public void actionPerformed(ActionEvent e) {
  2481. cardLayout.show(cardPanel,"test");
  2482. }
  2483. });
  2484. cardPanel.add(button);
  2485. button.setText("第一张");
  2486. frame.setVisible(true);
  2487. <!--文本输入控件 ->
  2488. //javaxswingJLabel类
  2489. //JLabel构造方法
  2490. JFrame frame = new JFrame();
  2491. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2492. frame.setBounds(400,320,400,240);
  2493. JLabel label = new JLabel("aa");
  2494. frame.getContentPane().add(label);
  2495. frame.setVisible(true);
  2496. //setFont方法——设置标签上文本的字体
  2497. JFrame frame = new JFrame();
  2498. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2499. frame.setBounds(400,320,400,240);
  2500. JLabel label = new JLabel("aa");
  2501. label.setFont(new Font("微软雅黑",Font.BOLD,20));
  2502. frame.getContentPane().add(label);
  2503. frame.setVisible(true);
  2504. //setHorizontalTextPosition方法——设置标签中文本相对于图标的水平位置
  2505. ImageIcon icon = new ImageIcon("src/1.jpg");
  2506. JFrame frame = new JFrame();
  2507. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2508. frame.setBounds(400,320,400,240);
  2509. JLabel label = new JLabel("aa");
  2510. label.setIcon(icon);
  2511. label.setHorizontalTextPosition(SwingConstants.RIGHT);
  2512. frame.getContentPane().add(label);
  2513. frame.setVisible(true);
  2514. //setText方法——设置标签上显示的文本信息
  2515. JFrame frame = new JFrame();
  2516. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2517. frame.setBounds(400,320,400,240);
  2518. JLabel label = new JLabel();
  2519. label.setText("Left");
  2520. frame.getContentPane().add(label);
  2521. frame.setVisible(true);
  2522. //setVerticalTextPosition方法——设置文本相对于图标的垂直位置
  2523. ImageIcon icon = new ImageIcon("src/1.jpg");
  2524. JFrame frame = new JFrame();
  2525. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2526. frame.setBounds(400,320,400,240);
  2527. JLabel label = new JLabel();
  2528. label.setIcon(icon);
  2529. label.setText("aa");
  2530. label.setVerticalTextPosition(SwingConstants.BOTTOM);
  2531. label.setHorizontalTextPosition(SwingConstants.CENTER);
  2532. frame.getContentPane().add(label);
  2533. frame.setVisible(true);
  2534. //javaxswingJTextField类
  2535. //JTextField构造方法
  2536. JFrame frame = new JFrame();
  2537. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2538. frame.setBounds(400,320,400,240);
  2539. JTextField textField = new JTextField("科技");
  2540. frame.getContentPane().add(textField);
  2541. frame.setVisible(true);
  2542. //addActionListener方法——响应用户在文本域中按Enter键时的操作
  2543. JFrame frame = new JFrame();
  2544. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2545. frame.setBounds(400,320,400,240);
  2546. final JTextField textField = new JTextField();
  2547. textField.addActionListener(new ActionListener() {
  2548. @Override
  2549. public void actionPerformed(ActionEvent e) {
  2550. textField.getText();
  2551. }
  2552. });
  2553. frame.getContentPane().add(textField);
  2554. frame.setVisible(true);
  2555. //addFocusListener方法——响应文本域焦点事件
  2556. JFrame frame = new JFrame();
  2557. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2558. frame.setBounds(400,320,400,240);
  2559. final JTextField textField = new JTextField();
  2560. textField.addFocusListener(new FocusAdapter() {
  2561. @Override
  2562. public void focusGained(FocusEvent e) {
  2563. textField.setText("文本焦点");
  2564. }
  2565. });
  2566. frame.getContentPane().add(textField);
  2567. frame.setVisible(true);
  2568. //getText方法——获得文本域中输入的文本
  2569. JFrame frame = new JFrame();
  2570. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2571. frame.setBounds(400,320,400,240);
  2572. final JTextField textField = new JTextField();
  2573. textField.getText();
  2574. frame.getContentPane().add(textField);
  2575. frame.setVisible(true);
  2576. //setColumns方法——设置文本框的列数
  2577. JFrame frame = new JFrame();
  2578. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2579. frame.setBounds(400,320,400,240);
  2580. final JTextField textField = new JTextField();
  2581. textField.setColumns(10);
  2582. frame.getContentPane().add(textField);
  2583. frame.setVisible(true);
  2584. //setFont方法——设置文本域中的字体
  2585. JFrame frame = new JFrame();
  2586. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2587. frame.setBounds(400,320,400,240);
  2588. final JTextField textField = new JTextField();
  2589. textField.setFont(new Font("微软雅黑",Font.BOLD,15));
  2590. frame.getContentPane().add(textField);
  2591. frame.setVisible(true);
  2592. //javaxswingJPasswordField类
  2593. //JPasswordField构造方法
  2594. JFrame frame = new JFrame();
  2595. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2596. frame.setBounds(400,320,400,240);
  2597. JPasswordField passwordField = new JPasswordField(10);
  2598. frame.getContentPane().add(passwordField);
  2599. frame.setVisible(true);
  2600. //addActionListener方法——响应在密码域中按Enter键时的操作
  2601. JFrame frame = new JFrame();
  2602. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2603. frame.setBounds(400,320,400,240);
  2604. final JPasswordField passwordField = new JPasswordField(10);
  2605. passwordField.addActionListener(new ActionListener() {
  2606. @Override
  2607. public void actionPerformed(ActionEvent e) {
  2608. passwordField.setText(new String(passwordField.getPassword()));
  2609. }
  2610. });
  2611. frame.getContentPane().add(passwordField);
  2612. frame.setVisible(true);
  2613. //getPassword方法——获得输入的密码
  2614. JFrame frame = new JFrame();
  2615. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2616. frame.setBounds(400,320,400,240);
  2617. final JPasswordField passwordField = new JPasswordField(10);
  2618. passwordField.addActionListener(new ActionListener() {
  2619. @Override
  2620. public void actionPerformed(ActionEvent e) {
  2621. passwordField.getPassword();
  2622. }
  2623. });
  2624. frame.getContentPane().add(passwordField);
  2625. frame.setVisible(true);
  2626. //setEchoChar方法——修改密码域的回显字符
  2627. JFrame frame = new JFrame();
  2628. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2629. frame.setBounds(400,320,400,240);
  2630. final JPasswordField passwordField = new JPasswordField(10);
  2631. passwordField.setEchoChar('&');
  2632. frame.getContentPane().add(passwordField);
  2633. frame.setVisible(true);
  2634. //javaxswingJTextArea类
  2635. //JTextArea构造方法
  2636. JFrame frame = new JFrame();
  2637. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2638. frame.setBounds(400,320,400,240);
  2639. JTextArea textArea = new JTextArea(5,10);
  2640. frame.getContentPane().add(textArea);
  2641. frame.setVisible(true);
  2642. //addCaretListener方法——监听光标位置在文本区中的变化事件
  2643. JFrame frame = new JFrame();
  2644. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2645. frame.setBounds(400,320,400,240);
  2646. final Label label = new Label();
  2647. final JTextArea textArea = new JTextArea(5,10);
  2648. textArea.addCaretListener(new CaretListener() {
  2649. @Override
  2650. public void caretUpdate(CaretEvent e) {
  2651. label.setText(""+e.getDot());
  2652. }
  2653. });
  2654. frame.getContentPane().add(textArea);
  2655. frame.setVisible(true);
  2656. //append方法——在文本区中文本末尾增加字符串
  2657. JFrame frame = new JFrame();
  2658. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2659. frame.setBounds(400,320,400,240);
  2660. final TextArea textArea = new TextArea();
  2661. JButton button = new JButton("单击按钮");
  2662. button.addActionListener(new ActionListener() {
  2663. @Override
  2664. public void actionPerformed(ActionEvent e) {
  2665. textArea.append("单击一次按钮");
  2666. }
  2667. });
  2668. frame.getContentPane().add(textArea);
  2669. frame.setVisible(true);
  2670. //getText方法——获得文本区中输入的内容
  2671. JFrame frame = new JFrame();
  2672. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2673. frame.setBounds(400,320,400,240);
  2674. final TextArea textArea = new TextArea();
  2675. JButton button = new JButton();
  2676. button.addActionListener(new ActionListener() {
  2677. @Override
  2678. public void actionPerformed(ActionEvent e) {
  2679. textArea.getText();
  2680. }
  2681. });
  2682. frame.getContentPane().add(textArea);
  2683. frame.setVisible(true);
  2684. //setFont方法——设置文本区字体
  2685. JFrame frame = new JFrame();
  2686. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2687. frame.setBounds(400,320,400,240);
  2688. final TextArea textArea = new TextArea();
  2689. textArea.setFont(new Font("微软雅黑",Font.BOLD,15));
  2690. frame.getContentPane().add(textArea);
  2691. frame.setVisible(true);
  2692. //setLineWrap方法——设置是否自动换行
  2693. JFrame frame = new JFrame();
  2694. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2695. frame.setBounds(400,320,400,240);
  2696. JTextArea textArea = new JTextArea();
  2697. textArea.setLineWrap(true);
  2698. frame.getContentPane().add(textArea);
  2699. frame.setVisible(true);
  2700. <!--选择控件 ->
  2701. //javaxswingJButton类
  2702. //JButton构造方法
  2703. JFrame frame = new JFrame();
  2704. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2705. frame.setBounds(400,320,400,240);
  2706. JButton button = new JButton("这是一个按钮");
  2707. frame.getContentPane().add(button);
  2708. frame.setVisible(true);
  2709. //addActionListener方法——向按钮控件增加动作事件监听器
  2710. JFrame frame = new JFrame();
  2711. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2712. frame.setLayout(new FlowLayout());
  2713. frame.setBounds(400,320,400,240);
  2714. JButton button = new JButton("这是一个按钮");
  2715. final Label label = new Label();
  2716. button.addActionListener(new ActionListener() {
  2717. @Override
  2718. public void actionPerformed(ActionEvent e) {
  2719. label.setText("用户单击");
  2720. }
  2721. });
  2722. frame.getContentPane().add(label);
  2723. frame.getContentPane().add(button);
  2724. frame.setVisible(true);
  2725. //setActionCommand方法——设置按钮的动作命令
  2726. JFrame frame = new JFrame();
  2727. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2728. frame.setLayout(new FlowLayout());
  2729. frame.setBounds(400,320,400,240);
  2730. JButton button = new JButton("单击按钮");
  2731. final JLabel label = new JLabel();
  2732. button.addActionListener(new ActionListener() {
  2733. @Override
  2734. public void actionPerformed(ActionEvent e) {
  2735. label.setText("单击");
  2736. }
  2737. });
  2738. button.setActionCommand("alter");
  2739. frame.getContentPane().add(label);
  2740. frame.getContentPane().add(button);
  2741. frame.setVisible(true);
  2742. //setEnabled方法——设置按钮是否可用
  2743. JFrame frame = new JFrame();
  2744. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2745. frame.setLayout(new FlowLayout());
  2746. frame.setBounds(400,320,400,240);
  2747. JButton button = new JButton("按钮");
  2748. button.setEnabled(false);
  2749. frame.getContentPane().add(button);
  2750. frame.setVisible(true);
  2751. //setHorizontalTextPosition方法——设置文本相对于图标的水平位置
  2752. ImageIcon icon = new ImageIcon("src/1.jpg");
  2753. JFrame frame = new JFrame();
  2754. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2755. frame.setLayout(new FlowLayout());
  2756. frame.setBounds(400,320,400,240);
  2757. JButton button = new JButton("按钮",icon);
  2758. button.setHorizontalTextPosition(SwingConstants.LEFT);
  2759. frame.getContentPane().add(button);
  2760. frame.setVisible(true);
  2761. //setMnemonic方法——为按钮添加助记符
  2762. JFrame frame = new JFrame();
  2763. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2764. frame.setLayout(new FlowLayout());
  2765. frame.setBounds(400,320,400,240);
  2766. JButton button = new JButton("按钮");
  2767. button.setMnemonic(KeyEvent.VK_0);
  2768. frame.getContentPane().add(button);
  2769. frame.setVisible(true);
  2770. //setVerticalTextPosition方法——设置文本相对于图标的垂直位置
  2771. ImageIcon icon = new ImageIcon("src/1.jpg");
  2772. JFrame frame = new JFrame();
  2773. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2774. frame.setLayout(new FlowLayout());
  2775. frame.setBounds(400,320,400,240);
  2776. JButton button = new JButton("按钮",icon);
  2777. button.setVerticalTextPosition(SwingConstants.TOP);
  2778. frame.getContentPane().add(button);
  2779. frame.setVisible(true);
  2780. //isDefaultButton方法——判断是否为默认按钮
  2781. JFrame frame = new JFrame();
  2782. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2783. frame.setLayout(new FlowLayout());
  2784. frame.setBounds(400,320,400,240);
  2785. JButton button = new JButton("按钮");
  2786. frame.getRootPane().setDefaultButton(button);
  2787. System.out.println(button.isDefaultButton());
  2788. frame.getContentPane().add(button);
  2789. frame.setVisible(true);
  2790. //javaxswingJCheckBox类
  2791. //JCheckBox构造方法
  2792. JFrame frame = new JFrame();
  2793. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2794. frame.setLayout(new FlowLayout());
  2795. frame.setBounds(400,320,400,240);
  2796. JCheckBox checkBox = new JCheckBox("复选框");
  2797. frame.getContentPane().add(checkBox);
  2798. frame.setVisible(true);
  2799. //addActionListener方法——向复选框控件增加动作事件监听器
  2800. JFrame frame = new JFrame();
  2801. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2802. frame.setLayout(new FlowLayout());
  2803. frame.setBounds(400,320,400,240);
  2804. final Label label = new Label();
  2805. JCheckBox checkBox = new JCheckBox("复选框");
  2806. checkBox.addActionListener(new ActionListener() {
  2807. @Override
  2808. public void actionPerformed(ActionEvent e) {
  2809. label.setText("这是一个复选框");
  2810. }
  2811. });
  2812. frame.getContentPane().add(label);
  2813. frame.getContentPane().add(checkBox);
  2814. frame.setVisible(true);
  2815. //addItemListener方法——为复选框增加选择状态变化事件监听
  2816. JFrame frame = new JFrame();
  2817. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2818. frame.setLayout(new FlowLayout());
  2819. frame.setBounds(400,320,400,240);
  2820. final Label label = new Label();
  2821. JCheckBox checkBox = new JCheckBox("复选框");
  2822. checkBox.addItemListener(new ItemListener() {
  2823. @Override
  2824. public void itemStateChanged(ItemEvent e) {
  2825. label.setText("复选框已选择");
  2826. }
  2827. });
  2828. frame.getContentPane().add(label);
  2829. frame.getContentPane().add(checkBox);
  2830. frame.setVisible(true);
  2831. //isSelected方法——判断复选框是否被用户选择
  2832. JFrame frame = new JFrame();
  2833. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2834. frame.setLayout(new FlowLayout());
  2835. frame.setBounds(400,320,400,240);
  2836. final Label label = new Label();
  2837. JCheckBox checkBox = new JCheckBox("复选框");
  2838. label.setText(""+checkBox.isSelected());
  2839. frame.getContentPane().add(label);
  2840. frame.getContentPane().add(checkBox);
  2841. frame.setVisible(true);
  2842. //setSelected方法——将复选框设置成选择状态
  2843. JFrame frame = new JFrame();
  2844. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2845. frame.setLayout(new FlowLayout());
  2846. frame.setBounds(400,320,400,240);
  2847. JCheckBox checkBox = new JCheckBox("复选框");
  2848. checkBox.setSelected(true);
  2849. frame.getContentPane().add(checkBox);
  2850. frame.setVisible(true);
  2851. //javaxswingJRadioButton类
  2852. //JRadioButton构造方法
  2853. JFrame frame = new JFrame();
  2854. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2855. frame.setLayout(new FlowLayout());
  2856. frame.setBounds(400,320,400,240);
  2857. JRadioButton radioButton = new JRadioButton("单选");
  2858. frame.getContentPane().add(radioButton);
  2859. frame.setVisible(true);
  2860. //addActionListener方法——向单选按钮控件增加动作事件监听器
  2861. JFrame frame = new JFrame();
  2862. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2863. frame.setLayout(new FlowLayout());
  2864. frame.setBounds(400,320,400,240);
  2865. final JLabel label = new JLabel();
  2866. JRadioButton radioButton = new JRadioButton("单选");
  2867. radioButton.addActionListener(new ActionListener() {
  2868. @Override
  2869. public void actionPerformed(ActionEvent e) {
  2870. label.setText("选择");
  2871. }
  2872. });
  2873. frame.getContentPane().add(label);
  2874. frame.getContentPane().add(radioButton);
  2875. frame.setVisible(true);
  2876. //isSelected方法——判断单选按钮是否被用户选择
  2877. JFrame frame = new JFrame();
  2878. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2879. frame.setLayout(new FlowLayout());
  2880. frame.setBounds(400,320,400,240);
  2881. final JLabel label = new JLabel();
  2882. JRadioButton radioButton = new JRadioButton("单选");
  2883. label.setText(radioButton.isSelected()+"");
  2884. frame.getContentPane().add(label);
  2885. frame.getContentPane().add(radioButton);
  2886. frame.setVisible(true);
  2887. //setSelected方法——将单选按钮设置成选择状态
  2888. JFrame frame = new JFrame();
  2889. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2890. frame.setLayout(new FlowLayout());
  2891. frame.setBounds(400,320,400,240);
  2892. final JLabel label = new JLabel();
  2893. JRadioButton radioButton = new JRadioButton("单选");
  2894. radioButton.setSelected(true);
  2895. frame.getContentPane().add(label);
  2896. frame.getContentPane().add(radioButton);
  2897. frame.setVisible(true);
  2898. //javaxswingButtonGroup类
  2899. //add方法——将按钮添加到按钮组中
  2900. JFrame frame = new JFrame();
  2901. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2902. frame.setLayout(new FlowLayout());
  2903. frame.setBounds(400,320,400,240);
  2904. ButtonGroup group = new ButtonGroup();
  2905. JRadioButton radioButton1 = new JRadioButton("按钮1");
  2906. JRadioButton radioButton2 = new JRadioButton("按钮2");
  2907. group.add(radioButton1);
  2908. group.add(radioButton2);
  2909. frame.getContentPane().add(radioButton1);
  2910. frame.getContentPane().add(radioButton2);
  2911. frame.setVisible(true);
  2912. //remove方法——删除已经添加到按钮组中的按钮
  2913. JFrame frame = new JFrame();
  2914. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2915. frame.setLayout(new FlowLayout());
  2916. frame.setBounds(400,320,400,240);
  2917. ButtonGroup group = new ButtonGroup();
  2918. JRadioButton radioButton1 = new JRadioButton("按钮1");
  2919. JRadioButton radioButton2 = new JRadioButton("按钮2");
  2920. JRadioButton radioButton3 = new JRadioButton("按钮3");
  2921. group.add(radioButton1);
  2922. group.add(radioButton2);
  2923. group.add(radioButton3);
  2924. group.remove(radioButton3);
  2925. frame.getContentPane().add(radioButton1);
  2926. frame.getContentPane().add(radioButton2);
  2927. frame.getContentPane().add(radioButton3);
  2928. frame.setVisible(true);
  2929. //javaxswingBorderFactory类
  2930. //createBevelBorder方法——创建指定类型的斜面边框
  2931. JFrame frame = new JFrame();
  2932. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2933. frame.setLayout(new FlowLayout());
  2934. frame.setBounds(400,320,400,240);
  2935. JPanel panel = new JPanel();
  2936. panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
  2937. frame.getContentPane().add(panel);
  2938. frame.setVisible(true);
  2939. //createCompoundBorder方法——指定复合边框
  2940. JFrame frame = new JFrame();
  2941. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2942. frame.setLayout(new FlowLayout());
  2943. frame.setBounds(400,320,400,240);
  2944. JPanel panel = new JPanel();
  2945. Border outside = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
  2946. Border inside = BorderFactory.createBevelBorder(BevelBorder.RAISED);
  2947. panel.setBorder(new CompoundBorder(outside,inside));
  2948. frame.getContentPane().add(panel);
  2949. frame.setVisible(true);
  2950. //createEmptyBorder方法——创建一个空白的边框
  2951. JFrame frame = new JFrame();
  2952. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2953. frame.setLayout(new FlowLayout());
  2954. frame.setBounds(400,320,400,240);
  2955. JPanel panel = new JPanel();
  2956. panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
  2957. frame.getContentPane().add(panel,BorderLayout.CENTER);
  2958. frame.setVisible(true);
  2959. //createEtchedBorder方法——创建浮雕效果的边框
  2960. JFrame frame = new JFrame();
  2961. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2962. frame.setLayout(new FlowLayout());
  2963. frame.setBounds(400,320,400,240);
  2964. JPanel panel = new JPanel();
  2965. panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  2966. frame.getContentPane().add(panel);
  2967. frame.setVisible(true);
  2968. //createLineBorder方法——创建直线边框
  2969. JFrame frame = new JFrame();
  2970. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2971. frame.setLayout(new FlowLayout());
  2972. frame.setBounds(400,320,400,240);
  2973. JPanel panel = new JPanel();
  2974. panel.setBorder(BorderFactory.createLineBorder(Color.RED,5));
  2975. frame.getContentPane().add(panel);
  2976. frame.setVisible(true);
  2977. //createMatteBorder方法——创建纯色或者指定图片的边框
  2978. JFrame frame = new JFrame();
  2979. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2980. frame.setLayout(new FlowLayout());
  2981. frame.setBounds(400,320,400,240);
  2982. JPanel panel = new JPanel();
  2983. panel.setBorder(BorderFactory.createMatteBorder(1, 2, 3, 4, Color.RED));
  2984. frame.getContentPane().add(panel);
  2985. frame.setVisible(true);
  2986. //createTitledBorder方法——创建带有标题的边框
  2987. JFrame frame = new JFrame();
  2988. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2989. frame.setLayout(new FlowLayout());
  2990. frame.setBounds(400,320,400,240);
  2991. JPanel panel = new JPanel();
  2992. panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"标题边框"));
  2993. frame.getContentPane().add(panel);
  2994. frame.setVisible(true);
  2995. //javaxswingJComboBox类
  2996. //JComboBox构造方法
  2997. JFrame frame = new JFrame();
  2998. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  2999. frame.setLayout(new FlowLayout());
  3000. frame.setBounds(400,320,400,240);
  3001. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3002. model.addElement("JAVA");
  3003. model.addElement("PHP");
  3004. JComboBox comboBox = new JComboBox(model);
  3005. frame.getContentPane().add(comboBox);
  3006. frame.setVisible(true);
  3007. //addActionListener方法——监听组合框的选择事件
  3008. JFrame frame = new JFrame();
  3009. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3010. frame.setLayout(new FlowLayout());
  3011. frame.setBounds(400,320,400,240);
  3012. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3013. model.addElement("JAVA");
  3014. model.addElement("PHP");
  3015. JComboBox comboBox = new JComboBox(model);
  3016. comboBox.addActionListener(new ActionListener() {
  3017. @Override
  3018. public void actionPerformed(ActionEvent e) {
  3019. JOptionPane.showMessageDialog(null, "选择");
  3020. return;
  3021. }
  3022. });
  3023. frame.getContentPane().add(comboBox);
  3024. frame.setVisible(true);
  3025. //addItem方法——给组合框增加元素
  3026. JFrame frame = new JFrame();
  3027. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3028. frame.setLayout(new FlowLayout());
  3029. frame.setBounds(400,320,400,240);
  3030. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3031. model.addElement("JAVA");
  3032. model.addElement("PHP");
  3033. JComboBox comboBox = new JComboBox(model);
  3034. comboBox.addItem("JAVA1");
  3035. frame.getContentPane().add(comboBox);
  3036. frame.setVisible(true);
  3037. //getSelectedItem方法——获得用户选择的元素
  3038. JFrame frame = new JFrame();
  3039. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3040. frame.setLayout(new FlowLayout());
  3041. frame.setBounds(400,320,400,240);
  3042. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3043. model.addElement("JAVA");
  3044. model.addElement("PHP");
  3045. final JComboBox comboBox = new JComboBox(model);
  3046. comboBox.addActionListener(new ActionListener() {
  3047. @Override
  3048. public void actionPerformed(ActionEvent e) {
  3049. JOptionPane.showMessageDialog(null, ""+comboBox.getSelectedItem());
  3050. return;
  3051. }
  3052. });
  3053. frame.getContentPane().add(comboBox);
  3054. frame.setVisible(true);
  3055. //setEditable方法——设置组合框中的文本框是否处于编辑状态
  3056. JFrame frame = new JFrame();
  3057. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3058. frame.setLayout(new FlowLayout());
  3059. frame.setBounds(400,320,400,240);
  3060. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3061. model.addElement("JAVA");
  3062. model.addElement("PHP");
  3063. final JComboBox comboBox = new JComboBox(model);
  3064. comboBox.setEditable(true);
  3065. frame.getContentPane().add(comboBox);
  3066. frame.setVisible(true);
  3067. //setMaximumRowCount方法——设置组合框中可以显示的列表项个数
  3068. JFrame frame = new JFrame();
  3069. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3070. frame.setLayout(new FlowLayout());
  3071. frame.setBounds(400,320,400,240);
  3072. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3073. model.addElement("JAVA");
  3074. model.addElement("PHP");
  3075. final JComboBox comboBox = new JComboBox(model);
  3076. comboBox.setMaximumRowCount(1);
  3077. frame.getContentPane().add(comboBox);
  3078. frame.setVisible(true);
  3079. //setSelectedIndex方法——设置当前组合框中选择的元素
  3080. JFrame frame = new JFrame();
  3081. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3082. frame.setLayout(new FlowLayout());
  3083. frame.setBounds(400,320,400,240);
  3084. DefaultComboBoxModel model = new DefaultComboBoxModel();
  3085. model.addElement("JAVA");
  3086. model.addElement("PHP");
  3087. final JComboBox comboBox = new JComboBox(model);
  3088. comboBox.setSelectedIndex(1);
  3089. frame.getContentPane().add(comboBox);
  3090. frame.setVisible(true);
  3091. //javaxswingJSlider类
  3092. //JSlider构造方法
  3093. JFrame frame = new JFrame();
  3094. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3095. frame.setLayout(new FlowLayout());
  3096. frame.setBounds(400,320,400,240);
  3097. JSlider slider = new JSlider(JSlider.HORIZONTAL);
  3098. frame.getContentPane().add(slider);
  3099. frame.setVisible(true);
  3100. //addChangeListener方法——监听滑块滑动事件
  3101. JFrame frame = new JFrame();
  3102. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3103. frame.setLayout(new FlowLayout());
  3104. frame.setBounds(400,320,400,240);
  3105. final Label label = new Label("未滑动");
  3106. JSlider slider = new JSlider(JSlider.HORIZONTAL);
  3107. slider.addChangeListener(new ChangeListener() {
  3108. @Override
  3109. public void stateChanged(ChangeEvent e) {
  3110. label.setText("已滑动");
  3111. }
  3112. });
  3113. frame.getContentPane().add(label);
  3114. frame.getContentPane().add(slider);
  3115. frame.setVisible(true);
  3116. //getValue方法——获得滑块的当前值
  3117. JFrame frame = new JFrame();
  3118. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3119. frame.setLayout(new FlowLayout());
  3120. frame.setBounds(400,320,400,240);
  3121. final Label label = new Label("未滑动");
  3122. final JSlider slider = new JSlider(JSlider.HORIZONTAL);
  3123. slider.addChangeListener(new ChangeListener() {
  3124. @Override
  3125. public void stateChanged(ChangeEvent e) {
  3126. label.setText(""+slider.getValue());
  3127. }
  3128. });
  3129. frame.getContentPane().add(label);
  3130. frame.getContentPane().add(slider);
  3131. frame.setVisible(true);
  3132. //setFont方法——设置滑块上显示的标签的字体
  3133. JFrame frame = new JFrame();
  3134. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3135. frame.setLayout(new FlowLayout());
  3136. frame.setBounds(400,320,400,240);
  3137. final Label label = new Label("未滑动");
  3138. final JSlider slider = new JSlider(0,100,50);
  3139. slider.addChangeListener(new ChangeListener() {
  3140. @Override
  3141. public void stateChanged(ChangeEvent e) {
  3142. label.setText(""+slider.getValue());
  3143. }
  3144. });
  3145. slider.setMajorTickSpacing(20);
  3146. slider.setMinorTickSpacing(1);
  3147. slider.setPaintTicks(true);
  3148. slider.setPaintLabels(true);
  3149. slider.setFont(new Font("微软雅黑",Font.BOLD,15));
  3150. frame.getContentPane().add(label);
  3151. frame.getContentPane().add(slider);
  3152. frame.setVisible(true);
  3153. //setLabelTable方法——给滑块设置标签
  3154. JFrame frame = new JFrame();
  3155. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3156. frame.setLayout(new FlowLayout());
  3157. frame.setBounds(400,320,400,240);
  3158. final JSlider slider = new JSlider(0,100,50);
  3159. Hashtable<Integer, Component> labels = new Hashtable<Integer, Component>();
  3160. labels.put(0, new JLabel("0"));
  3161. labels.put(0, new JLabel("50"));
  3162. labels.put(0, new JLabel("100"));
  3163. slider.setLabelTable(labels);
  3164. slider.setPaintLabels(true);
  3165. frame.getContentPane().add(slider);
  3166. frame.setVisible(true);
  3167. //setMajorTickSpacing方法——设置主刻度间的距离
  3168. JFrame frame = new JFrame();
  3169. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3170. frame.setLayout(new FlowLayout());
  3171. frame.setBounds(400,320,400,240);
  3172. final JSlider slider = new JSlider(0,100,50);
  3173. slider.setMajorTickSpacing(10);
  3174. slider.setPaintTicks(true);
  3175. frame.getContentPane().add(slider);
  3176. frame.setVisible(true);
  3177. //setMaximum方法——设置滑块的最大值
  3178. JFrame frame = new JFrame();
  3179. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3180. frame.setLayout(new FlowLayout());
  3181. frame.setBounds(400,320,400,240);
  3182. final JSlider slider = new JSlider(0,100,50);
  3183. slider.setMaximum(100);
  3184. frame.getContentPane().add(slider);
  3185. frame.setVisible(true);
  3186. //setMinimum方法——设置滑块的最小值
  3187. JFrame frame = new JFrame();
  3188. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3189. frame.setLayout(new FlowLayout());
  3190. frame.setBounds(400,320,400,240);
  3191. final JSlider slider = new JSlider(0,100,50);
  3192. slider.setMinimum(50);
  3193. frame.getContentPane().add(slider);
  3194. frame.setVisible(true);
  3195. //setMinorTickSpacing方法——设置副刻度间的距离
  3196. JFrame frame = new JFrame();
  3197. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3198. frame.setLayout(new FlowLayout());
  3199. frame.setBounds(400,320,400,240);
  3200. JSlider slider = new JSlider(0,50,25);
  3201. slider.setMinorTickSpacing(1);
  3202. slider.setPaintTicks(true);
  3203. frame.getContentPane().add(slider);
  3204. frame.setVisible(true);
  3205. //javaxswingJSpinner类
  3206. //JSpinner构造方法
  3207. JFrame frame = new JFrame();
  3208. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3209. frame.setLayout(new FlowLayout());
  3210. frame.setBounds(400,320,400,240);
  3211. String[] months = {"周一","周二","周三","周四","周五"};
  3212. SpinnerListModel model = new SpinnerListModel(months);
  3213. JSpinner spinner = new JSpinner(model);
  3214. frame.getContentPane().add(spinner);
  3215. frame.setVisible(true);
  3216. //addChangeListener方法——监听微调按钮变化事件
  3217. JFrame frame = new JFrame();
  3218. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3219. frame.setLayout(new FlowLayout());
  3220. frame.setBounds(400,320,400,240);
  3221. String[] months = {"周一","周二","周三","周四","周五"};
  3222. SpinnerListModel model = new SpinnerListModel(months);
  3223. JSpinner spinner = new JSpinner(model);
  3224. spinner.addChangeListener(new ChangeListener() {
  3225. @Override
  3226. public void stateChanged(ChangeEvent e) {
  3227. JOptionPane.showMessageDialog(null, "变化");
  3228. return;
  3229. }
  3230. });
  3231. frame.getContentPane().add(spinner);
  3232. frame.setVisible(true);
  3233. //getValue方法——获得微调按钮当前的值
  3234. JFrame frame = new JFrame();
  3235. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3236. frame.setLayout(new FlowLayout());
  3237. frame.setBounds(400,320,400,240);
  3238. String[] months = {"周一","周二","周三","周四","周五"};
  3239. SpinnerListModel model = new SpinnerListModel(months);
  3240. final JSpinner spinner = new JSpinner(model);
  3241. spinner.addChangeListener(new ChangeListener() {
  3242. @Override
  3243. public void stateChanged(ChangeEvent e) {
  3244. JOptionPane.showMessageDialog(null, spinner.getValue()+"");
  3245. return;
  3246. }
  3247. });
  3248. frame.getContentPane().add(spinner);
  3249. frame.setVisible(true);
  3250. //setEditor方法——设置编辑微调按钮值的控件
  3251. JFrame frame = new JFrame();
  3252. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3253. frame.setLayout(new FlowLayout());
  3254. frame.setBounds(400,320,400,240);
  3255. final JSpinner spinner = new JSpinner(new SpinnerDateModel());
  3256. spinner.setEditor(new JSpinner.DateEditor(spinner,"yyyy-MM-dd"));
  3257. frame.getContentPane().add(spinner);
  3258. frame.setVisible(true);
  3259. <!--菜单和工具栏控件 ->
  3260. //javaxswingJMenuBar类
  3261. //JMenuBar构造方法
  3262. JFrame frame = new JFrame();
  3263. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3264. frame.setLayout(new FlowLayout());
  3265. frame.setBounds(400,320,400,240);
  3266. JMenuBar menuBar = new JMenuBar();
  3267. frame.setJMenuBar(menuBar);
  3268. JMenu menu1 = new JMenu("菜单");
  3269. menuBar.add(menu1);
  3270. frame.setVisible(true);
  3271. //add方法——向菜单条中增加菜单
  3272. JFrame frame = new JFrame();
  3273. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3274. frame.setLayout(new FlowLayout());
  3275. frame.setBounds(400,320,400,240);
  3276. JMenuBar menuBar = new JMenuBar();
  3277. frame.setJMenuBar(menuBar);
  3278. JMenu menu1 = new JMenu("菜单");
  3279. menuBar.add(menu1);
  3280. frame.setVisible(true);
  3281. //setLayout方法——设置控件的布局
  3282. JFrame frame = new JFrame();
  3283. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3284. frame.setLayout(new FlowLayout());
  3285. frame.setBounds(400,320,400,240);
  3286. JMenuBar menuBar = new JMenuBar();
  3287. menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
  3288. frame.setJMenuBar(menuBar);
  3289. JMenu menu1 = new JMenu("菜单");
  3290. menuBar.add(menu1);
  3291. frame.setVisible(true);
  3292. //javaxswingJMenu类
  3293. //JMenu构造方法
  3294. JFrame frame = new JFrame();
  3295. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3296. frame.setLayout(new FlowLayout());
  3297. frame.setBounds(400,320,400,240);
  3298. JMenuBar menuBar = new JMenuBar();
  3299. frame.setJMenuBar(menuBar);
  3300. JMenu menu1 = new JMenu("菜单");
  3301. menuBar.add(menu1);
  3302. frame.setVisible(true);
  3303. //add方法——给菜单增加菜单项
  3304. JFrame frame = new JFrame();
  3305. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3306. frame.setLayout(new FlowLayout());
  3307. frame.setBounds(400,320,400,240);
  3308. JMenuBar menuBar = new JMenuBar();
  3309. frame.setJMenuBar(menuBar);
  3310. JMenu menu1 = new JMenu("菜单");
  3311. menuBar.add(menu1);
  3312. frame.setVisible(true);
  3313. //addSeparator方法——给菜单项增加分隔符
  3314. JFrame frame = new JFrame();
  3315. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3316. frame.setLayout(new FlowLayout());
  3317. frame.setBounds(400,320,400,240);
  3318. JMenuBar menuBar = new JMenuBar();
  3319. frame.setJMenuBar(menuBar);
  3320. JMenu menu1 = new JMenu("菜单");
  3321. JMenu menu2 = new JMenu("菜单2");
  3322. menuBar.add(menu1);
  3323. menuBar.add(menu2);
  3324. menu2.addSeparator();
  3325. frame.setVisible(true);
  3326. //setFont方法——设置菜单字体
  3327. JFrame frame = new JFrame();
  3328. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3329. frame.setLayout(new FlowLayout());
  3330. frame.setBounds(400,320,400,240);
  3331. JMenuBar menuBar = new JMenuBar();
  3332. frame.setJMenuBar(menuBar);
  3333. JMenu menu1 = new JMenu("菜单");
  3334. JMenu menu2 = new JMenu("菜单2");
  3335. menu2.setFont(new Font("微软雅黑",Font.BOLD,15));
  3336. menuBar.add(menu1);
  3337. menuBar.add(menu2);
  3338. frame.setVisible(true);
  3339. //setMnemonic方法——给菜单设置助记符
  3340. JFrame frame = new JFrame();
  3341. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3342. frame.setLayout(new FlowLayout());
  3343. frame.setBounds(400,320,400,240);
  3344. JMenuBar menuBar = new JMenuBar();
  3345. frame.setJMenuBar(menuBar);
  3346. JMenu menu1 = new JMenu("菜单");
  3347. JMenu menu2 = new JMenu("菜单2");
  3348. menu1.setMnemonic(KeyEvent.VK_A);
  3349. menuBar.add(menu1);
  3350. menuBar.add(menu2);
  3351. frame.setVisible(true);
  3352. //setPopupMenuVisible方法——设置弹出菜单是否可见
  3353. //设置弹出菜单是否可见
  3354. //javaxswingJMenuItem类
  3355. //JMenuItem构造方法
  3356. JFrame frame = new JFrame();
  3357. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3358. frame.setLayout(new FlowLayout());
  3359. frame.setBounds(400,320,400,240);
  3360. JMenuBar menuBar = new JMenuBar();
  3361. frame.setJMenuBar(menuBar);
  3362. JMenu menu1 = new JMenu("菜单");
  3363. JMenuItem menuItem1 = new JMenuItem("打开");
  3364. menu1.add(menuItem1);
  3365. menuBar.add(menu1);
  3366. frame.setVisible(true);
  3367. //addActionListener方法——处理菜单项动作事件
  3368. JFrame frame = new JFrame();
  3369. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3370. frame.setLayout(new FlowLayout());
  3371. frame.setBounds(400,320,400,240);
  3372. JMenuBar menuBar = new JMenuBar();
  3373. frame.setJMenuBar(menuBar);
  3374. JMenu menu1 = new JMenu("菜单");
  3375. JMenuItem menuItem1 = new JMenuItem("打开");
  3376. menuItem1.addActionListener(new ActionListener() {
  3377. @Override
  3378. public void actionPerformed(ActionEvent e) {
  3379. System.out.println("+");
  3380. return;
  3381. }
  3382. });
  3383. menu1.add(menuItem1);
  3384. menuBar.add(menu1);
  3385. frame.setVisible(true);
  3386. //setAccelerator方法——给菜单项设置快捷键
  3387. JFrame frame = new JFrame();
  3388. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3389. frame.setLayout(new FlowLayout());
  3390. frame.setBounds(400,320,400,240);
  3391. JMenuBar menuBar = new JMenuBar();
  3392. frame.setJMenuBar(menuBar);
  3393. JMenu menu1 = new JMenu("菜单");
  3394. JMenuItem menuItem1 = new JMenuItem("打开");
  3395. menuItem1.setAccelerator(KeyStroke.getKeyStroke("ctrl 2"));
  3396. menu1.add(menuItem1);
  3397. menuBar.add(menu1);
  3398. frame.setVisible(true);
  3399. //setEnabled方法——启用或禁用菜单项
  3400. JFrame frame = new JFrame();
  3401. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3402. frame.setLayout(new FlowLayout());
  3403. frame.setBounds(400,320,400,240);
  3404. JMenuBar menuBar = new JMenuBar();
  3405. frame.setJMenuBar(menuBar);
  3406. JMenu menu1 = new JMenu("菜单");
  3407. JMenuItem menuItem1 = new JMenuItem("打开");
  3408. menuItem1.setEnabled(false);
  3409. menu1.add(menuItem1);
  3410. menuBar.add(menu1);
  3411. frame.setVisible(true);
  3412. //setFont方法——为菜单项设置字体
  3413. JFrame frame = new JFrame();
  3414. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3415. frame.setLayout(new FlowLayout());
  3416. frame.setBounds(400,320,400,240);
  3417. JMenuBar menuBar = new JMenuBar();
  3418. frame.setJMenuBar(menuBar);
  3419. JMenu menu1 = new JMenu("菜单");
  3420. JMenuItem menuItem1 = new JMenuItem("打开");
  3421. menuItem1.setFont(new Font("微软雅黑",Font.BOLD,15));
  3422. menu1.add(menuItem1);
  3423. menuBar.add(menu1);
  3424. frame.setVisible(true);
  3425. //javaxswingJPopupMenu类
  3426. //JPopupMenu构造方法
  3427. JFrame frame = new JFrame();
  3428. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3429. frame.setLayout(new FlowLayout());
  3430. JPanel jPanel = new JPanel();
  3431. frame.setBounds(400,320,400,240);
  3432. JPopupMenu popup = new JPopupMenu();
  3433. jPanel.setComponentPopupMenu(popup);
  3434. frame.getContentPane().add(jPanel);
  3435. frame.setVisible(true);
  3436. //add方法——给弹出菜单增加菜单项
  3437. JFrame frame = new JFrame();
  3438. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3439. frame.setLayout(new FlowLayout());
  3440. frame.setBounds(400,320,400,240);
  3441. JPanel panel = new JPanel();
  3442. JPopupMenu popup = new JPopupMenu();
  3443. popup.add(new JMenuItem("男生",new ImageIcon("src/1.jpg")));
  3444. panel.setComponentPopupMenu(popup);
  3445. frame.getContentPane().add(panel);
  3446. frame.setVisible(true);
  3447. //addSeparator方法——给弹出菜单的菜单项之间增加分隔符
  3448. JFrame frame = new JFrame();
  3449. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3450. frame.setLayout(new FlowLayout());
  3451. frame.setBounds(400,320,400,240);
  3452. JPanel panel = new JPanel();
  3453. JPopupMenu popup = new JPopupMenu();
  3454. popup.add("男");
  3455. popup.add("女");
  3456. popup.addSeparator();
  3457. panel.setComponentPopupMenu(popup);
  3458. frame.getContentPane().add(panel);
  3459. frame.setVisible(true);
  3460. //setPopupSize方法——设置弹出菜单的大小
  3461. JFrame frame = new JFrame();
  3462. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3463. frame.setLayout(new FlowLayout());
  3464. frame.setBounds(400,320,400,240);
  3465. JPanel panel = new JPanel();
  3466. JPopupMenu popup = new JPopupMenu();
  3467. popup.add("男");
  3468. popup.add("女");
  3469. popup.addSeparator();
  3470. popup.setPopupSize(100,100);
  3471. frame.getContentPane().add(panel);
  3472. frame.setVisible(true);
  3473. //javaxswingJCheckBoxMenuItem类
  3474. //JCheckBoxMenuItem构造方法
  3475. JFrame frame = new JFrame();
  3476. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3477. frame.setLayout(new FlowLayout());
  3478. frame.setBounds(400,320,400,240);
  3479. JMenu menu = new JMenu();
  3480. JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("菜单");
  3481. frame.getContentPane().add(menu);
  3482. frame.setVisible(true);
  3483. //addActionListener方法——监听复选框菜单项的动作事件
  3484. JFrame frame = new JFrame();
  3485. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3486. frame.setLayout(new FlowLayout());
  3487. frame.setBounds(400,320,400,240);
  3488. JMenu menu = new JMenu();
  3489. JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("菜单");
  3490. jCheckBoxMenuItem.addActionListener(new ActionListener() {
  3491. @Override
  3492. public void actionPerformed(ActionEvent e) {
  3493. System.out.println("+");
  3494. }
  3495. });
  3496. frame.getContentPane().add(menu);
  3497. frame.setVisible(true);
  3498. //setFont方法——给复选框菜单项设置字体
  3499. JFrame frame = new JFrame();
  3500. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3501. frame.setLayout(new FlowLayout());
  3502. frame.setBounds(400,320,400,240);
  3503. JMenu menu = new JMenu();
  3504. JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("菜单");
  3505. jCheckBoxMenuItem.setFont(new Font("微软雅黑", Font.BOLD, 15));
  3506. frame.getContentPane().add(menu);
  3507. frame.setVisible(true);
  3508. //javaxswingJRadioButtonMenuItem类
  3509. //JRadioButtonMenuItem构造方法
  3510. JFrame frame = new JFrame();
  3511. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3512. frame.setLayout(new FlowLayout());
  3513. frame.setBounds(400,320,400,240);
  3514. JMenu menu = new JMenu();
  3515. JRadioButton jRadioButton = new JRadioButton("菜单");
  3516. menu.add(jRadioButton);
  3517. frame.getContentPane().add(menu);
  3518. frame.setVisible(true);
  3519. //addActionListener方法——监听单选按钮菜单项的动作事件
  3520. JFrame frame = new JFrame();
  3521. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3522. frame.setLayout(new FlowLayout());
  3523. frame.setBounds(400,320,400,240);
  3524. JMenu menu = new JMenu();
  3525. JRadioButton jRadioButton = new JRadioButton("菜单");
  3526. jRadioButton.addActionListener(new ActionListener() {
  3527. @Override
  3528. public void actionPerformed(ActionEvent e) {
  3529. System.out.println("+");
  3530. }
  3531. });
  3532. frame.getContentPane().add(menu);
  3533. frame.setVisible(true);
  3534. //setFont方法——给单选按钮菜单项设置字体
  3535. JFrame frame = new JFrame();
  3536. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3537. frame.setLayout(new FlowLayout());
  3538. frame.setBounds(400,320,400,240);
  3539. JMenu menu = new JMenu();
  3540. JRadioButton jRadioButton = new JRadioButton("菜单");
  3541. jRadioButton.setFont(new Font("微软雅黑",Font.BOLD,15));
  3542. frame.getContentPane().add(menu);
  3543. frame.setVisible(true);
  3544. //javaxswingJToolBar类
  3545. //JToolBar构造方法
  3546. JFrame frame = new JFrame();
  3547. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3548. frame.setLayout(new FlowLayout());
  3549. frame.setBounds(400,320,400,240);
  3550. JToolBar toolBar = new JToolBar();
  3551. JButton button = new JButton("按钮");
  3552. toolBar.add(button);
  3553. frame.getContentPane().add(toolBar);
  3554. frame.setVisible(true);
  3555. //add方法——为工具栏添加控件
  3556. JFrame frame = new JFrame();
  3557. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3558. frame.setLayout(new FlowLayout());
  3559. frame.setBounds(400,320,400,240);
  3560. JToolBar toolBar = new JToolBar();
  3561. JButton button = new JButton("按钮");
  3562. toolBar.add(button);
  3563. frame.getContentPane().add(toolBar);
  3564. frame.setVisible(true);
  3565. //addSeparator方法——给工具栏增加分隔符
  3566. JFrame frame = new JFrame();
  3567. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3568. frame.setLayout(new FlowLayout());
  3569. frame.setBounds(400,320,400,240);
  3570. JToolBar toolBar = new JToolBar();
  3571. JButton button = new JButton("按钮");
  3572. toolBar.add(button);
  3573. JButton button1 = new JButton("按钮");
  3574. toolBar.add(button1);
  3575. toolBar.addSeparator(new Dimension(50,100));
  3576. frame.getContentPane().add(toolBar);
  3577. frame.setVisible(true);
  3578. //setFloatable方法——固定工具栏
  3579. JFrame frame = new JFrame();
  3580. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3581. frame.setLayout(new FlowLayout());
  3582. frame.setBounds(400,320,400,240);
  3583. JToolBar toolBar = new JToolBar();
  3584. toolBar.setFloatable(false);
  3585. JButton button = new JButton("按钮");
  3586. toolBar.add(button);
  3587. JButton button1 = new JButton("按钮");
  3588. toolBar.add(button1);
  3589. frame.getContentPane().add(toolBar);
  3590. frame.setVisible(true);
  3591. <!--对话框控件 ->
  3592. //javaxswingJOptionPane类
  3593. //showConfirmDialog方法——创建一个确认对话框
  3594. JFrame frame = new JFrame();
  3595. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3596. frame.setLayout(new FlowLayout());
  3597. frame.setBounds(400,320,400,240);
  3598. JOptionPane.showConfirmDialog(frame, "确定退出");
  3599. frame.setVisible(true);
  3600. //showInputDialog方法——创建接收用户输入信息的对话框
  3601. JFrame frame = new JFrame();
  3602. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3603. frame.setLayout(new FlowLayout());
  3604. frame.setBounds(400,320,400,240);
  3605. JOptionPane.showInputDialog(frame,"输入名字");
  3606. frame.setVisible(true);
  3607. //showMessageDialog方法——创建消息对话框
  3608. JFrame frame = new JFrame();
  3609. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3610. frame.setLayout(new FlowLayout());
  3611. frame.setBounds(400,320,400,240);
  3612. JOptionPane.showMessageDialog(frame, "欢迎使用");
  3613. frame.setVisible(true);
  3614. //showOptionDialog方法——根据用户指定的选项创建对话框
  3615. JFrame frame = new JFrame();
  3616. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3617. frame.setLayout(new FlowLayout());
  3618. frame.setBounds(400,320,400,240);
  3619. JOptionPane.showOptionDialog(frame, "对话框", "对话框1", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, null, "java");
  3620. frame.setVisible(true);
  3621. //javaxswingJFileChooser类
  3622. //JFileChooser构造方法
  3623. JFrame frame = new JFrame();
  3624. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3625. frame.setLayout(new FlowLayout());
  3626. frame.setBounds(400,320,400,240);
  3627. JFileChooser fileChooser = new JFileChooser();
  3628. fileChooser.showOpenDialog(frame);
  3629. frame.setVisible(true);
  3630. //getSelectedFile方法——获得用户选择的文件
  3631. JFrame frame = new JFrame();
  3632. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3633. frame.setLayout(new FlowLayout());
  3634. frame.setBounds(400,320,400,240);
  3635. JFileChooser fileChooser = new JFileChooser();
  3636. fileChooser.showOpenDialog(frame);
  3637. File selectedFile = fileChooser.getSelectedFile();
  3638. System.out.println(selectedFile.getAbsolutePath());
  3639. frame.setVisible(true);
  3640. //getSelectedFiles方法——获得选择的一组文件
  3641. JFrame frame = new JFrame();
  3642. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3643. frame.setLayout(new FlowLayout());
  3644. frame.setBounds(400,320,400,240);
  3645. JFileChooser fileChooser = new JFileChooser();
  3646. fileChooser.showOpenDialog(frame);
  3647. File[] selectedFile = fileChooser.getSelectedFiles();
  3648. System.out.println(selectedFile.length);
  3649. frame.setVisible(true);
  3650. //setFileFilter方法——设置文件过滤器
  3651. JFrame frame = new JFrame();
  3652. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3653. frame.setLayout(new FlowLayout());
  3654. frame.setBounds(400,320,400,240);
  3655. JFileChooser fileChooser = new JFileChooser();
  3656. fileChooser.showOpenDialog(frame);
  3657. fileChooser.setFileFilter(new FileFilter() {
  3658. @Override
  3659. public String getDescription() {
  3660. return null;
  3661. }
  3662. @Override
  3663. public boolean accept(File f) {
  3664. return false;
  3665. }
  3666. });
  3667. frame.setVisible(true);
  3668. //setFileHidingEnabled方法——设置是否显示隐藏文件
  3669. JFrame frame = new JFrame();
  3670. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3671. frame.setLayout(new FlowLayout());
  3672. frame.setBounds(400,320,400,240);
  3673. JFileChooser fileChooser = new JFileChooser();
  3674. fileChooser.showOpenDialog(frame);
  3675. fileChooser.setFileHidingEnabled(true);
  3676. frame.setVisible(true);
  3677. //setFileSelectionMode方法——设置选择模式
  3678. JFrame frame = new JFrame();
  3679. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3680. frame.setLayout(new FlowLayout());
  3681. frame.setBounds(400,320,400,240);
  3682. JFileChooser fileChooser = new JFileChooser();
  3683. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  3684. fileChooser.showOpenDialog(frame);
  3685. frame.setVisible(true);
  3686. //setMultiSelectionEnabled方法——设置是否能够同时选择多个文件或文件夹
  3687. JFrame frame = new JFrame();
  3688. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3689. frame.setLayout(new FlowLayout());
  3690. frame.setBounds(400,320,400,240);
  3691. JFileChooser fileChooser = new JFileChooser();
  3692. fileChooser.setMultiSelectionEnabled(true);
  3693. fileChooser.showOpenDialog(frame);
  3694. frame.setVisible(true);
  3695. //showDialog方法——打开对话框
  3696. JFrame frame = new JFrame();
  3697. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3698. frame.setLayout(new FlowLayout());
  3699. frame.setBounds(400,320,400,240);
  3700. JFileChooser fileChooser = new JFileChooser();
  3701. fileChooser.showDialog(frame,"自定义按钮");
  3702. frame.setVisible(true);
  3703. //showOpenDialog方法——弹出打开对话框
  3704. JFrame frame = new JFrame();
  3705. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3706. frame.setLayout(new FlowLayout());
  3707. frame.setBounds(400,320,400,240);
  3708. JFileChooser fileChooser = new JFileChooser();
  3709. fileChooser.showOpenDialog(frame);
  3710. frame.setVisible(true);
  3711. //showSaveDialog方法——弹出保存对话框
  3712. JFrame frame = new JFrame();
  3713. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3714. frame.setLayout(new FlowLayout());
  3715. frame.setBounds(400,320,400,240);
  3716. JFileChooser fileChooser = new JFileChooser();
  3717. fileChooser.showSaveDialog(frame);
  3718. frame.setVisible(true);
  3719. //javaxswingJColorChooser类
  3720. //JColorChooser构造方法
  3721. JFrame frame = new JFrame();
  3722. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3723. frame.setLayout(new FlowLayout());
  3724. frame.setBounds(400,320,400,240);
  3725. JColorChooser chooser = new JColorChooser();
  3726. frame.getContentPane().add(chooser);
  3727. frame.setVisible(true);
  3728. //getColor方法——获得在颜色选择器中选择的颜色
  3729. JFrame frame = new JFrame();
  3730. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3731. frame.setLayout(new FlowLayout());
  3732. frame.setBounds(400,320,400,240);
  3733. final JColorChooser chooser = new JColorChooser();
  3734. frame.getContentPane().add(chooser);
  3735. chooser.getSelectionModel().addChangeListener(new ChangeListener() {
  3736. @Override
  3737. public void stateChanged(ChangeEvent e) {
  3738. System.out.println(chooser.getColor());
  3739. }
  3740. });
  3741. frame.setVisible(true);
  3742. //showDialog方法——创建颜色选择对话框
  3743. JFrame frame = new JFrame();
  3744. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3745. frame.setLayout(new FlowLayout());
  3746. frame.setBounds(400,320,400,240);
  3747. Color showDialog = JColorChooser.showDialog(frame, "", null);
  3748. frame.getContentPane().setBackground(showDialog);
  3749. frame.setVisible(true);
  3750. <!--列表控件 ->
  3751. //javaxswingJList类
  3752. //JList构造方法
  3753. JFrame frame = new JFrame();
  3754. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3755. frame.setLayout(new FlowLayout());
  3756. frame.setBounds(400,320,400,240);
  3757. DefaultListModel model = new DefaultListModel();
  3758. model.addElement("JAVA");
  3759. model.addElement("PHP");
  3760. model.addElement("ASP");
  3761. JList list = new JList(model);
  3762. frame.getContentPane().add(list);
  3763. frame.setVisible(true);
  3764. //addListSelectionListener方法——监听列表项选择事件
  3765. JFrame frame = new JFrame();
  3766. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3767. frame.setLayout(new FlowLayout());
  3768. frame.setBounds(400,320,400,240);
  3769. String[] items = {"Java","Perl","C++"};
  3770. JList list = new JList(items);
  3771. list.addListSelectionListener(new ListSelectionListener() {
  3772. @Override
  3773. public void valueChanged(ListSelectionEvent e) {
  3774. System.out.println("选中");
  3775. }
  3776. });
  3777. frame.getContentPane().add(list);
  3778. frame.setVisible(true);
  3779. //addMouseListener方法——监听鼠标事件
  3780. JFrame frame = new JFrame();
  3781. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3782. frame.setLayout(new FlowLayout());
  3783. frame.setBounds(400,320,400,240);
  3784. String[] items = {"Java","Perl","C++"};
  3785. JList list = new JList(items);
  3786. list.addMouseListener(new MouseAdapter() {
  3787. @Override
  3788. public void mouseClicked(MouseEvent e) {
  3789. System.out.println("选中");
  3790. }
  3791. });
  3792. frame.getContentPane().add(list);
  3793. frame.setVisible(true);
  3794. //getSelectedIndex方法——获得当前选择元素的索引值
  3795. JFrame frame = new JFrame();
  3796. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3797. frame.setLayout(new FlowLayout());
  3798. frame.setBounds(400,320,400,240);
  3799. String[] items = {"Java","Perl","C++"};
  3800. final JList list = new JList(items);
  3801. list.addMouseListener(new MouseAdapter() {
  3802. @Override
  3803. public void mouseClicked(MouseEvent e) {
  3804. System.out.println(list.getSelectedIndex());
  3805. }
  3806. });
  3807. frame.getContentPane().add(list);
  3808. frame.setVisible(true);
  3809. //getSelectedValue方法——获得当前选择的元素
  3810. JFrame frame = new JFrame();
  3811. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3812. frame.setLayout(new FlowLayout());
  3813. frame.setBounds(400,320,400,240);
  3814. String[] items = {"Java","Perl","C++"};
  3815. final JList list = new JList(items);
  3816. list.addMouseListener(new MouseAdapter() {
  3817. @Override
  3818. public void mouseClicked(MouseEvent e) {
  3819. System.out.println(list.getSelectedValue());
  3820. }
  3821. });
  3822. frame.getContentPane().add(list);
  3823. frame.setVisible(true);
  3824. //setCellRenderer方法——为列表设置渲染器
  3825. //列表设置渲染器
  3826. //setLayoutOrientation方法——设置列表项的布局方式
  3827. JFrame frame = new JFrame();
  3828. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3829. frame.setLayout(new FlowLayout());
  3830. frame.setBounds(400,320,400,240);
  3831. String[] items = {"Java","Perl","C++"};
  3832. final JList list = new JList(items);
  3833. list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
  3834. frame.getContentPane().add(list);
  3835. frame.setVisible(true);
  3836. //setListData方法——为列表设置列表项
  3837. JFrame frame = new JFrame();
  3838. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3839. frame.setLayout(new FlowLayout());
  3840. frame.setBounds(400,320,400,240);
  3841. String[] items = {"Java","Perl","C++"};
  3842. final JList list = new JList();
  3843. list.setListData(items);
  3844. frame.getContentPane().add(list);
  3845. frame.setVisible(true);
  3846. //setModel方法——为列表设置列表模型
  3847. JFrame frame = new JFrame();
  3848. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3849. frame.setLayout(new FlowLayout());
  3850. frame.setBounds(400,320,400,240);
  3851. DefaultListModel model = new DefaultListModel();
  3852. model.addElement("JAVA");
  3853. model.addElement("PHP");
  3854. model.addElement("ASP");
  3855. JList list = new JList(model);
  3856. frame.getContentPane().add(list);
  3857. frame.setVisible(true);
  3858. //setSelectedIndex方法——设置当前选中的列表项
  3859. JFrame frame = new JFrame();
  3860. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3861. frame.setLayout(new FlowLayout());
  3862. frame.setBounds(400,320,400,240);
  3863. String[] items = {"Java","Perl","C++"};
  3864. final JList list = new JList();
  3865. list.setListData(items);
  3866. list.setSelectedIndex(1);
  3867. frame.getContentPane().add(list);
  3868. frame.setVisible(true);
  3869. //setSelectionBackground方法——设置列表项选中时的背景色
  3870. JFrame frame = new JFrame();
  3871. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3872. frame.setLayout(new FlowLayout());
  3873. frame.setBounds(400,320,400,240);
  3874. String[] items = {"Java","Perl","C++"};
  3875. final JList list = new JList();
  3876. list.setListData(items);
  3877. list.setBackground(Color.BLUE);
  3878. frame.getContentPane().add(list);
  3879. frame.setVisible(true);
  3880. //setSelectionForeground方法——设置列表项选中时的前景色
  3881. JFrame frame = new JFrame();
  3882. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3883. frame.setLayout(new FlowLayout());
  3884. frame.setBounds(400,320,400,240);
  3885. String[] items = {"Java","Perl","C++"};
  3886. final JList list = new JList();
  3887. list.setListData(items);
  3888. list.setSelectionForeground(Color.BLUE);
  3889. frame.getContentPane().add(list);
  3890. frame.setVisible(true);
  3891. //setSelectionMode方法——设置列表项的选择模式
  3892. JFrame frame = new JFrame();
  3893. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3894. frame.setLayout(new FlowLayout());
  3895. frame.setBounds(400,320,400,240);
  3896. String[] items = {"Java","Perl","C++"};
  3897. final JList list = new JList();
  3898. list.setListData(items);
  3899. list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  3900. frame.getContentPane().add(list);
  3901. frame.setVisible(true);
  3902. //setVisibleRowCount方法——设置visibleRowCount属性
  3903. JFrame frame = new JFrame();
  3904. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3905. frame.setLayout(new FlowLayout());
  3906. frame.setBounds(400,320,400,240);
  3907. String[] items = {"Java","Perl","C++"};
  3908. final JList list = new JList();
  3909. list.setListData(items);
  3910. list.setVisibleRowCount(2);
  3911. frame.getContentPane().add(list);
  3912. frame.setVisible(true);
  3913. //javaxswingDefaultListModel类
  3914. //add方法——在列表模型中的指定位置增加元素
  3915. JFrame frame = new JFrame();
  3916. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3917. frame.setLayout(new FlowLayout());
  3918. frame.setBounds(400,320,400,240);
  3919. DefaultListModel model = new DefaultListModel();
  3920. model.add(0, "JAVA");
  3921. model.add(0, "PHP");
  3922. model.add(0, "ASP");
  3923. model.add(0, "JSP");
  3924. JList list = new JList();
  3925. list.setModel(model);
  3926. frame.getContentPane().add(list);
  3927. frame.setVisible(true);
  3928. //addElement方法——向列表模型的末尾增加新元素
  3929. JFrame frame = new JFrame();
  3930. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3931. frame.setLayout(new FlowLayout());
  3932. frame.setBounds(400,320,400,240);
  3933. DefaultListModel model = new DefaultListModel();
  3934. model.addElement("JAVA");
  3935. model.addElement("PHP");
  3936. model.addElement("ASP");
  3937. JList list = new JList();
  3938. list.setModel(model);
  3939. frame.getContentPane().add(list);
  3940. frame.setVisible(true);
  3941. //clear方法——清空列表模型中的全部元素
  3942. JFrame frame = new JFrame();
  3943. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3944. frame.setLayout(new FlowLayout());
  3945. frame.setBounds(400,320,400,240);
  3946. DefaultListModel model = new DefaultListModel();
  3947. model.addElement("JAVA");
  3948. model.addElement("PHP");
  3949. model.addElement("ASP");
  3950. model.clear();
  3951. JList list = new JList();
  3952. list.setModel(model);
  3953. frame.getContentPane().add(list);
  3954. frame.setVisible(true);
  3955. //contains方法——判断列表模型中是否包含指定的元素
  3956. JFrame frame = new JFrame();
  3957. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3958. frame.setLayout(new FlowLayout());
  3959. frame.setBounds(400,320,400,240);
  3960. DefaultListModel model = new DefaultListModel();
  3961. model.addElement("JAVA");
  3962. model.addElement("PHP");
  3963. model.addElement("ASP");
  3964. JList list = new JList();
  3965. list.setModel(model);
  3966. System.out.println(model.contains("JAVA"));
  3967. frame.getContentPane().add(list);
  3968. frame.setVisible(true);
  3969. //elementAt方法——获得指定索引值的元素
  3970. JFrame frame = new JFrame();
  3971. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3972. frame.setLayout(new FlowLayout());
  3973. frame.setBounds(400,320,400,240);
  3974. DefaultListModel model = new DefaultListModel();
  3975. model.addElement("JAVA");
  3976. model.addElement("PHP");
  3977. model.addElement("ASP");
  3978. JList list = new JList();
  3979. list.setModel(model);
  3980. System.out.println(model.elementAt(2));
  3981. frame.getContentPane().add(list);
  3982. frame.setVisible(true);
  3983. //elements方法——获得模型中全部元素的枚举
  3984. JFrame frame = new JFrame();
  3985. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  3986. frame.setLayout(new FlowLayout());
  3987. frame.setBounds(400,320,400,240);
  3988. DefaultListModel model = new DefaultListModel();
  3989. model.addElement("JAVA");
  3990. model.addElement("PHP");
  3991. model.addElement("ASP");
  3992. JList list = new JList();
  3993. list.setModel(model);
  3994. Enumeration<?> elements = model.elements();
  3995. while (elements.hasMoreElements()) {
  3996. System.out.println(elements.nextElement());
  3997. }
  3998. frame.getContentPane().add(list);
  3999. frame.setVisible(true);
  4000. //getSize方法——获得列表模型中元素的个数
  4001. JFrame frame = new JFrame();
  4002. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4003. frame.setLayout(new FlowLayout());
  4004. frame.setBounds(400,320,400,240);
  4005. DefaultListModel model = new DefaultListModel();
  4006. model.addElement("JAVA");
  4007. model.addElement("PHP");
  4008. model.addElement("ASP");
  4009. JList list = new JList();
  4010. list.setModel(model);
  4011. System.out.println(model.getSize());
  4012. frame.getContentPane().add(list);
  4013. frame.setVisible(true);
  4014. //remove方法——删除列表模型中指定索引的元素
  4015. JFrame frame = new JFrame();
  4016. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4017. frame.setLayout(new FlowLayout());
  4018. frame.setBounds(400,320,400,240);
  4019. DefaultListModel model = new DefaultListModel();
  4020. model.addElement("JAVA");
  4021. model.addElement("PHP");
  4022. model.addElement("ASP");
  4023. JList list = new JList();
  4024. list.setModel(model);
  4025. System.out.println(model.remove(2));
  4026. frame.getContentPane().add(list);
  4027. frame.setVisible(true);
  4028. //removeRange方法——从列表模型中删除指定区域的元素
  4029. JFrame frame = new JFrame();
  4030. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4031. frame.setLayout(new FlowLayout());
  4032. frame.setBounds(400,320,400,240);
  4033. DefaultListModel model = new DefaultListModel();
  4034. model.addElement("JAVA");
  4035. model.addElement("PHP");
  4036. model.addElement("ASP");
  4037. JList list = new JList();
  4038. list.setModel(model);
  4039. model.removeRange(0,1);
  4040. frame.getContentPane().add(list);
  4041. frame.setVisible(true);
  4042. <!--表格控件 ->
  4043. //javaxswingJTable类
  4044. //JTable构造方法
  4045. JFrame frame = new JFrame();
  4046. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4047. frame.setLayout(new FlowLayout());
  4048. frame.setBounds(400,320,400,240);
  4049. String[] columnNames = {"学号","姓名"};
  4050. String[][] rowData = {{"20101","小王"},{"20102","大王"}};
  4051. JTable table = new JTable(rowData,columnNames);
  4052. frame.getContentPane().add(table);
  4053. frame.setVisible(true);
  4054. //addColumn方法——向表格中增加一列
  4055. JFrame frame = new JFrame();
  4056. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4057. frame.setLayout(new FlowLayout());
  4058. frame.setBounds(400,320,400,240);
  4059. DefaultTableModel model = new DefaultTableModel();
  4060. model.addColumn("学号");
  4061. model.addColumn("姓名");
  4062. model.addRow(new String[]{"20101","1号"});
  4063. model.addRow(new String[]{"20102","2号"});
  4064. JTable table = new JTable(model);
  4065. TableColumn column = new TableColumn(0,100);
  4066. table.addColumn(column);
  4067. frame.getContentPane().add(table);
  4068. frame.setVisible(true);
  4069. //addRowSelectionInterval方法——增加选中的行
  4070. JFrame frame = new JFrame();
  4071. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4072. frame.setLayout(new FlowLayout());
  4073. frame.setBounds(400,320,400,240);
  4074. DefaultTableModel model = new DefaultTableModel();
  4075. model.addColumn("学号");
  4076. model.addColumn("姓名");
  4077. model.addRow(new String[]{"20101","1号"});
  4078. model.addRow(new String[]{"20102","2号"});
  4079. JTable table = new JTable(model);
  4080. table.addRowSelectionInterval(0, 1);
  4081. frame.getContentPane().add(table);
  4082. frame.setVisible(true);
  4083. //getColumn方法——获得表格中的指定列
  4084. JFrame frame = new JFrame();
  4085. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4086. frame.setLayout(new FlowLayout());
  4087. frame.setBounds(400,320,400,240);
  4088. DefaultTableModel model = new DefaultTableModel();
  4089. model.addColumn("学号");
  4090. model.addColumn("姓名");
  4091. model.addRow(new String[]{"20101","1号"});
  4092. model.addRow(new String[]{"20102","2号"});
  4093. JTable table = new JTable(model);
  4094. System.out.println(table.getColumn("学号"));
  4095. frame.getContentPane().add(table);
  4096. frame.setVisible(true);
  4097. //getColumnClass方法——获得表格中指定列的类型
  4098. //用于获得表格中指定列的类型
  4099. //getColumnCount方法——获得列模型中的列数
  4100. JFrame frame = new JFrame();
  4101. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4102. frame.setLayout(new FlowLayout());
  4103. frame.setBounds(400,320,400,240);
  4104. DefaultTableModel model = new DefaultTableModel();
  4105. model.addColumn("学号");
  4106. model.addColumn("姓名");
  4107. model.addRow(new String[]{"20101","1号"});
  4108. model.addRow(new String[]{"20102","2号"});
  4109. JTable table = new JTable(model);
  4110. System.out.println(table.getColumnCount());
  4111. frame.getContentPane().add(table);
  4112. frame.setVisible(true);
  4113. //getColumnName方法——获得指定列的名称
  4114. JFrame frame = new JFrame();
  4115. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4116. frame.setLayout(new FlowLayout());
  4117. frame.setBounds(400,320,400,240);
  4118. DefaultTableModel model = new DefaultTableModel();
  4119. model.addColumn("学号");
  4120. model.addColumn("姓名");
  4121. model.addRow(new String[]{"20101","1号"});
  4122. model.addRow(new String[]{"20102","2号"});
  4123. JTable table = new JTable(model);
  4124. System.out.println(table.getColumnName(0));
  4125. frame.getContentPane().add(table);
  4126. frame.setVisible(true);
  4127. //getModel方法——获得当前表格使用的表格模型
  4128. JFrame frame = new JFrame();
  4129. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4130. frame.setLayout(new FlowLayout());
  4131. frame.setBounds(400,320,400,240);
  4132. DefaultTableModel model = new DefaultTableModel();
  4133. model.addColumn("学号");
  4134. model.addColumn("姓名");
  4135. model.addRow(new String[]{"20101","1号"});
  4136. model.addRow(new String[]{"20102","2号"});
  4137. JTable table = new JTable(model);
  4138. System.out.println(table.getModel());
  4139. frame.getContentPane().add(table);
  4140. frame.setVisible(true);
  4141. //getRowCount方法——获得表格中包含的行数
  4142. JFrame frame = new JFrame();
  4143. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4144. frame.setLayout(new FlowLayout());
  4145. frame.setBounds(400,320,400,240);
  4146. DefaultTableModel model = new DefaultTableModel();
  4147. model.addColumn("学号");
  4148. model.addColumn("姓名");
  4149. model.addRow(new String[]{"20101","1号"});
  4150. model.addRow(new String[]{"20102","2号"});
  4151. JTable table = new JTable(model);
  4152. System.out.println(table.getRowCount());
  4153. frame.getContentPane().add(table);
  4154. frame.setVisible(true);
  4155. //getSelectedColumn方法——获得用户选中的第一列的索引
  4156. JFrame frame = new JFrame();
  4157. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4158. frame.setLayout(new FlowLayout());
  4159. frame.setBounds(400,320,400,240);
  4160. DefaultTableModel model = new DefaultTableModel();
  4161. model.addColumn("学号");
  4162. model.addColumn("姓名");
  4163. model.addRow(new String[]{"20101","1号"});
  4164. model.addRow(new String[]{"20102","2号"});
  4165. JTable table = new JTable(model);
  4166. System.out.println(table.getSelectedColumn());
  4167. frame.getContentPane().add(table);
  4168. frame.setVisible(true);
  4169. //getSelectedRow方法——获得选中行的索引
  4170. JFrame frame = new JFrame();
  4171. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4172. frame.setLayout(new FlowLayout());
  4173. frame.setBounds(400,320,400,240);
  4174. DefaultTableModel model = new DefaultTableModel();
  4175. model.addColumn("学号");
  4176. model.addColumn("姓名");
  4177. model.addRow(new String[]{"20101","1号"});
  4178. model.addRow(new String[]{"20102","2号"});
  4179. JTable table = new JTable(model);
  4180. System.out.println(table.getSelectedRow());
  4181. frame.getContentPane().add(table);
  4182. frame.setVisible(true);
  4183. //print方法——打印调用该方法的表格
  4184. JFrame frame = new JFrame();
  4185. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4186. frame.setLayout(new FlowLayout());
  4187. frame.setBounds(400,320,400,240);
  4188. DefaultTableModel model = new DefaultTableModel();
  4189. model.addColumn("学号");
  4190. model.addColumn("姓名");
  4191. model.addRow(new String[]{"20101","1号"});
  4192. model.addRow(new String[]{"20102","2号"});
  4193. JTable table = new JTable(model);
  4194. table.print();
  4195. frame.getContentPane().add(table);
  4196. frame.setVisible(true);
  4197. //setAutoCreateRowSorter方法——设置表格能否根据行的内容进行排列
  4198. JFrame frame = new JFrame();
  4199. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4200. frame.setLayout(new FlowLayout());
  4201. frame.setBounds(400,320,400,240);
  4202. DefaultTableModel model = new DefaultTableModel();
  4203. model.addColumn("学号");
  4204. model.addColumn("姓名");
  4205. model.addRow(new String[]{"20101","1号"});
  4206. model.addRow(new String[]{"20102","2号"});
  4207. JTable table = new JTable(model);
  4208. table.setAutoCreateRowSorter(true);
  4209. frame.getContentPane().add(table);
  4210. frame.setVisible(true);
  4211. //setAutoResizeMode方法——指定改变表格大小时各列的调整方式
  4212. //指定改变表格大小时各列的调整方式
  4213. //setModel方法——为表格设置新的表格模型
  4214. JFrame frame = new JFrame();
  4215. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4216. frame.setLayout(new FlowLayout());
  4217. frame.setBounds(400,320,400,240);
  4218. DefaultTableModel model = new DefaultTableModel();
  4219. model.addColumn("学号");
  4220. model.addColumn("姓名");
  4221. model.addRow(new String[]{"20101","1号"});
  4222. model.addRow(new String[]{"20102","2号"});
  4223. JTable table = new JTable();
  4224. table.setModel(model);
  4225. frame.getContentPane().add(table);
  4226. frame.setVisible(true);
  4227. //setRowHeight方法——设置表格的列高
  4228. JFrame frame = new JFrame();
  4229. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4230. frame.setLayout(new FlowLayout());
  4231. frame.setBounds(400,320,400,240);
  4232. DefaultTableModel model = new DefaultTableModel();
  4233. model.addColumn("学号");
  4234. model.addColumn("姓名");
  4235. model.addRow(new String[]{"20101","1号"});
  4236. model.addRow(new String[]{"20102","2号"});
  4237. JTable table = new JTable(model);
  4238. table.setRowHeight(0,50);
  4239. frame.getContentPane().add(table);
  4240. frame.setVisible(true);
  4241. //setShowGrid方法——设置是否显示单元格的分隔线
  4242. JFrame frame = new JFrame();
  4243. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4244. frame.setLayout(new FlowLayout());
  4245. frame.setBounds(400,320,400,240);
  4246. DefaultTableModel model = new DefaultTableModel();
  4247. model.addColumn("学号");
  4248. model.addColumn("姓名");
  4249. model.addRow(new String[]{"20101","1号"});
  4250. model.addRow(new String[]{"20102","2号"});
  4251. JTable table = new JTable(model);
  4252. table.setShowGrid(false);
  4253. frame.getContentPane().add(table);
  4254. frame.setVisible(true);
  4255. //DefaultTableModel类
  4256. //DefaultTableModel构造方法
  4257. JFrame frame = new JFrame();
  4258. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4259. frame.setLayout(new FlowLayout());
  4260. frame.setBounds(400,320,400,240);
  4261. JTable table = new JTable();
  4262. DefaultTableModel model = new DefaultTableModel();
  4263. table.setModel(model);
  4264. frame.getContentPane().add(table);
  4265. frame.setVisible(true);
  4266. //addColumn方法——向表格模型中增加列
  4267. JFrame frame = new JFrame();
  4268. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4269. frame.setLayout(new FlowLayout());
  4270. frame.setBounds(400,320,400,240);
  4271. JTable table = new JTable();
  4272. DefaultTableModel model = new DefaultTableModel();
  4273. model.addColumn("学号");
  4274. model.addColumn("姓名");
  4275. table.setModel(model);
  4276. frame.getContentPane().add(table);
  4277. frame.setVisible(true);
  4278. //addRow方法——向表格模型中增加一行数据
  4279. JFrame frame = new JFrame();
  4280. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4281. frame.setLayout(new FlowLayout());
  4282. frame.setBounds(400,320,400,240);
  4283. JTable table = new JTable();
  4284. DefaultTableModel model = new DefaultTableModel();
  4285. model.addColumn("学号");
  4286. model.addColumn("姓名");
  4287. model.addRow(new String[]{"2001","2002"});
  4288. model.addRow(new String[]{"小张","小王"});
  4289. table.setModel(model);
  4290. frame.getContentPane().add(table);
  4291. frame.setVisible(true);
  4292. //getDataVector方法——将表格中的数据保存到一个向量中
  4293. JFrame frame = new JFrame();
  4294. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4295. frame.setLayout(new FlowLayout());
  4296. frame.setBounds(400,320,400,240);
  4297. JTable table = new JTable();
  4298. DefaultTableModel model = new DefaultTableModel();
  4299. model.addColumn("学号");
  4300. model.addColumn("姓名");
  4301. model.addRow(new String[]{"2001","2002"});
  4302. model.addRow(new String[]{"小张","小王"});
  4303. table.setModel(model);
  4304. Vector dateVector = model.getDataVector();
  4305. for (Iterator iterator = dateVector.iterator(); iterator.hasNext();) {
  4306. System.out.println(iterator.next());
  4307. }
  4308. frame.getContentPane().add(table);
  4309. frame.setVisible(true);
  4310. //setRowCount方法——设置表格模型中数据的行数
  4311. JFrame frame = new JFrame();
  4312. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4313. frame.setLayout(new FlowLayout());
  4314. frame.setBounds(400,320,400,240);
  4315. JTable table = new JTable();
  4316. DefaultTableModel model = new DefaultTableModel();
  4317. model.addColumn("学号");
  4318. model.addColumn("姓名");
  4319. model.addRow(new String[]{"2001","2002"});
  4320. model.addRow(new String[]{"小张","小王"});
  4321. table.setModel(model);
  4322. model.setRowCount(0);
  4323. frame.getContentPane().add(table);
  4324. frame.setVisible(true);
  4325. <!--树控件 ->
  4326. //javaxswingJTree类
  4327. //JTree构造方法
  4328. JFrame frame = new JFrame();
  4329. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4330. frame.setLayout(new FlowLayout());
  4331. frame.setBounds(400,320,400,240);
  4332. String[] nodes = {"JAVA","PHP","JSP"};
  4333. JTree tree = new JTree(nodes);
  4334. frame.getContentPane().add(tree);
  4335. frame.setVisible(true);
  4336. //addSelectionInterval方法——将树控件指定范围内的路径增加到选择状态中
  4337. JFrame frame = new JFrame();
  4338. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4339. frame.setLayout(new FlowLayout());
  4340. frame.setBounds(400,320,400,240);
  4341. String[] nodes = {"JAVA","PHP","JSP"};
  4342. JTree tree = new JTree(nodes);
  4343. tree.addSelectionInterval(0, 2);
  4344. frame.getContentPane().add(tree);
  4345. frame.setVisible(true);
  4346. //addSelectionRow方法——将位于特定行的路径增加到选择状态中
  4347. JFrame frame = new JFrame();
  4348. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4349. frame.setLayout(new FlowLayout());
  4350. frame.setBounds(400,320,400,240);
  4351. String[] nodes = {"JAVA","PHP","JSP"};
  4352. JTree tree = new JTree(nodes);
  4353. tree.addSelectionRow(0);
  4354. frame.getContentPane().add(tree);
  4355. frame.setVisible(true);
  4356. //addTreeExpansionListener方法——监听树结点展开合并事件
  4357. JFrame frame = new JFrame();
  4358. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4359. frame.setLayout(new FlowLayout());
  4360. frame.setBounds(400,320,400,240);
  4361. String[] nodes = {"JAVA","PHP","JSP"};
  4362. JTree tree = new JTree(nodes);
  4363. tree.addTreeExpansionListener(new TreeExpansionListener() {
  4364. @Override
  4365. public void treeExpanded(TreeExpansionEvent event) {
  4366. JOptionPane.showMessageDialog(null, "合并节点");
  4367. }
  4368. @Override
  4369. public void treeCollapsed(TreeExpansionEvent event) {
  4370. JOptionPane.showMessageDialog(null, "展开节点");
  4371. }
  4372. });
  4373. frame.getContentPane().add(tree);
  4374. frame.setVisible(true);
  4375. //addTreeSelectionListener方法——监听树结点的选择事件
  4376. JFrame frame = new JFrame();
  4377. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4378. frame.setLayout(new FlowLayout());
  4379. frame.setBounds(400,320,400,240);
  4380. String[] nodes = {"JAVA","PHP","JSP"};
  4381. JTree tree = new JTree(nodes);
  4382. tree.addTreeSelectionListener(new TreeSelectionListener() {
  4383. @Override
  4384. public void valueChanged(TreeSelectionEvent e) {
  4385. JOptionPane.showMessageDialog(null, "选择了一个节点");
  4386. return;
  4387. }
  4388. });
  4389. frame.getContentPane().add(tree);
  4390. frame.setVisible(true);
  4391. //getLastSelectedPathComponent方法——获得用户选择的结点
  4392. JFrame frame = new JFrame();
  4393. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4394. frame.setLayout(new FlowLayout());
  4395. frame.setBounds(400,320,400,240);
  4396. String[] nodes = {"JAVA","PHP","JSP"};
  4397. final JTree tree = new JTree(nodes);
  4398. tree.addTreeSelectionListener(new TreeSelectionListener() {
  4399. @Override
  4400. public void valueChanged(TreeSelectionEvent e) {
  4401. Object selectObject = tree.getLastSelectedPathComponent();
  4402. JOptionPane.showMessageDialog(null, "选择"+selectObject);
  4403. }
  4404. });
  4405. frame.getContentPane().add(tree);
  4406. frame.setVisible(true);
  4407. //getModel方法——获得树模型
  4408. JFrame frame = new JFrame();
  4409. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4410. frame.setLayout(new FlowLayout());
  4411. frame.setBounds(400,320,400,240);
  4412. String[] nodes = {"JAVA","PHP","JSP"};
  4413. final JTree tree = new JTree(nodes);
  4414. TreeModel model = tree.getModel();
  4415. System.out.println(model.getRoot());
  4416. frame.getContentPane().add(tree);
  4417. frame.setVisible(true);
  4418. //getRowCount方法——输出树结构中当前显示的行数
  4419. JFrame frame = new JFrame();
  4420. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4421. frame.setLayout(new FlowLayout());
  4422. frame.setBounds(400,320,400,240);
  4423. String[] nodes = {"JAVA","PHP","JSP"};
  4424. final JTree tree = new JTree(nodes);
  4425. System.out.println(tree.getRowCount());
  4426. frame.getContentPane().add(tree);
  4427. frame.setVisible(true);
  4428. //setCellEditor方法——设置树结点编辑器
  4429. JFrame frame = new JFrame();
  4430. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4431. frame.setLayout(new FlowLayout());
  4432. frame.setBounds(400,320,400,240);
  4433. String[] nodes = {"JAVA","PHP","JSP"};
  4434. final JTree tree = new JTree(nodes);
  4435. tree.setCellEditor(new DefaultCellEditor(new JTextField()));
  4436. tree.setEditable(true);
  4437. frame.getContentPane().add(tree);
  4438. frame.setVisible(true);
  4439. //setCellRenderer方法——为树控件设置渲染器
  4440. JFrame frame = new JFrame();
  4441. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4442. frame.setLayout(new FlowLayout());
  4443. frame.setBounds(400,320,400,240);
  4444. String[] nodes = {"JAVA","PHP","JSP"};
  4445. final JTree tree = new JTree(nodes);
  4446. DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
  4447. renderer.setLeafIcon(new ImageIcon("src/1.jpg"));
  4448. tree.setCellRenderer(renderer);
  4449. frame.getContentPane().add(tree);
  4450. frame.setVisible(true);
  4451. //setRootVisible方法——设置根结点是否可见
  4452. JFrame frame = new JFrame();
  4453. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4454. frame.setLayout(new FlowLayout());
  4455. frame.setBounds(400,320,400,240);
  4456. String[] nodes = {"JAVA","PHP","JSP"};
  4457. final JTree tree = new JTree(nodes);
  4458. tree.setRootVisible(true);
  4459. frame.getContentPane().add(tree);
  4460. frame.setVisible(true);
  4461. //setRowHeight方法——设置结点的高度
  4462. JFrame frame = new JFrame();
  4463. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4464. frame.setLayout(new FlowLayout());
  4465. frame.setBounds(400,320,400,240);
  4466. String[] nodes = {"JAVA","PHP","JSP"};
  4467. final JTree tree = new JTree(nodes);
  4468. tree.setRowHeight(50);
  4469. frame.getContentPane().add(tree);
  4470. frame.setVisible(true);
  4471. //setShowsRootHandles方法——设置是否显示“把手”
  4472. JFrame frame = new JFrame();
  4473. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4474. frame.setLayout(new FlowLayout());
  4475. frame.setBounds(400,320,400,240);
  4476. String[] nodes = {"JAVA","PHP","JSP"};
  4477. final JTree tree = new JTree(nodes);
  4478. tree.setShowsRootHandles(false);
  4479. frame.getContentPane().add(tree);
  4480. frame.setVisible(true);
  4481. //javaxswingtreeDefaultTreeModel类
  4482. //getChild方法——获得指定结点的指定位置的元素
  4483. JFrame frame = new JFrame();
  4484. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4485. frame.setLayout(new FlowLayout());
  4486. frame.setBounds(400,320,400,240);
  4487. DefaultMutableTreeNode root = new DefaultMutableTreeNode("图书");
  4488. DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("人工智能");
  4489. parent1.add(new DefaultMutableTreeNode("大数据"));
  4490. parent1.add(new DefaultMutableTreeNode("大数据1"));
  4491. parent1.add(new DefaultMutableTreeNode("大数据2"));
  4492. parent1.add(new DefaultMutableTreeNode("大数据3"));
  4493. root.add(parent1);
  4494. DefaultTreeModel model = new DefaultTreeModel(root);
  4495. System.out.println(model.getChild(parent1, 3));
  4496. JTree jTree = new JTree(model);
  4497. frame.getContentPane().add(jTree);
  4498. frame.setVisible(true);
  4499. //getChildCount方法——获得指定结点的子结点个数
  4500. JFrame frame = new JFrame();
  4501. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4502. frame.setLayout(new FlowLayout());
  4503. frame.setBounds(400,320,400,240);
  4504. DefaultMutableTreeNode root = new DefaultMutableTreeNode("图书");
  4505. DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("人工智能");
  4506. parent1.add(new DefaultMutableTreeNode("大数据"));
  4507. parent1.add(new DefaultMutableTreeNode("大数据1"));
  4508. parent1.add(new DefaultMutableTreeNode("大数据2"));
  4509. parent1.add(new DefaultMutableTreeNode("大数据3"));
  4510. root.add(parent1);
  4511. DefaultTreeModel model = new DefaultTreeModel(root);
  4512. System.out.println(model.getChildCount(parent1));
  4513. JTree jTree = new JTree(model);
  4514. frame.getContentPane().add(jTree);
  4515. frame.setVisible(true);
  4516. //getRoot方法——获得树模型的根结点
  4517. JFrame frame = new JFrame();
  4518. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4519. frame.setLayout(new FlowLayout());
  4520. frame.setBounds(400,320,400,240);
  4521. DefaultMutableTreeNode root = new DefaultMutableTreeNode("图书");
  4522. DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("人工智能");
  4523. parent1.add(new DefaultMutableTreeNode("大数据"));
  4524. parent1.add(new DefaultMutableTreeNode("大数据1"));
  4525. parent1.add(new DefaultMutableTreeNode("大数据2"));
  4526. parent1.add(new DefaultMutableTreeNode("大数据3"));
  4527. root.add(parent1);
  4528. DefaultTreeModel model = new DefaultTreeModel(root);
  4529. System.out.println(model.getRoot());
  4530. JTree jTree = new JTree(model);
  4531. frame.getContentPane().add(jTree);
  4532. frame.setVisible(true);
  4533. //insertNodeInto方法——向树模型中指定位置插入新结点
  4534. //插入新的节点
  4535. //removeNodeFromParent方法——从树模型中删除结点
  4536. //删除节点
  4537. <!--进度指示控件 ->
  4538. //javaxswingJProgressBar控件
  4539. //JProgressBar构造方法
  4540. JFrame frame = new JFrame();
  4541. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4542. frame.setLayout(new FlowLayout());
  4543. frame.setBounds(400,320,400,240);
  4544. JProgressBar jProgressBar = new JProgressBar(0,100);
  4545. frame.getContentPane().add(jProgressBar);
  4546. frame.setVisible(true);
  4547. //addChangeListener方法——监听进度条变化事件
  4548. JFrame frame = new JFrame();
  4549. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4550. frame.setLayout(new FlowLayout());
  4551. frame.setBounds(400,320,400,240);
  4552. JProgressBar jProgressBar = new JProgressBar(0,100);
  4553. jProgressBar.addChangeListener(new ChangeListener() {
  4554. @Override
  4555. public void stateChanged(ChangeEvent e) {
  4556. System.out.println("启动完毕");
  4557. }
  4558. });
  4559. frame.getContentPane().add(jProgressBar);
  4560. frame.setVisible(true);
  4561. }
  4562. //getValue方法——获得当前进度条显示的值
  4563. JFrame frame = new JFrame();
  4564. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4565. frame.setLayout(new FlowLayout());
  4566. frame.setBounds(400,320,400,240);
  4567. JProgressBar jProgressBar = new JProgressBar(0,100);
  4568. System.out.println(jProgressBar.getValue());
  4569. frame.getContentPane().add(jProgressBar);
  4570. frame.setVisible(true);
  4571. //setBorderPainted方法——设置是否绘制滚动条的边框
  4572. JFrame frame = new JFrame();
  4573. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4574. frame.setLayout(new FlowLayout());
  4575. frame.setBounds(400,320,400,240);
  4576. JProgressBar jProgressBar = new JProgressBar(0,100);
  4577. jProgressBar.setBorderPainted(false);
  4578. frame.getContentPane().add(jProgressBar);
  4579. frame.setVisible(true);
  4580. //setIndeterminate方法——设置滚动条持续滚动
  4581. JFrame frame = new JFrame();
  4582. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4583. frame.setLayout(new FlowLayout());
  4584. frame.setBounds(400,320,400,240);
  4585. JProgressBar jProgressBar = new JProgressBar(0,100);
  4586. jProgressBar.setIndeterminate(true);
  4587. frame.getContentPane().add(jProgressBar);
  4588. frame.setVisible(true);
  4589. //setString方法——设置在滚动条上显示的文本
  4590. JFrame frame = new JFrame();
  4591. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4592. frame.setLayout(new FlowLayout());
  4593. frame.setBounds(400,320,400,240);
  4594. JProgressBar jProgressBar = new JProgressBar(0,100);
  4595. jProgressBar.setString("图书");
  4596. jProgressBar.setStringPainted(true);
  4597. frame.getContentPane().add(jProgressBar);
  4598. frame.setVisible(true);
  4599. //setValue方法——设置滚动条当前显示的值
  4600. JFrame frame = new JFrame();
  4601. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4602. frame.setLayout(new FlowLayout());
  4603. frame.setBounds(400,320,400,240);
  4604. JProgressBar jProgressBar = new JProgressBar(0,100);
  4605. jProgressBar.setValue(50);
  4606. System.out.println(jProgressBar.getValue());
  4607. frame.getContentPane().add(jProgressBar);
  4608. frame.setVisible(true);
  4609. //javaxswingProgressMonitor控件
  4610. //ProgressMonitor构造方法
  4611. //创建进度显示器
  4612. //close方法——关闭进度显示器
  4613. //关闭进度显示器
  4614. //setNote方法——设置进度显示器上显示的文本
  4615. //设置进度显示器上的文本
  4616. <!--高级布局管理器 ->
  4617. //javaawtGridBagLayout网格包布局
  4618. //GridBagLayout构造方法
  4619. JFrame frame = new JFrame();
  4620. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4621. frame.setBounds(400,320,400,240);
  4622. GridLayout gridBagLayout = new GridLayout();
  4623. frame.getContentPane().setLayout(gridBagLayout);
  4624. frame.setVisible(true);
  4625. //anchor属性——设置组件在其所显示区域的显示位置
  4626. //设置组件在其显示区域的显示位置,通常有9个方位
  4627. //fill属性——设置组件的填充方式
  4628. //fill属性可以设置组件填充
  4629. //gridx属性和gridy属性——设置组件起始点所在单元格的索引值
  4630. JFrame frame = new JFrame();
  4631. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4632. frame.setBounds(400,320,400,240);
  4633. GridLayout gridBagLayout = new GridLayout();
  4634. JButton button = new JButton();
  4635. button.setText("0,0");
  4636. GridBagConstraints gridBagConstraints = new GridBagConstraints();
  4637. gridBagConstraints.gridy = 0;
  4638. gridBagConstraints.gridx = 0;
  4639. frame.getContentPane().setLayout(gridBagLayout);
  4640. frame.getContentPane().add(button,gridBagConstraints);
  4641. frame.setVisible(true);
  4642. //gridheight属性和gridwidth属性——设置组件占用网格的行数和列数
  4643. JFrame frame = new JFrame();
  4644. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4645. frame.setBounds(400,320,400,240);
  4646. GridLayout gridBagLayout = new GridLayout();
  4647. JButton button = new JButton();
  4648. button.setText("0,0");
  4649. GridBagConstraints gridBagConstraints = new GridBagConstraints();
  4650. gridBagConstraints.gridy = 0;
  4651. gridBagConstraints.gridx = 0;
  4652. gridBagConstraints.gridwidth = 2;
  4653. frame.getContentPane().setLayout(gridBagLayout);
  4654. frame.getContentPane().add(button,gridBagConstraints);
  4655. frame.setVisible(true);
  4656. //insets属性——设置组件四周与单元格边缘之间的最小距离
  4657. JFrame frame = new JFrame();
  4658. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4659. frame.setBounds(400,320,400,240);
  4660. GridLayout gridBagLayout = new GridLayout();
  4661. JButton button = new JButton();
  4662. button.setText("0,0");
  4663. GridBagConstraints gridBagConstraints = new GridBagConstraints();
  4664. gridBagConstraints.insets = new Insets(10, 10, 10, 10);
  4665. gridBagConstraints.gridx = 1;
  4666. gridBagConstraints.gridy = 1;
  4667. frame.getContentPane().setLayout(gridBagLayout);
  4668. frame.getContentPane().add(button,gridBagConstraints);
  4669. frame.setVisible(true);
  4670. //ipadx属性和ipady属性——修改组件的首选大小
  4671. JFrame frame = new JFrame();
  4672. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4673. frame.setBounds(400,320,400,240);
  4674. GridLayout gridBagLayout = new GridLayout();
  4675. JButton button = new JButton();
  4676. button.setText("0,0");
  4677. GridBagConstraints gridBagConstraints = new GridBagConstraints();
  4678. gridBagConstraints.ipadx = 10;
  4679. gridBagConstraints.ipady = 20;
  4680. gridBagConstraints.gridx = 1;
  4681. gridBagConstraints.gridy = 1;
  4682. frame.getContentPane().setLayout(gridBagLayout);
  4683. frame.getContentPane().add(button,gridBagConstraints);
  4684. frame.setVisible(true);
  4685. //weightx属性和weighty属性——设置网格组的每一行和每一列对额外空间的分布方式
  4686. JFrame frame = new JFrame();
  4687. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4688. frame.setBounds(400,320,400,240);
  4689. GridLayout gridBagLayout = new GridLayout();
  4690. JButton button = new JButton();
  4691. button.setText("0,0");
  4692. GridBagConstraints gridBagConstraints = new GridBagConstraints();
  4693. gridBagConstraints.weightx = 40;
  4694. gridBagConstraints.weighty = 40;
  4695. gridBagConstraints.gridx = 1;
  4696. gridBagConstraints.gridy = 1;
  4697. frame.getContentPane().setLayout(gridBagLayout);
  4698. frame.getContentPane().add(button,gridBagConstraints);
  4699. frame.setVisible(true);
  4700. //javaxswingSpringLayout弹簧布局
  4701. //SpringLayout构造方法
  4702. JFrame frame = new JFrame();
  4703. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4704. frame.setBounds(400,320,400,240);
  4705. SpringLayout springLayout = new SpringLayout();
  4706. frame.getContentPane().setLayout(springLayout);
  4707. frame.setVisible(true);
  4708. //getConstraints方法——获得组件的SpringLayoutConstraints对象
  4709. //获得组件的SpringLayoutConstraints对象
  4710. //putConstraint方法——建立组件之间各个边的约束
  4711. //建立组件之间各个边的约束
  4712. //javaxswingBoxLayout箱式布局
  4713. //BoxLayout构造方法
  4714. JFrame frame = new JFrame();
  4715. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  4716. frame.setBounds(400,320,400,240);
  4717. BoxLayout hBox = new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS);
  4718. frame.getContentPane().setLayout(hBox);
  4719. frame.setVisible(true);
  4720. //Box容器——箱式容器
  4721. //布局管理器轻量级容器
  4722. <!--输入/输出流 ->
  4723. //javaioBufferedInputStream——缓冲字节输入流
  4724. //available方法——返回可读取的估计字节数
  4725. File file = new File("c:/test.txt");
  4726. FileInputStream in = new FileInputStream(file);
  4727. BufferedInputStream bis = new BufferedInputStream(in);
  4728. bis.read();
  4729. System.out.println(bis.available());
  4730. bis.close();
  4731. in.close();
  4732. //close方法——关闭输入流并释放与该流关联的所有系统资源
  4733. File file = new File("c:/test.txt");
  4734. FileInputStream in = new FileInputStream(file);
  4735. BufferedInputStream bis = new BufferedInputStream(in);
  4736. bis.read();
  4737. System.out.println(bis.available());
  4738. bis.close();
  4739. in.close();
  4740. //mark方法——记录当前指针所在的位置
  4741. File file = new File("c:/test.txt");
  4742. FileInputStream in = new FileInputStream(file);
  4743. BufferedInputStream bis = new BufferedInputStream(in);
  4744. bis.mark(5);
  4745. int count = 0;
  4746. for (int i = 0; i < file.length(); i++) {
  4747. count++;
  4748. int read = bis.read();
  4749. if (count%5 == 0) {
  4750. bis.reset();
  4751. System.out.println(read);
  4752. }
  4753. }
  4754. bis.close();
  4755. in.close();
  4756. //markSupported方法——测试是否支持mark方法和reset方法
  4757. File file = new File("c:/test.txt");
  4758. FileInputStream in = new FileInputStream(file);
  4759. BufferedInputStream bis = new BufferedInputStream(in);
  4760. if(bis.markSupported()){
  4761. System.out.println("BUFFERED类支持mark和reset方法");
  4762. }
  4763. bis.close();
  4764. in.close();
  4765. //read方法——从输入流中读取数据
  4766. File file = new File("c:/test.txt");
  4767. FileInputStream in = new FileInputStream(file);
  4768. BufferedInputStream bis = new BufferedInputStream(in);
  4769. bis.read();
  4770. System.out.println(bis.available());
  4771. bis.close();
  4772. in.close();
  4773. //reset方法——重新定位输入流
  4774. File file = new File("c:/test.txt");
  4775. FileInputStream in = new FileInputStream(file);
  4776. BufferedInputStream bis = new BufferedInputStream(in);
  4777. bis.mark(5);
  4778. int count = 0;
  4779. for (int i = 0; i < file.length(); i++) {
  4780. count++;
  4781. int read = bis.read();
  4782. if (count%5 == 0) {
  4783. bis.reset();
  4784. System.out.println(read);
  4785. }
  4786. }
  4787. bis.close();
  4788. in.close();
  4789. //skip方法——跳过并丢弃指定字节数量的数据
  4790. File file = new File("c:/test.txt");
  4791. FileInputStream in = new FileInputStream(file);
  4792. BufferedInputStream bis = new BufferedInputStream(in);
  4793. bis.skip(5);
  4794. int i = 0;
  4795. while ((i = bis.read())!=-1) {
  4796. System.out.println(i);
  4797. }
  4798. bis.close();
  4799. in.close();
  4800. //javaioBufferedOutputStream——缓冲字节输出流
  4801. File file = new File("c:/test.txt");
  4802. FileOutputStream fos = new FileOutputStream(file);
  4803. BufferedOutputStream bus = new BufferedOutputStream(fos);
  4804. byte[] by = "香蕉".getBytes();
  4805. bus.write(by);
  4806. bus.flush();
  4807. bus.close();
  4808. //flush方法——刷新此缓冲输出流
  4809. File file = new File("c:/test.txt");
  4810. FileOutputStream fos = new FileOutputStream(file);
  4811. BufferedOutputStream bus = new BufferedOutputStream(fos);
  4812. byte[] by = "香蕉".getBytes();
  4813. bus.write(by);
  4814. bus.flush();
  4815. bus.close();
  4816. //write方法——向输出流中写数据
  4817. File file = new File("c:/test.txt");
  4818. FileOutputStream fos = new FileOutputStream(file);
  4819. BufferedOutputStream bus = new BufferedOutputStream(fos);
  4820. byte[] by = "香蕉".getBytes();
  4821. bus.write(by);
  4822. bus.flush();
  4823. bus.close();
  4824. //javaioBufferedReader——缓冲字符输入流
  4825. //close方法——关闭流并释放与之关联的所有资源
  4826. BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
  4827. System.out.println("输入一个字符串");
  4828. String str = buf.readLine();
  4829. buf.close();
  4830. System.out.println(str);
  4831. //mark方法——记录当前读指针所在的位置
  4832. File file = new File("c:/test.txt");
  4833. BufferedReader buf = new BufferedReader(new FileReader(file));
  4834. int count = 0;
  4835. buf.mark(1);
  4836. for (int i = 0; i < file.length(); i++) {
  4837. count++;
  4838. int read = buf.read();
  4839. if(count%5==0){
  4840. buf.reset();
  4841. System.out.println(read);
  4842. }
  4843. }
  4844. buf.close();
  4845. //markSupported方法——判断指定流是否支持mark操作
  4846. File file = new File("c:/test.txt");
  4847. BufferedReader buf = new BufferedReader(new FileReader(file));
  4848. if(buf.markSupported()){
  4849. System.out.println("BufferedReader支持mark");
  4850. }
  4851. buf.close();
  4852. //read方法——从缓冲输入流中读取字符
  4853. File file = new File("c:/test.txt");
  4854. BufferedReader buf = new BufferedReader(new FileReader(file));
  4855. int count = 0;
  4856. buf.mark(1);
  4857. for (int i = 0; i < file.length(); i++) {
  4858. count++;
  4859. int read = buf.read();
  4860. if(count%5==0){
  4861. buf.reset();
  4862. System.out.println(read);
  4863. }
  4864. }
  4865. buf.close();
  4866. //readLine方法——读取一个文本行
  4867. BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
  4868. System.out.println("输入一个字符串");
  4869. String str = buf.readLine();
  4870. buf.close();
  4871. System.out.println(str);
  4872. //ready方法——判断此流是否已准备好被读取
  4873. File file = new File("c:/test.txt");
  4874. BufferedReader buf = new BufferedReader(new FileReader(file));
  4875. if(buf.ready()){
  4876. System.out.println(buf.readLine());
  4877. }
  4878. buf.close();
  4879. //reset方法——重定位指针
  4880. File file = new File("c:/test.txt");
  4881. BufferedReader buf = new BufferedReader(new FileReader(file));
  4882. int count = 0;
  4883. buf.mark(10);
  4884. for(int i= 0; i< file.length();i++){
  4885. count++;
  4886. int read = buf.read();
  4887. if(count%3 == 0)
  4888. buf.reset();
  4889. System.out.println(read);
  4890. }
  4891. buf.close();
  4892. //skip方法——跳过指定数量的字符
  4893. File file = new File("c:/test.txt");
  4894. BufferedReader buf = new BufferedReader(new FileReader(file));
  4895. buf.skip(10);
  4896. int i;
  4897. while((i = buf.read())!=-1){
  4898. System.out.println(i);
  4899. }
  4900. buf.close();
  4901. //javaioBufferedWriter——缓冲字符输出流
  4902. //close方法——关闭流
  4903. File file = new File("c:/test.txt");
  4904. FileWriter fw = new FileWriter(file);
  4905. BufferedWriter bufw = new BufferedWriter(fw);
  4906. String str = "最近好吗";
  4907. bufw.write(str);
  4908. bufw.close();
  4909. fw.close();
  4910. //flush方法——刷新缓冲区
  4911. File file = new File("c:/test.txt");
  4912. FileWriter fw = new FileWriter(file);
  4913. String strArr[] = {"词典","参考大全","手册"};
  4914. BufferedWriter bufw = new BufferedWriter(fw);
  4915. for (int i = 0; i < strArr.length; i++) {
  4916. bufw.write(strArr[i]);
  4917. }
  4918. bufw.flush();
  4919. fw.close();
  4920. //newLine方法——写入一个行分隔符
  4921. File file = new File("c:/test.txt");
  4922. FileWriter fw = new FileWriter(file);
  4923. String strArr[] = {"词典1","参考大全1","手册1"};
  4924. BufferedWriter bufw = new BufferedWriter(fw);
  4925. for (int i = 0; i < strArr.length; i++) {
  4926. bufw.write(strArr[i]);
  4927. bufw.newLine();
  4928. }
  4929. bufw.flush();
  4930. fw.close();
  4931. //write方法——向缓冲区中写数据
  4932. File file = new File("c:/test.txt");
  4933. FileWriter fw = new FileWriter(file);
  4934. char[] ch = {'a','b','c'};
  4935. BufferedWriter bufw = new BufferedWriter(fw);
  4936. bufw.write(ch,0,3);
  4937. bufw.flush();
  4938. fw.close();
  4939. //javaioDataInputStream——数据输入流
  4940. //read方法——读取一定数量的字节
  4941. File file = new File("c:/test.txt");
  4942. FileInputStream fis = new FileInputStream(file);
  4943. DataInputStream dis = new DataInputStream(fis);
  4944. byte[] b = new byte[50];
  4945. dis.read(b);
  4946. for (int i = 0; i < b.length; i++) {
  4947. System.out.println(b[i]);
  4948. }
  4949. dis.close();
  4950. fis.close();
  4951. //readByte方法——读取此操作需要的字节
  4952. File file = new File("c:/test.txt");
  4953. FileInputStream fis = new FileInputStream(file);
  4954. DataInputStream dis = new DataInputStream(fis);
  4955. System.out.println(dis.readByte());
  4956. dis.close();
  4957. fis.close();
  4958. //readBoolean方法——读取一个布尔值
  4959. File file = new File("c:/test.txt");
  4960. FileInputStream fis = new FileInputStream(file);
  4961. DataInputStream dis = new DataInputStream(fis);
  4962. System.out.println(dis.readBoolean());
  4963. dis.close();
  4964. fis.close();
  4965. //readchar方法——读取此操作需要的字符
  4966. File file = new File("c:/test.txt");
  4967. FileInputStream fis = new FileInputStream(file);
  4968. DataInputStream dis = new DataInputStream(fis);
  4969. System.out.println(dis.readChar());
  4970. dis.close();
  4971. fis.close();
  4972. //readDouble方法——读取一个double值
  4973. File file = new File("c:/test.txt");
  4974. FileInputStream fis = new FileInputStream(file);
  4975. DataInputStream dis = new DataInputStream(fis);
  4976. System.out.println(dis.readDouble());
  4977. dis.close();
  4978. fis.close();
  4979. //readFloat方法——读取一个float值
  4980. File file = new File("c:/test.txt");
  4981. FileInputStream fis = new FileInputStream(file);
  4982. DataInputStream dis = new DataInputStream(fis);
  4983. System.out.println(dis.readFloat());
  4984. dis.close();
  4985. fis.close();
  4986. //readFully方法——读取一些字节
  4987. File file = new File("c:/test.txt");
  4988. FileInputStream fis = new FileInputStream(file);
  4989. DataInputStream dis = new DataInputStream(fis);
  4990. byte[] b = new byte[20];
  4991. dis.readFully(b);
  4992. for (int i = 0; i < b.length; i++) {
  4993. System.out.println(b[i]);
  4994. }
  4995. dis.close();
  4996. fis.close();
  4997. //readInt方法——读取一个int值
  4998. File file = new File("c:/test.txt");
  4999. FileInputStream fis = new FileInputStream(file);
  5000. DataInputStream dis = new DataInputStream(fis);
  5001. System.out.println(dis.readInt());
  5002. dis.close();
  5003. fis.close();
  5004. //readLong方法——读取一个long值
  5005. File file = new File("c:/test.txt");
  5006. FileInputStream fis = new FileInputStream(file);
  5007. DataInputStream dis = new DataInputStream(fis);
  5008. System.out.println(dis.readLong());
  5009. dis.close();
  5010. fis.close();
  5011. //readShort方法——读取一个short值
  5012. File file = new File("c:/test.txt");
  5013. FileInputStream fis = new FileInputStream(file);
  5014. DataInputStream dis = new DataInputStream(fis);
  5015. System.out.println(dis.readShort());
  5016. dis.close();
  5017. fis.close();
  5018. //readUnsignedByte方法——读取无符号的byte值
  5019. File file = new File("c:/test.txt");
  5020. FileInputStream fis = new FileInputStream(file);
  5021. DataInputStream dis = new DataInputStream(fis);
  5022. System.out.println(dis.readUnsignedByte());
  5023. dis.close();
  5024. fis.close();
  5025. //readUnsignedShort方法——读取无符号的short值
  5026. File file = new File("c:/test.txt");
  5027. FileInputStream fis = new FileInputStream(file);
  5028. DataInputStream dis = new DataInputStream(fis);
  5029. System.out.println(dis.readUnsignedShort());
  5030. dis.close();
  5031. fis.close();
  5032. //readUTF方法——读取UTF-编码的字符串
  5033. File file = new File("c:/test.txt");
  5034. FileOutputStream fos = new FileOutputStream(file);
  5035. DataOutputStream dos = new DataOutputStream(fos);
  5036. dos.writeUTF("图书");
  5037. FileInputStream fis = new FileInputStream(file);
  5038. DataInputStream dis = new DataInputStream(fis);
  5039. System.out.println(dis.readUTF());
  5040. dos.close();
  5041. dis.close();
  5042. fos.close();
  5043. fis.close();
  5044. //skipBytes方法——在输入流中跳过数据的n字节
  5045. File file = new File("c:/test.txt");
  5046. FileInputStream fis = new FileInputStream(file);
  5047. DataInputStream dis = new DataInputStream(fis);
  5048. byte[] b = new byte[50];
  5049. dis.skipBytes(10);
  5050. dis.read(b);
  5051. for (int i = 0; i < b.length; i++) {
  5052. System.out.println(b[i]);
  5053. }
  5054. dis.close();
  5055. fis.close();
  5056. //javaioDataOutputStream——数据输出流
  5057. //flush方法——清空此数据输出流
  5058. File file = new File("c:/test.txt");
  5059. FileOutputStream fos = new FileOutputStream(file);
  5060. DataOutputStream dos = new DataOutputStream(fos);
  5061. byte[] b = "欢迎使用".getBytes();
  5062. dos.write(b);
  5063. dos.flush();
  5064. dos.close();
  5065. fos.close();
  5066. //size方法——返回计数器written的当前值
  5067. File file = new File("c:/test.txt");
  5068. FileOutputStream fos = new FileOutputStream(file);
  5069. DataOutputStream dos = new DataOutputStream(fos);
  5070. byte[] b = "欢迎使用".getBytes();
  5071. dos.write(b);
  5072. System.out.println(dos.size());
  5073. dos.flush();
  5074. dos.close();
  5075. fos.close();
  5076. //write方法——写入基础输出流
  5077. File file = new File("c:/test.txt");
  5078. FileOutputStream fos = new FileOutputStream(file);
  5079. DataOutputStream dos = new DataOutputStream(fos);
  5080. byte[] b = "欢迎使用".getBytes();
  5081. dos.write(b);
  5082. dos.flush();
  5083. dos.close();
  5084. fos.close();
  5085. //writeBoolean方法——将boolean值写入输出流
  5086. File file = new File("c:/test.txt");
  5087. FileOutputStream fos = new FileOutputStream(file);
  5088. DataOutputStream dos = new DataOutputStream(fos);
  5089. dos.writeBoolean(true);
  5090. dos.flush();
  5091. dos.close();
  5092. fos.close();
  5093. //writeByte方法——将byte值写入输出流
  5094. File file = new File("c:/test.txt");
  5095. FileOutputStream fos = new FileOutputStream(file);
  5096. DataOutputStream dos = new DataOutputStream(fos);
  5097. dos.writeByte(99);
  5098. dos.flush();
  5099. dos.close();
  5100. fos.close();
  5101. //writeBytes方法——将字符串中每一个字符的低字节内容写入输出流
  5102. File file = new File("c:/test.txt");
  5103. FileOutputStream fos = new FileOutputStream(file);
  5104. DataOutputStream dos = new DataOutputStream(fos);
  5105. dos.writeBytes("JAVA");
  5106. dos.flush();
  5107. dos.close();
  5108. fos.close();
  5109. //writeChar方法——将char值写入输出流
  5110. File file = new File("c:/test.txt");
  5111. FileOutputStream fos = new FileOutputStream(file);
  5112. DataOutputStream dos = new DataOutputStream(fos);
  5113. char v = 'a';
  5114. dos.writeChar(v);
  5115. dos.flush();
  5116. dos.close();
  5117. fos.close();
  5118. //writechars方法——将字符串每个char值写入输出流
  5119. File file = new File("c:/test.txt");
  5120. FileOutputStream fos = new FileOutputStream(file);
  5121. DataOutputStream dos = new DataOutputStream(fos);
  5122. dos.writeChars("JAVA");
  5123. dos.flush();
  5124. dos.close();
  5125. fos.close();
  5126. //writeDouble方法——将double值写入输出流
  5127. File file = new File("c:/test.txt");
  5128. FileOutputStream fos = new FileOutputStream(file);
  5129. DataOutputStream dos = new DataOutputStream(fos);
  5130. double v = 123.10;
  5131. dos.writeDouble(v);
  5132. dos.flush();
  5133. dos.close();
  5134. fos.close();
  5135. //writeFloat方法——将float值写入输出流
  5136. File file = new File("c:/test.txt");
  5137. FileOutputStream fos = new FileOutputStream(file);
  5138. DataOutputStream dos = new DataOutputStream(fos);
  5139. float v = (float) 123.100;
  5140. dos.writeFloat(v);
  5141. dos.flush();
  5142. dos.close();
  5143. fos.close();
  5144. //writeLong方法——将long值写入输出流
  5145. File file = new File("c:/test.txt");
  5146. FileOutputStream fos = new FileOutputStream(file);
  5147. DataOutputStream dos = new DataOutputStream(fos);
  5148. long v = 123456;
  5149. dos.writeLong(v);
  5150. dos.flush();
  5151. dos.close();
  5152. fos.close();
  5153. //writeShort方法——将short值写入输出流
  5154. File file = new File("c:/test.txt");
  5155. FileOutputStream fos = new FileOutputStream(file);
  5156. DataOutputStream dos = new DataOutputStream(fos);
  5157. short v = 123;
  5158. dos.writeShort(v);
  5159. dos.flush();
  5160. dos.close();
  5161. fos.close();
  5162. //writeUTF方法——将UTF-编码字符写入输出流
  5163. File file = new File("c:/test.txt");
  5164. FileOutputStream fos = new FileOutputStream(file);
  5165. DataOutputStream dos = new DataOutputStream(fos);
  5166. dos.writeUTF("写入文件的数据");
  5167. dos.flush();
  5168. dos.close();
  5169. fos.close();
  5170. //javaioInputStream——字节输入流
  5171. //available方法——获取有效的可读取字节数
  5172. File file = new File("c:/test.txt");
  5173. FileInputStream in = new FileInputStream(file);
  5174. System.out.println(in.available());
  5175. //close方法——关闭输入流
  5176. InputStream is = System.in;
  5177. byte[] b = new byte[50];
  5178. is.read(b);
  5179. System.out.println("控制台内容"+new String(b).trim());
  5180. is.close();
  5181. //mark方法——在此输入流中标记当前的位置
  5182. File file = new File("c:/test.txt");
  5183. InputStream bis = new BufferedInputStream(new FileInputStream(file));
  5184. int count = 0;
  5185. bis.mark(50);
  5186. for (int i = 0; i < file.length(); i++) {
  5187. int read = bis.read();
  5188. if(count%5==0){
  5189. bis.reset();
  5190. System.out.println();
  5191. }
  5192. count++;
  5193. System.out.println(read);
  5194. }
  5195. bis.close();
  5196. //markSupported方法——判断是否支持mark和reset方法
  5197. File file = new File("c:/test.txt");
  5198. FileInputStream in = new FileInputStream(file);
  5199. if(in.markSupported()){
  5200. System.out.println("支持mark和reset");
  5201. }else {
  5202. System.out.println("不支持");
  5203. }
  5204. in.close();
  5205. //read方法——读取字节输入流中的数据
  5206. InputStream is = System.in;
  5207. byte[] b = new byte[50];
  5208. is.read(b);
  5209. System.out.println("控制台内容"+new String(b).trim());
  5210. is.close();
  5211. //reset方法——重新定位到最后一次mark方法时的位置
  5212. File file = new File("c:/test.txt");
  5213. FileInputStream in = new FileInputStream(file);
  5214. BufferedInputStream bis = new BufferedInputStream(in);
  5215. int count = 0;
  5216. bis.mark(50);
  5217. for (int i = 0; i < file.length(); i++) {
  5218. count++;
  5219. int read = bis.read();
  5220. if(count== 5)
  5221. bis.reset();
  5222. System.out.println(read);
  5223. }
  5224. bis.close();
  5225. //skip方法——跳过并丢弃n字节
  5226. File file = new File("c:/test.txt");
  5227. FileInputStream in = new FileInputStream(file);
  5228. in.skip(10);
  5229. int i;
  5230. while ((i = in.read())!=-1) {
  5231. System.out.println(i);
  5232. }
  5233. in.close();
  5234. //javaioOutputStream——字节输出流
  5235. //close方法——关闭字节输出流
  5236. PrintStream out = System.out;
  5237. byte[] b = "欢迎使用".getBytes();
  5238. out.write(b);
  5239. out.close();
  5240. //flush方法——刷新缓冲区
  5241. PrintStream out = System.out;
  5242. byte[] b = "欢迎使用".getBytes();
  5243. out.write(b);
  5244. out.flush();
  5245. out.close();
  5246. //write方法——向输出流写数据
  5247. PrintStream out = System.out;
  5248. byte[] b = "欢迎使用".getBytes();
  5249. out.write(b);
  5250. out.flush();
  5251. out.close();
  5252. //javaioObjectOutputStream——对象输出流
  5253. //close方法——关闭对象输出流
  5254. File file = new File("c:/test.txt");
  5255. FileOutputStream fos = new FileOutputStream(file);
  5256. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5257. oos.close();
  5258. fos.close();
  5259. //flush方法——刷新缓冲区
  5260. File file = new File("c:/test.txt");
  5261. FileOutputStream fos = new FileOutputStream(file);
  5262. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5263. oos.write(10);
  5264. oos.flush();
  5265. oos.close();
  5266. fos.close();
  5267. //writeObject方法——向输出流写入对象
  5268. public class javaTest {
  5269. public static void main(String[] args) throws Throwable {
  5270. File file = new File("c:/test.txt");
  5271. FileOutputStream fos = new FileOutputStream(file);
  5272. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5273. Student student = new Student();
  5274. student.name = "小王";
  5275. student.sex = "男";
  5276. oos.writeObject(student);
  5277. oos.flush();
  5278. oos.close();
  5279. fos.close();
  5280. }
  5281. }
  5282. class Student implements Serializable{
  5283. String name;
  5284. String sex;
  5285. }
  5286. //writeBytes方法——向输出流写入字符串
  5287. File file = new File("c:/test.txt");
  5288. FileOutputStream fos = new FileOutputStream(file);
  5289. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5290. String str = "JAVA";
  5291. oos.writeBytes(str);
  5292. oos.close();
  5293. fos.close();
  5294. //write方法——向输出流写入byte值
  5295. File file = new File("c:/test.txt");
  5296. FileOutputStream fos = new FileOutputStream(file);
  5297. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5298. byte[] b = "JAVA".getBytes();
  5299. oos.write(b);
  5300. oos.close();
  5301. fos.close();
  5302. //writeChar方法——向输出流写入char值
  5303. File file = new File("c:/test.txt");
  5304. FileOutputStream fos = new FileOutputStream(file);
  5305. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5306. char v = 'a';
  5307. oos.writeChar(v);
  5308. oos.close();
  5309. fos.close();
  5310. //writeChars方法——向输出流写入String值
  5311. File file = new File("c:/test.txt");
  5312. FileOutputStream fos = new FileOutputStream(file);
  5313. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5314. oos.writeChars("JAVA");
  5315. oos.close();
  5316. fos.close();
  5317. //writeDouble方法——向输出流写入double值
  5318. File file = new File("c:/test.txt");
  5319. FileOutputStream fos = new FileOutputStream(file);
  5320. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5321. oos.writeDouble(123.456);
  5322. oos.close();
  5323. fos.close();
  5324. //writeFields方法——将已缓冲的字段写入流中
  5325. File file = new File("c:/test.txt");
  5326. FileOutputStream fos = new FileOutputStream(file);
  5327. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5328. double d = 123.456;
  5329. oos.writeDouble(d);
  5330. oos.writeFields();
  5331. oos.close();
  5332. fos.close();
  5333. //writeFloat方法——向输出流写入float值
  5334. File file = new File("c:/test.txt");
  5335. FileOutputStream fos = new FileOutputStream(file);
  5336. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5337. oos.writeFloat(100.123f);
  5338. oos.close();
  5339. fos.close();
  5340. //writeInt方法——向输出流写入int值
  5341. File file = new File("c:/test.txt");
  5342. FileOutputStream fos = new FileOutputStream(file);
  5343. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5344. oos.writeInt(100);
  5345. oos.close();
  5346. fos.close();
  5347. //writeLong方法——向输出流写入long值
  5348. File file = new File("c:/test.txt");
  5349. FileOutputStream fos = new FileOutputStream(file);
  5350. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5351. long v = 10000;
  5352. oos.writeLong(v);
  5353. oos.close();
  5354. fos.close();
  5355. //writeShort方法——向输出流写入short值
  5356. File file = new File("c:/test.txt");
  5357. FileOutputStream fos = new FileOutputStream(file);
  5358. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5359. short v = 123;
  5360. oos.writeShort(v);
  5361. oos.close();
  5362. fos.close();
  5363. //writeUTF方法——向输出流写入float值
  5364. File file = new File("c:/test.txt");
  5365. FileOutputStream fos = new FileOutputStream(file);
  5366. ObjectOutputStream oos = new ObjectOutputStream(fos);
  5367. oos.writeUTF("aaaaa");
  5368. oos.close();
  5369. fos.close();
  5370. //javaioObjectInputStream——对象输入流
  5371. //readObject方法——从输入流读取对象
  5372. public class javaTest {
  5373. public static void main(String[] args) throws Throwable {
  5374. File file = new File("c:/test.txt");
  5375. FileInputStream fis = new FileInputStream(file);
  5376. ObjectInputStream ois = new ObjectInputStream(fis);
  5377. Student st = (Student) ois.readObject();
  5378. System.out.println(st.name);
  5379. System.out.println(st.sex);
  5380. ois.close();
  5381. fis.close();
  5382. }
  5383. }
  5384. class Student implements Serializable{
  5385. String name;
  5386. String sex;
  5387. }
  5388. //readInt方法——从输入流读取int值
  5389. File file = new File("c:/test.txt");
  5390. FileInputStream fis = new FileInputStream(file);
  5391. ObjectInputStream ois = new ObjectInputStream(fis);
  5392. System.out.println(ois.readInt());
  5393. ois.close();
  5394. fis.close();
  5395. //readFloat方法——从输入流读取float值
  5396. File file = new File("c:/test.txt");
  5397. FileInputStream fis = new FileInputStream(file);
  5398. ObjectInputStream ois = new ObjectInputStream(fis);
  5399. System.out.println(ois.readFloat());
  5400. ois.close();
  5401. fis.close();
  5402. //readchar方法——从输入流读取char值
  5403. File file = new File("c:/test.txt");
  5404. FileInputStream fis = new FileInputStream(file);
  5405. ObjectInputStream ois = new ObjectInputStream(fis);
  5406. System.out.println(ois.readChar());
  5407. ois.close();
  5408. fis.close();
  5409. //javaioRandomAccessFile——随机文件访问类
  5410. //close方法——关闭数据流
  5411. File file = new File("c:/test.txt");
  5412. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5413. r.read();
  5414. r.close();
  5415. //getChannel方法——返回此文件的FileChannel对象
  5416. File file = new File("c:/test.txt");
  5417. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5418. FileChannel channel = r.getChannel();
  5419. r.close();
  5420. //getFD方法——返回此流的不透明文件描述符对象
  5421. File file = new File("c:/test.txt");
  5422. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5423. FileDescriptor fd = r.getFD();
  5424. r.close();
  5425. //getFilePointer方法——返回文件中的当前偏移量
  5426. File file = new File("c:/test.txt");
  5427. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5428. r.seek(10);
  5429. System.out.println(r.getFilePointer());
  5430. r.close();
  5431. //length方法——返回此文件的长度
  5432. File file = new File("c:/test.txt");
  5433. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5434. System.out.println(r.length());
  5435. r.close();
  5436. //read方法——读取一个数据字节
  5437. File file = new File("c:/test.txt");
  5438. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5439. r.read();
  5440. r.close();
  5441. //readBoolean方法——从文件读取一个boolean值
  5442. File file = new File("c:/test.txt");
  5443. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5444. System.out.println(r.readBoolean());
  5445. r.close();
  5446. //readByte方法——从文件读取一个byte值
  5447. File file = new File("c:/test.txt");
  5448. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5449. System.out.println(r.readByte());
  5450. r.close();
  5451. //readInt方法——从文件读取一个int值
  5452. File file = new File("c:/test.txt");
  5453. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5454. System.out.println(r.readInt());
  5455. r.close();
  5456. //readChar方法——从文件读取一个char值
  5457. File file = new File("c:/test.txt");
  5458. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5459. System.out.println(r.readChar());
  5460. r.close();
  5461. //readDouble方法——从文件读取一个double值
  5462. File file = new File("c:/test.txt");
  5463. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5464. System.out.println(r.readDouble());
  5465. r.close();
  5466. //readFloat方法——从文件读取一个float值
  5467. File file = new File("c:/test.txt");
  5468. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5469. System.out.println(r.readFloat());
  5470. r.close();
  5471. //readFully方法——从文件读取一个字节数组
  5472. File file = new File("c:/test.txt");
  5473. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5474. byte[] b = new byte[10];
  5475. r.readFully(b);
  5476. for (int i = 0; i < b.length; i++) {
  5477. System.out.println(b[i]);
  5478. }
  5479. r.close();
  5480. //readLine方法——从此文件读取文本的下一行
  5481. File file = new File("c:/test.txt");
  5482. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5483. System.out.println(r.readLine());
  5484. r.close();
  5485. //readLong方法——从文件读取一个long值
  5486. File file = new File("c:/test.txt");
  5487. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5488. System.out.println(r.readLong());
  5489. r.close();
  5490. //readShort方法——从文件读取一个short值
  5491. File file = new File("c:/test.txt");
  5492. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5493. System.out.println(r.readShort());
  5494. r.close();
  5495. //readUnsignedByte方法——从文件读取无符号byte值
  5496. File file = new File("c:/test.txt");
  5497. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5498. System.out.println(r.readUnsignedByte());
  5499. r.close();
  5500. //readUnsignedShort方法——从文件读取无符号short值
  5501. File file = new File("c:/test.txt");
  5502. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5503. System.out.println(r.readUnsignedShort());
  5504. r.close();
  5505. //readUTF方法——从此文件读取一个UTF-字符串
  5506. File file = new File("c:/test.txt");
  5507. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5508. System.out.println(r.readUTF());
  5509. r.close();
  5510. //setLength方法——设置此文件的长度
  5511. File file = new File("c:/test.txt");
  5512. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5513. r.setLength(10);
  5514. r.close();
  5515. //seek方法——将文件指针移至指定位置
  5516. File file = new File("c:/test.txt");
  5517. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5518. r.seek(10);
  5519. r.writeChars("欢迎使用");
  5520. r.close();
  5521. //skipBytes方法——跳过并丢弃n字节
  5522. File file = new File("c:/test.txt");
  5523. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5524. r.skipBytes(3);
  5525. System.out.println(r.read());
  5526. r.close();
  5527. //write方法——向此文件写入指定的字节
  5528. File file = new File("c:/test.txt");
  5529. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5530. byte[] b = {65,66,67,68,69,70};
  5531. r.seek(r.length());
  5532. r.write(b);
  5533. r.close();
  5534. //writeBoolean方法——将boolean值写入文件
  5535. File file = new File("c:/test.txt");
  5536. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5537. r.seek(r.length());
  5538. boolean v = true;
  5539. r.writeBoolean(v);
  5540. r.close();
  5541. //writeByte方法——将byte值写入该文件
  5542. File file = new File("c:/test.txt");
  5543. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5544. r.seek(r.length());
  5545. byte v = 24;
  5546. r.writeByte(v);
  5547. r.close();
  5548. //writeBytes方法——按字节序列将该字符串写入该文件
  5549. File file = new File("c:/test.txt");
  5550. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5551. r.seek(r.length());
  5552. r.writeBytes("JAVA");
  5553. r.close();
  5554. //writeChars方法——写入作为字符数据的字符串
  5555. File file = new File("c:/test.txt");
  5556. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5557. r.seek(r.length());
  5558. r.writeChars("JAVA");
  5559. r.close();
  5560. //writeDouble方法——写入double值
  5561. File file = new File("c:/test.txt");
  5562. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5563. r.seek(r.length());
  5564. double v = 123.123d;
  5565. r.writeDouble(v);
  5566. r.close();
  5567. //writeFloat方法——向文件写入float值
  5568. File file = new File("c:/test.txt");
  5569. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5570. r.seek(r.length());
  5571. float v = 123.123f;
  5572. r.writeFloat(v);
  5573. r.close();
  5574. //writeInt方法——向文件写入int值
  5575. File file = new File("c:/test.txt");
  5576. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5577. r.seek(r.length());
  5578. int v = 123;
  5579. r.writeInt(v);
  5580. r.close();
  5581. //writeLong方法——向文件写入long值
  5582. File file = new File("c:/test.txt");
  5583. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5584. r.seek(r.length());
  5585. long v = 123123;
  5586. r.writeLong(v);
  5587. r.close();
  5588. //writeShort方法——向文件写入short值
  5589. File file = new File("c:/test.txt");
  5590. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5591. r.seek(r.length());
  5592. short v = 12;
  5593. r.writeShort(v);
  5594. r.close();
  5595. //writeUTF方法——向文件写入UTF- 编码的字符串
  5596. File file = new File("c:/test.txt");
  5597. RandomAccessFile r = new RandomAccessFile(file, "rw");
  5598. r.seek(r.length());
  5599. r.writeUTF("参考手册");
  5600. r.close();
  5601. //javaioReader——读取字符流的抽象类
  5602. //close方法——关闭流
  5603. InputStreamReader rin = new InputStreamReader(System.in);
  5604. char[] c = new char[50];
  5605. rin.read(c);
  5606. String str = new String(c);
  5607. System.out.println(str.trim());
  5608. rin.close();
  5609. //mark方法——标记流中的当前位置
  5610. File file = new File("c:/test.txt");
  5611. BufferedReader r = new BufferedReader(new FileReader(file));
  5612. int count = 0;
  5613. r.mark(10);
  5614. for (int i = 0; i < file.length(); i++) {
  5615. int read = r.read();
  5616. if(count%5 == 0){
  5617. r.reset();
  5618. System.out.println();
  5619. }
  5620. count++;
  5621. System.out.println(read);
  5622. }
  5623. r.close();
  5624. //markSupported方法——判断是否支持mark操作
  5625. File file = new File("c:/test.txt");
  5626. FileReader r = new FileReader(file);
  5627. if(r.markSupported()){
  5628. System.out.println("支持");
  5629. }else{
  5630. System.out.println("不支持");
  5631. }
  5632. r.close();
  5633. //read方法——读取流中的数据
  5634. InputStreamReader r = new InputStreamReader(System.in);
  5635. char[] c = new char[100];
  5636. r.read(c);
  5637. String str = new String(c);
  5638. System.out.println(str.trim());
  5639. r.close();
  5640. //ready方法——判断是否准备读取此流
  5641. File file = new File("c:/test.txt");
  5642. FileReader r = new FileReader(file);
  5643. if(r.ready()){
  5644. int i ;
  5645. while ((i = r.read())!=-1) {
  5646. System.out.println(i);
  5647. }
  5648. }
  5649. r.close();
  5650. //reset方法——重置该流
  5651. File file = new File("c:/test.txt");
  5652. BufferedReader r = new BufferedReader(new FileReader(file));
  5653. int count = 0;
  5654. r.mark(5);
  5655. for (int i = 0; i < file.length(); i++) {
  5656. int read = r.read();
  5657. if(count % 5 == 0){
  5658. r.reset();
  5659. System.out.println();
  5660. }
  5661. count++;
  5662. System.err.println(read);
  5663. }
  5664. r.close();
  5665. //skip方法——跳过并丢弃n字节
  5666. File file = new File("c:/test.txt");
  5667. FileReader r = new FileReader(file);
  5668. if(r.ready()){
  5669. r.skip(10);
  5670. int i;
  5671. while ((i=r.read())!=-1) {
  5672. System.out.println(i);
  5673. }
  5674. }
  5675. r.close();
  5676. //javautilScanner——简单文本扫描器
  5677. //close方法——关闭扫描器
  5678. Scanner s = new Scanner("JAVA LIKE PHP");
  5679. s.close();
  5680. //delimiter方法——返回匹配分隔符的 Pattern
  5681. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5682. s.findInLine("(\\d+) and (\\d+) and (\\w+) and (\\w+)");
  5683. MatchResult result = s.match();
  5684. for (int i = 0; i < result.groupCount(); i++) {
  5685. System.out.println(result.group(i));
  5686. }
  5687. s.close();
  5688. //findInLine方法——忽略分隔符查找下一个构造模式
  5689. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5690. s.findInLine("(\\d+) and (\\d+) and (\\w+) and (\\w+)");
  5691. MatchResult result = s.match();
  5692. for (int i = 0; i < result.groupCount(); i++) {
  5693. System.out.println(result.group(i));
  5694. }
  5695. s.close();
  5696. //findWithinHorizon方法——试图查找下一个指定模式
  5697. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5698. String str = s.findWithinHorizon("and", 7);
  5699. System.out.println(str);
  5700. s.close();
  5701. //hasNext方法——扫描器的输入中是否有另一个标记
  5702. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5703. if (s.hasNext(Pattern.compile("\\d"))) {
  5704. System.out.println("有另一个标记");
  5705. }
  5706. s.close();
  5707. //hasNextBigDecimal方法——下一个标记是否默认基数中的一个 BigDecimal
  5708. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5709. if (s.hasNextBigDecimal()) {
  5710. System.out.println(s.nextBigDecimal());
  5711. }
  5712. s.close();
  5713. //hasNextBigInteger方法——下一个标记是否默认基数中的一个BigInteger
  5714. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5715. if (s.hasNextBigInteger()) {
  5716. System.out.println(s.nextBigInteger());
  5717. }
  5718. s.close();
  5719. //hasNextBoolean方法——下一个标记是否为布尔值
  5720. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5721. if (s.hasNextBoolean()) {
  5722. System.out.println(s.nextBoolean());
  5723. }
  5724. s.close();
  5725. //hasNextByte方法——下一个标记是否为byte值
  5726. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5727. if (s.hasNextByte()) {
  5728. System.out.println(s.nextByte());
  5729. }
  5730. s.close();
  5731. //hasNextDouble方法——下一个标记是否为double值
  5732. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5733. if (s.hasNextDouble()) {
  5734. System.out.println(s.nextDouble());
  5735. }
  5736. s.close();
  5737. //hasNextFloat方法——下一个标记是否为float值
  5738. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5739. if (s.hasNextFloat()) {
  5740. System.out.println(s.nextFloat());
  5741. }
  5742. s.close();
  5743. //hasNextInt方法——下一个标记是否为int值
  5744. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5745. if (s.hasNextInt()) {
  5746. System.out.println(s.nextInt());
  5747. }
  5748. s.close();
  5749. //hasNextLine方法——是否存在下一行
  5750. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5751. if (s.hasNextLine()) {
  5752. System.out.println(s.nextLine());
  5753. }
  5754. s.close();
  5755. //hasNextLong方法——下一个标记是否为long值
  5756. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5757. if (s.hasNextLong()) {
  5758. System.out.println(s.nextLong());
  5759. }
  5760. s.close();
  5761. //hasNextShort方法——下一个标记是否为short值
  5762. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5763. if (s.hasNextShort()) {
  5764. System.out.println(s.nextShort());
  5765. }
  5766. s.close();
  5767. //ioException方法——最后抛出的 IOException
  5768. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5769. IOException ie = s.ioException();
  5770. if(ie != null){
  5771. ie.printStackTrace();
  5772. }
  5773. s.close();
  5774. //locale方法——返回此扫描器的语言环境
  5775. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5776. System.out.println(s.locale());
  5777. s.close();
  5778. //match方法——最后扫描操作的匹配结果
  5779. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5780. s.findInLine("(\\d+) and (\\d+) and (\\w+) and (\\w+)");
  5781. MatchResult result = s.match();
  5782. for (int i = 0; i < result.groupCount(); i++) {
  5783. System.out.println(result.group(i));
  5784. }
  5785. s.close();
  5786. //next方法——读取下一个完整标记
  5787. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5788. if(s.hasNext()){
  5789. System.out.println(s.next());
  5790. }
  5791. s.close();
  5792. //nextBigDecimal方法——读取下一个BigDecimal值
  5793. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5794. if(s.hasNextBigDecimal()){
  5795. System.out.println(s.nextBigDecimal());
  5796. }
  5797. s.close();
  5798. //nextBigInteger方法——读取下一个BigInteger值
  5799. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5800. if(s.hasNextBigInteger()){
  5801. System.out.println(s.nextBigInteger());
  5802. }
  5803. s.close();
  5804. //nextBoolean方法——读取下一个boolean值
  5805. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5806. if(s.hasNextBoolean()){
  5807. System.out.println(s.nextBoolean());
  5808. }
  5809. s.close();
  5810. //nextByte方法——读取下一个byte值
  5811. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5812. if(s.hasNextByte()){
  5813. System.out.println(s.nextByte());
  5814. }
  5815. s.close();
  5816. //nextDouble方法——读取下一个double值
  5817. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5818. if(s.hasNextDouble()){
  5819. System.out.println(s.nextDouble());
  5820. }
  5821. s.close();
  5822. //nextFloat方法——读取下一个float值
  5823. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5824. if(s.hasNextFloat()){
  5825. System.out.println(s.nextFloat());
  5826. }
  5827. s.close();
  5828. //nextInt方法——读取下一个int值
  5829. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5830. if(s.hasNextInt()){
  5831. System.out.println(s.nextInt());
  5832. }
  5833. s.close();
  5834. //nextShort方法——读取下一个short值
  5835. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5836. if(s.hasNextShort()){
  5837. System.out.println(s.nextShort());
  5838. }
  5839. s.close();
  5840. //nextLine方法——读取下一行文本
  5841. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5842. if(s.hasNextLine()){
  5843. System.out.println(s.nextLine());
  5844. }
  5845. s.close();
  5846. //nextLong方法——读取下一个long值
  5847. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5848. if(s.hasNextLong()){
  5849. System.out.println(s.nextLong());
  5850. }
  5851. s.close();
  5852. //nextShort方法——读取下一个short值
  5853. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5854. if(s.hasNextShort()){
  5855. System.out.println(s.nextShort());
  5856. }
  5857. s.close();
  5858. //radix方法——返回扫描器的默认基数
  5859. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5860. System.out.println(s.radix());
  5861. s.close();
  5862. //reset方法——重置扫描器
  5863. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5864. s.useRadix(4);
  5865. s.reset();
  5866. s.close();
  5867. //skip方法——忽略分隔符跳过匹配的输入信息
  5868. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5869. s.skip(Pattern.compile("\\d and"));
  5870. if(s.hasNext()){
  5871. System.out.println(s.next());
  5872. }
  5873. s.close();
  5874. //toString方法——返回扫描器的字符串表示形式
  5875. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5876. System.out.println(s.toString());
  5877. s.close();
  5878. //useDelimiter方法——设置为指定分隔模式
  5879. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5880. s.useDelimiter(Pattern.compile("\\sfish\\s"));
  5881. s.close();
  5882. //useLocale方法——设置为指定的语言环境
  5883. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5884. s.useDelimiter(Pattern.compile("\\sfish\\s"));
  5885. s.useLocale(new Locale("EN","US"));
  5886. System.out.println(s.locale());
  5887. s.close();
  5888. //useRadix方法——设置指定基数
  5889. Scanner s = new Scanner("1 and 2 and yellow and blue and");
  5890. s.useRadix(16);
  5891. System.out.println(s.radix());
  5892. s.close();
  5893. //javautilWriter——写入字符流的抽象类
  5894. //append方法——添加指定字符
  5895. PrintWriter out = new PrintWriter(System.out);
  5896. out = out.append('a');
  5897. out.write(0);
  5898. out.close();
  5899. //close方法——关闭数据流
  5900. PrintWriter out = new PrintWriter(System.out);
  5901. out = out.append('a');
  5902. out.write(0);
  5903. out.close();
  5904. //flush方法——刷新流的缓冲
  5905. PrintWriter out = new PrintWriter(System.out);
  5906. out.write("欢迎使用");
  5907. out.flush();
  5908. out.close();
  5909. //write方法——向字符输出流中写数据
  5910. PrintWriter out = new PrintWriter(System.out);
  5911. out.write("欢迎使用");
  5912. out.flush();
  5913. out.close();
  5914. <!--数据库操作 ->
  5915. //javasqlDriverManager——驱动程序管理类
  5916. //deregisterDriver方法——删除一个驱动程序
  5917. Class.forName("com.mysql.jdbc.Driver");
  5918. String url = "jdbc:mysql:///java?username=root&password=root";
  5919. Driver driver = DriverManager.getDriver(url);
  5920. DriverManager.deregisterDriver(driver);
  5921. //getDriver方法——选择一个适当的驱动程序
  5922. Class.forName("com.mysql.jdbc.Driver");
  5923. String url = "jdbc:mysql:///java?username=root&password=root";
  5924. Driver driver = DriverManager.getDriver(url);
  5925. DriverManager.deregisterDriver(driver);
  5926. //getDrivers方法——获取全部已加载的驱动程序
  5927. Class.forName("com.mysql.jdbc.Driver");
  5928. Enumeration<Driver> em = DriverManager.getDrivers();
  5929. while (em.hasMoreElements()) {
  5930. Driver d = em.nextElement();
  5931. System.out.println(d.getClass().getName());
  5932. }
  5933. //getConnection方法——获取数据库连接
  5934. Class.forName("com.mysql.jdbc.Driver");
  5935. String url = "jdbc:mysql:///java?username=root&password=root";
  5936. Connection con = DriverManager.getConnection(url);
  5937. System.out.println("连接成功");
  5938. //getLoginTimeout方法——获取连接数据库的等待时间
  5939. Class.forName("com.mysql.jdbc.Driver");
  5940. String url = "jdbc:mysql:///java?username=root&password=root";
  5941. Connection con = DriverManager.getConnection(url);
  5942. System.out.println(DriverManager.getLoginTimeout());
  5943. //getLogWriter方法——获取日志的打印输出流
  5944. Class.forName("com.mysql.jdbc.Driver");
  5945. String url = "jdbc:mysql:///java?username=root&password=root";
  5946. Connection con = DriverManager.getConnection(url);
  5947. PrintWriter out = DriverManager.getLogWriter();
  5948. if(out != null){
  5949. out.print("得到信息");
  5950. out.close();
  5951. }
  5952. //println方法——输出日志
  5953. Class.forName("com.mysql.jdbc.Driver");
  5954. String url = "jdbc:mysql:///java?username=root&password=root";
  5955. Connection con = DriverManager.getConnection(url);
  5956. DriverManager.println("获取驱动成功");
  5957. //registerDriver方法——注册给定的驱动程序
  5958. Class.forName("com.mysql.jdbc.Driver");
  5959. String url = "jdbc:mysql:///java?username=root&password=root";
  5960. Driver driver = DriverManager.getDriver(url);
  5961. DriverManager.deregisterDriver(driver);
  5962. DriverManager.registerDriver(driver);
  5963. //setLoginTimeout方法——设置连接数据库的等待时间
  5964. Class.forName("com.mysql.jdbc.Driver");
  5965. String url = "jdbc:mysql:///java?username=root&password=root";
  5966. DriverManager.setLoginTimeout(10);
  5967. //setLogWriter方法——设置日志的打印输出流
  5968. File file = new File("c:/test.txt");
  5969. PrintWriter out = new PrintWriter(file);
  5970. DriverManager.setLogWriter(out);
  5971. //javasqlConnection——数据库连接接口
  5972. //clearWarnings方法——清除警告信息
  5973. Class.forName("com.mysql.jdbc.Driver");
  5974. String url = "jdbc:mysql:///java?username=root&password=root";
  5975. Connection con = DriverManager.getConnection(url);
  5976. con.clearWarnings();
  5977. //close方法——关闭数据库连接对象
  5978. Class.forName("com.mysql.jdbc.Driver");
  5979. String url = "jdbc:mysql:///java?username=root&password=root";
  5980. Connection con = DriverManager.getConnection(url);
  5981. con.close();
  5982. //commit方法——提交事务
  5983. Class.forName("com.mysql.jdbc.Driver");
  5984. String url = "jdbc:mysql:///java?username=root&password=root";
  5985. Connection con = DriverManager.getConnection(url);
  5986. con.setAutoCommit(false);
  5987. Statement st = con.createStatement();
  5988. String sql = "insert into users(username,password)values('xiaoli','123456')";
  5989. st.execute(sql);
  5990. con.commit();
  5991. //createBlob方法——构造二进制的对象
  5992. Class.forName("com.mysql.jdbc.Driver");
  5993. String url = "jdbc:mysql:///java?username=root&password=root";
  5994. Connection con = DriverManager.getConnection(url);
  5995. con.setAutoCommit(true);
  5996. Blob blob = con.createBlob();
  5997. //createNClob方法——构造字节字符的对象
  5998. Class.forName("com.mysql.jdbc.Driver");
  5999. String url = "jdbc:mysql:///java?username=root&password=root";
  6000. Connection con = DriverManager.getConnection(url);
  6001. con.setAutoCommit(true);
  6002. NClob nclob = con.createNClob();
  6003. //createSQLXML方法——构造SQLXML对象
  6004. Class.forName("com.mysql.jdbc.Driver");
  6005. String url = "jdbc:mysql:///java?username=root&password=root";
  6006. Connection con = DriverManager.getConnection(url);
  6007. con.setAutoCommit(true);
  6008. SQLXML sqlXML = con.createSQLXML();
  6009. //createStatement方法——创建一个Statement对象
  6010. Class.forName("com.mysql.jdbc.Driver");
  6011. String url = "jdbc:mysql:///java?username=root&password=root";
  6012. Connection con = DriverManager.getConnection(url);
  6013. con.setAutoCommit(true);
  6014. Statement st = con.createStatement();
  6015. //getAutoCommit方法——获取提交模式
  6016. Class.forName("com.mysql.jdbc.Driver");
  6017. String url = "jdbc:mysql:///java?username=root&password=root";
  6018. Connection con = DriverManager.getConnection(url);
  6019. con.setAutoCommit(true);
  6020. Statement st = con.createStatement();
  6021. //getCatalog方法——获取当前目录名称
  6022. Class.forName("com.mysql.jdbc.Driver");
  6023. String url = "jdbc:mysql:///java?username=root&password=root";
  6024. Connection con = DriverManager.getConnection(url);
  6025. System.out.println(con.getCatalog());
  6026. //getClientInfo方法——获取客户端信息列表
  6027. Class.forName("com.mysql.jdbc.Driver");
  6028. String url = "jdbc:mysql:///java?username=root&password=root";
  6029. Connection con = DriverManager.getConnection(url);
  6030. Properties props = con.getClientInfo();
  6031. //getHoldability方法——获取ResultSet对象的可保存性
  6032. Class.forName("com.mysql.jdbc.Driver");
  6033. String url = "jdbc:mysql:///java?username=root&password=root";
  6034. Connection con = DriverManager.getConnection(url);
  6035. System.out.println(con.getHoldability());
  6036. //getMetaData方法——获取数据库的元数据
  6037. Class.forName("com.mysql.jdbc.Driver");
  6038. String url = "jdbc:mysql:///java?username=root&password=root";
  6039. Connection con = DriverManager.getConnection(url);
  6040. DatabaseMetaData metaData = con.getMetaData();
  6041. //getTransactionIsolation方法——获取事务隔离级别
  6042. Class.forName("com.mysql.jdbc.Driver");
  6043. String url = "jdbc:mysql:///java?username=root&password=root";
  6044. Connection con = DriverManager.getConnection(url);
  6045. System.out.println(con.getTransactionIsolation());
  6046. //getTypeMap方法——获取Map对象
  6047. Class.forName("com.mysql.jdbc.Driver");
  6048. String url = "jdbc:mysql:///java?username=root&password=root";
  6049. Connection con = DriverManager.getConnection(url);
  6050. Map<String, Class<?>> map = con.getTypeMap();
  6051. //getWarnings方法——获取调用报告的第一个警告
  6052. Class.forName("com.mysql.jdbc.Driver");
  6053. String url = "jdbc:mysql:///java?username=root&password=root";
  6054. Connection con = DriverManager.getConnection(url);
  6055. SQLWarning warning = con.getWarnings();
  6056. //isClosed方法——判断Connection对象是否被关闭
  6057. Class.forName("com.mysql.jdbc.Driver");
  6058. String url = "jdbc:mysql:///java?username=root&password=root";
  6059. Connection con = DriverManager.getConnection(url);
  6060. System.out.println(con.isClosed());
  6061. con.close();
  6062. System.out.println(con.isClosed());
  6063. //isReadOnly方法——判断Connection对象是否为只读模式
  6064. Class.forName("com.mysql.jdbc.Driver");
  6065. String url = "jdbc:mysql:///java?username=root&password=root";
  6066. Connection con = DriverManager.getConnection(url);
  6067. System.out.println(con.isReadOnly());
  6068. //isValid方法——判断连接是否有效
  6069. Class.forName("com.mysql.jdbc.Driver");
  6070. String url = "jdbc:mysql:///java?username=root&password=root";
  6071. Connection con = DriverManager.getConnection(url);
  6072. if(con.isValid(10)){
  6073. System.out.println("连接有效");
  6074. }
  6075. //nativeSQL方法——返回SQL语句的本机形式
  6076. Class.forName("com.mysql.jdbc.Driver");
  6077. String url = "jdbc:mysql:///java?username=root&password=root";
  6078. Connection con = DriverManager.getConnection(url);
  6079. String sql = "select top 10 * from users";
  6080. System.out.println(con.nativeSQL(sql));
  6081. //prepareCall方法——调用数据库存储过程
  6082. Class.forName("com.mysql.jdbc.Driver");
  6083. String url = "jdbc:mysql:///java?username=root&password=root";
  6084. Connection con = DriverManager.getConnection(url);
  6085. String sql = "{call saveuser(?,?)}";
  6086. con.prepareCall(sql);
  6087. //prepareStatement方法——创建一个预处理语句
  6088. Class.forName("com.mysql.jdbc.Driver");
  6089. String url = "jdbc:mysql:///java?username=root&password=root";
  6090. Connection con = DriverManager.getConnection(url);
  6091. String sql = "select * from users where password = 123456";
  6092. PreparedStatement ps = con.prepareStatement(sql);
  6093. //rollback方法——事务回滚
  6094. Class.forName("com.mysql.jdbc.Driver");
  6095. String url = "jdbc:mysql:///java?username=root&password=root";
  6096. Connection con = DriverManager.getConnection(url);
  6097. Statement st = con.createStatement();
  6098. con.setAutoCommit(false);
  6099. st.execute("delete from users where username = 'xiaoli'");
  6100. con.rollback();
  6101. //setAutoCommit方法——设置数据库连接的提交模式
  6102. Class.forName("com.mysql.jdbc.Driver");
  6103. String url = "jdbc:mysql:///java?username=root&password=root";
  6104. Connection con = DriverManager.getConnection(url);
  6105. Statement st = con.createStatement();
  6106. con.setAutoCommit(false);
  6107. st.execute("delete from users where username = 'xiaoli'");
  6108. con.rollback();
  6109. //setCatalog方法——设置目录名称
  6110. Class.forName("com.mysql.jdbc.Driver");
  6111. String url = "jdbc:mysql:///java?username=root&password=root";
  6112. Connection con = DriverManager.getConnection(url);
  6113. con.setCatalog("gmy");
  6114. //setClientInfo方法——设置客户端信息
  6115. Class.forName("com.mysql.jdbc.Driver");
  6116. String url = "jdbc:mysql:///java?username=root&password=root";
  6117. Connection con = DriverManager.getConnection(url);
  6118. Properties props = new Properties();
  6119. props.setProperty("ClientUser", "root");
  6120. props.setProperty("ClientHostname", "root");
  6121. con.setClientInfo(props);
  6122. //setHoldability方法——设置ResultSet对象的可保存性
  6123. Class.forName("com.mysql.jdbc.Driver");
  6124. String url = "jdbc:mysql:///java?username=root&password=root";
  6125. Connection con = DriverManager.getConnection(url);
  6126. con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
  6127. //setReadOnly方法——设置连接只读模式
  6128. Class.forName("com.mysql.jdbc.Driver");
  6129. String url = "jdbc:mysql:///java?username=root&password=root";
  6130. Connection con = DriverManager.getConnection(url);
  6131. con.setReadOnly(true);
  6132. //setSavepoint方法——创建一个未命名的保存点
  6133. Class.forName("com.mysql.jdbc.Driver");
  6134. String url = "jdbc:mysql:///java?username=root&password=root";
  6135. Connection con = DriverManager.getConnection(url);
  6136. con.setSavepoint();
  6137. //releaseSavepoint方法——移除指定的SavePoint示例
  6138. Class.forName("com.mysql.jdbc.Driver");
  6139. String url = "jdbc:mysql:///java?username=root&password=root";
  6140. Connection con = DriverManager.getConnection(url);
  6141. Savepoint sp = con.setSavepoint("spName");
  6142. con.releaseSavepoint(sp);
  6143. //setTransactionIsolation方法——设置事务隔离级别
  6144. Class.forName("com.mysql.jdbc.Driver");
  6145. String url = "jdbc:mysql:///java?username=root&password=root";
  6146. Connection con = DriverManager.getConnection(url);
  6147. int level = Connection.TRANSACTION_SERIALIZABLE;
  6148. con.setTransactionIsolation(level);
  6149. //getTransactionIsolation方法——获取事务隔离级别
  6150. Class.forName("com.mysql.jdbc.Driver");
  6151. String url = "jdbc:mysql:///java?username=root&password=root";
  6152. Connection con = DriverManager.getConnection(url);
  6153. int level = con.getTransactionIsolation();
  6154. System.out.println(level);
  6155. //javasqlStatement——执行SQL语句接口
  6156. //addBatch方法——添加批处理语句
  6157. Class.forName("com.mysql.jdbc.Driver");
  6158. String url = "jdbc:mysql:///java?username=root&password=root";
  6159. Connection con = DriverManager.getConnection(url);
  6160. String sql1 = "insert into users values('aaa','111')";
  6161. String sql2 = "insert into users values('bbb','111')";
  6162. String sql3 = "insert into users values('ccc','111')";
  6163. Statement stmt = con.createStatement();
  6164. stmt.addBatch(sql1);
  6165. stmt.addBatch(sql2);
  6166. stmt.addBatch(sql3);
  6167. stmt.executeBatch();
  6168. stmt.close();
  6169. //cancel方法——中止SQL语句
  6170. Class.forName("com.mysql.jdbc.Driver");
  6171. String url = "jdbc:mysql:///java?username=root&password=root";
  6172. Connection con = DriverManager.getConnection(url);
  6173. Statement stmt = con.createStatement();
  6174. stmt.cancel();
  6175. //clearBatch方法——清除批处理语句
  6176. Class.forName("com.mysql.jdbc.Driver");
  6177. String url = "jdbc:mysql:///java?username=root&password=root";
  6178. Connection con = DriverManager.getConnection(url);
  6179. String sql1 = "insert into users values('aaa','111')";
  6180. String sql2 = "insert into users values('bbb','111')";
  6181. Statement stmt = con.createStatement();
  6182. stmt.addBatch(sql1);
  6183. stmt.addBatch(sql2);
  6184. stmt.clearBatch();
  6185. stmt.close();
  6186. //clearWarnings方法——清除所有警告
  6187. Class.forName("com.mysql.jdbc.Driver");
  6188. String url = "jdbc:mysql:///java?username=root&password=root";
  6189. Connection con = DriverManager.getConnection(url);
  6190. Statement stmt = con.createStatement();
  6191. stmt.clearWarnings();
  6192. //execute方法——执行SQL语句
  6193. Class.forName("com.mysql.jdbc.Driver");
  6194. String url = "jdbc:mysql:///java?username=root&password=root";
  6195. Connection con = DriverManager.getConnection(url);
  6196. String sql1 = "insert into users values('ddd','111')";
  6197. Statement stmt = con.createStatement();
  6198. boolean bool = stmt.execute(sql1);
  6199. System.out.println(bool);
  6200. //executeBatch方法——执行批处理语句
  6201. Class.forName("com.mysql.jdbc.Driver");
  6202. String url = "jdbc:mysql:///java?username=root&password=root";
  6203. Connection con = DriverManager.getConnection(url);
  6204. String sql1 = "insert into users values('eee','111')";
  6205. String sql2 = "insert into users values('fff','111')";
  6206. String sql3 = "insert into users values('ggg','111')";
  6207. Statement stmt = con.createStatement();
  6208. stmt.addBatch(sql1);
  6209. stmt.addBatch(sql2);
  6210. stmt.addBatch(sql3);
  6211. stmt.executeBatch();
  6212. stmt.close();
  6213. con.close();
  6214. //executeUpdate方法——执行更新操作
  6215. Class.forName("com.mysql.jdbc.Driver");
  6216. String url = "jdbc:mysql:///java?username=root&password=root";
  6217. Connection con = DriverManager.getConnection(url);
  6218. String sql = "delete from users";
  6219. Statement stmt = con.createStatement();
  6220. stmt.addBatch(sql);
  6221. stmt.executeUpdate(sql);
  6222. stmt.close();
  6223. con.close();
  6224. //executeQuery方法——执行查询操作
  6225. Class.forName("com.mysql.jdbc.Driver");
  6226. String url = "jdbc:mysql:///java?username=root&password=root";
  6227. Connection con = DriverManager.getConnection(url);
  6228. String sql = "select * from users";
  6229. Statement stmt = con.createStatement();
  6230. ResultSet rs = stmt.executeQuery(sql);
  6231. stmt.close();
  6232. con.close();
  6233. //getConnection方法——获取数据库连接对象
  6234. Class.forName("com.mysql.jdbc.Driver");
  6235. String url = "jdbc:mysql:///java?username=root&password=root";
  6236. Connection con = DriverManager.getConnection(url);
  6237. Statement stmt = con.createStatement();
  6238. Connection conn2 = stmt.getConnection();
  6239. stmt.close();
  6240. con.close();
  6241. //getFetchDirection方法——获取从数据库表获取行的方向
  6242. Class.forName("com.mysql.jdbc.Driver");
  6243. String url = "jdbc:mysql:///java?username=root&password=root";
  6244. Connection con = DriverManager.getConnection(url);
  6245. Statement stmt = con.createStatement();
  6246. System.out.println(stmt.getFetchDirection());
  6247. stmt.close();
  6248. con.close();
  6249. //getFetchSize方法——获取结果集的行数
  6250. Class.forName("com.mysql.jdbc.Driver");
  6251. String url = "jdbc:mysql:///java?username=root&password=root";
  6252. Connection con = DriverManager.getConnection(url);
  6253. Statement stmt = con.createStatement();
  6254. System.out.println(stmt.getFetchSize());
  6255. stmt.close();
  6256. con.close();
  6257. //getGeneratedKeys方法——获取自动生成的键
  6258. Class.forName("com.mysql.jdbc.Driver");
  6259. String url = "jdbc:mysql:///java?username=root&password=root";
  6260. Connection con = DriverManager.getConnection(url);
  6261. Statement stmt = con.createStatement();
  6262. ResultSet rs = stmt.getGeneratedKeys();
  6263. stmt.close();
  6264. con.close();
  6265. //getMaxFieldSize方法——获取最大字节数
  6266. Class.forName("com.mysql.jdbc.Driver");
  6267. String url = "jdbc:mysql:///java?username=root&password=root";
  6268. Connection con = DriverManager.getConnection(url);
  6269. Statement stmt = con.createStatement();
  6270. System.out.println(stmt.getMaxFieldSize());
  6271. stmt.close();
  6272. con.close();
  6273. //getMaxRows方法——获取最大行数
  6274. Class.forName("com.mysql.jdbc.Driver");
  6275. String url = "jdbc:mysql:///java?username=root&password=root";
  6276. Connection con = DriverManager.getConnection(url);
  6277. Statement stmt = con.createStatement();
  6278. System.out.println(stmt.getMaxRows());
  6279. stmt.close();
  6280. con.close();
  6281. //getMoreResults方法——移动到Statement对象的下一个结果
  6282. Class.forName("com.mysql.jdbc.Driver");
  6283. String url = "jdbc:mysql:///java?username=root&password=root";
  6284. Connection con = DriverManager.getConnection(url);
  6285. Statement stmt = con.createStatement();
  6286. if(stmt.getMoreResults()){
  6287. System.out.println("下一个对象");
  6288. }
  6289. stmt.close();
  6290. con.close();
  6291. //getQueryTimeout方法——获取等待执行的秒数
  6292. Class.forName("com.mysql.jdbc.Driver");
  6293. String url = "jdbc:mysql:///java?username=root&password=root";
  6294. Connection con = DriverManager.getConnection(url);
  6295. Statement stmt = con.createStatement();
  6296. System.out.println(stmt.getQueryTimeout());
  6297. stmt.close();
  6298. con.close();
  6299. //getResultSet方法——获取结果集
  6300. Class.forName("com.mysql.jdbc.Driver");
  6301. String url = "jdbc:mysql:///java?username=root&password=root";
  6302. Connection con = DriverManager.getConnection(url);
  6303. Statement stmt = con.createStatement();
  6304. String sql = "select * from users";
  6305. ResultSet rs = stmt.executeQuery(sql);
  6306. while (rs.next()) {
  6307. System.out.println(rs.getString(1));
  6308. }
  6309. stmt.close();
  6310. con.close();
  6311. //getResultSetConcurrency方法——获取结果集并发性
  6312. Class.forName("com.mysql.jdbc.Driver");
  6313. String url = "jdbc:mysql:///java?username=root&password=root";
  6314. Connection con = DriverManager.getConnection(url);
  6315. Statement stmt = con.createStatement();
  6316. System.out.println(stmt.getResultSetConcurrency());
  6317. stmt.close();
  6318. con.close();
  6319. //getResultSetHoldability方法——获取结果集可保存性
  6320. Class.forName("com.mysql.jdbc.Driver");
  6321. String url = "jdbc:mysql:///java?username=root&password=root";
  6322. Connection con = DriverManager.getConnection(url);
  6323. Statement stmt = con.createStatement();
  6324. System.out.println(stmt.getResultSetHoldability());
  6325. stmt.close();
  6326. con.close();
  6327. //getResultSetType方法——获取结果集类型
  6328. Class.forName("com.mysql.jdbc.Driver");
  6329. String url = "jdbc:mysql:///java?username=root&password=root";
  6330. Connection con = DriverManager.getConnection(url);
  6331. Statement stmt = con.createStatement();
  6332. System.out.println(stmt.getResultSetType());
  6333. stmt.close();
  6334. con.close();
  6335. //getUpdateCount方法——获取更新的记录数
  6336. Class.forName("com.mysql.jdbc.Driver");
  6337. String url = "jdbc:mysql:///java?username=root&password=root";
  6338. Connection con = DriverManager.getConnection(url);
  6339. Statement stmt = con.createStatement();
  6340. System.out.println(stmt.getUpdateCount());
  6341. stmt.close();
  6342. con.close();
  6343. //getWarnings方法——获取调用报告的第一个警告
  6344. Class.forName("com.mysql.jdbc.Driver");
  6345. String url = "jdbc:mysql:///java?username=root&password=root";
  6346. Connection con = DriverManager.getConnection(url);
  6347. Statement stmt = con.createStatement();
  6348. System.out.println(stmt.getWarnings());
  6349. stmt.close();
  6350. con.close();
  6351. //setMaxRows方法——获取最大行数
  6352. Class.forName("com.mysql.jdbc.Driver");
  6353. String url = "jdbc:mysql:///java?username=root&password=root";
  6354. Connection con = DriverManager.getConnection(url);
  6355. Statement stmt = con.createStatement();
  6356. stmt.setMaxRows(20);
  6357. stmt.close();
  6358. con.close();
  6359. //getMaxRows方法——获取最大行数限制值
  6360. Class.forName("com.mysql.jdbc.Driver");
  6361. String url = "jdbc:mysql:///java?username=root&password=root";
  6362. Connection con = DriverManager.getConnection(url);
  6363. Statement stmt = con.createStatement();
  6364. System.out.println(stmt.getMaxRows());
  6365. stmt.close();
  6366. con.close();
  6367. //close方法——关闭Statement对象
  6368. Class.forName("com.mysql.jdbc.Driver");
  6369. String url = "jdbc:mysql:///java?username=root&password=root";
  6370. Connection con = DriverManager.getConnection(url);
  6371. Statement stmt = con.createStatement();
  6372. stmt.close();
  6373. con.close();
  6374. //isClosed方法——判断Statement对象是否关闭
  6375. Class.forName("com.mysql.jdbc.Driver");
  6376. String url = "jdbc:mysql:///java?username=root&password=root";
  6377. Connection con = DriverManager.getConnection(url);
  6378. Statement stmt = con.createStatement();
  6379. System.out.println(stmt.isClosed());
  6380. stmt.close();
  6381. System.out.println(stmt.isClosed());
  6382. con.close();
  6383. //isPoolable方法——判断对象是否可池化
  6384. Class.forName("com.mysql.jdbc.Driver");
  6385. String url = "jdbc:mysql:///java?username=root&password=root";
  6386. Connection con = DriverManager.getConnection(url);
  6387. Statement stmt = con.createStatement();
  6388. System.out.println(stmt.isPoolable());
  6389. stmt.close();
  6390. con.close();
  6391. //setCursorName方法——设置指针名称
  6392. Class.forName("com.mysql.jdbc.Driver");
  6393. String url = "jdbc:mysql:///java?username=root&password=root";
  6394. Connection con = DriverManager.getConnection(url);
  6395. Statement stmt = con.createStatement();
  6396. stmt.setCursorName("pname");
  6397. stmt.close();
  6398. con.close();
  6399. //setEscapeProcessing方法——设置是否进行转义处理
  6400. Class.forName("com.mysql.jdbc.Driver");
  6401. String url = "jdbc:mysql:///java?username=root&password=root";
  6402. Connection con = DriverManager.getConnection(url);
  6403. Statement stmt = con.createStatement();
  6404. stmt.setEscapeProcessing(true);
  6405. stmt.close();
  6406. con.close();
  6407. //setFetchDirection方法——设置处理行的方向
  6408. Class.forName("com.mysql.jdbc.Driver");
  6409. String url = "jdbc:mysql:///java?username=root&password=root";
  6410. Connection con = DriverManager.getConnection(url);
  6411. Statement stmt = con.createStatement();
  6412. stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
  6413. stmt.close();
  6414. con.close();
  6415. //setFetchSize方法——设置结果集合的最大行数
  6416. Class.forName("com.mysql.jdbc.Driver");
  6417. String url = "jdbc:mysql:///java?username=root&password=root";
  6418. Connection con = DriverManager.getConnection(url);
  6419. Statement stmt = con.createStatement();
  6420. stmt.setFetchSize(50);
  6421. stmt.close();
  6422. con.close();
  6423. //setMaxFieldSize方法——设置可返回的最大字节数
  6424. Class.forName("com.mysql.jdbc.Driver");
  6425. String url = "jdbc:mysql:///java?username=root&password=root";
  6426. Connection con = DriverManager.getConnection(url);
  6427. Statement stmt = con.createStatement();
  6428. stmt.setMaxFieldSize(1000);
  6429. stmt.close();
  6430. con.close();
  6431. //setMaxRows方法——设置最大行数
  6432. Class.forName("com.mysql.jdbc.Driver");
  6433. String url = "jdbc:mysql:///java?username=root&password=root";
  6434. Connection con = DriverManager.getConnection(url);
  6435. Statement stmt = con.createStatement();
  6436. stmt.setMaxRows(20);
  6437. stmt.close();
  6438. con.close();
  6439. //setPoolable方法——设置语句是否可池化
  6440. Class.forName("com.mysql.jdbc.Driver");
  6441. String url = "jdbc:mysql:///java?username=root&password=root";
  6442. Connection con = DriverManager.getConnection(url);
  6443. Statement stmt = con.createStatement();
  6444. stmt.setPoolable(true);
  6445. stmt.close();
  6446. con.close();
  6447. //setQueryTimeout方法——设置等待执行的秒数
  6448. Class.forName("com.mysql.jdbc.Driver");
  6449. String url = "jdbc:mysql:///java?username=root&password=root";
  6450. Connection con = DriverManager.getConnection(url);
  6451. Statement stmt = con.createStatement();
  6452. stmt.setQueryTimeout(5);
  6453. stmt.close();
  6454. con.close();
  6455. //javasqlPreparedStatement——预编译SQL语句接口
  6456. //clearParameters方法——立即清除当前参数值
  6457. Class.forName("com.mysql.jdbc.Driver");
  6458. String url = "jdbc:mysql:///java?username=root&password=root";
  6459. Connection con = DriverManager.getConnection(url);
  6460. String sql = "update users set username = ? where username= ? ";
  6461. PreparedStatement ps = con.prepareStatement(sql);
  6462. ps.setString(1, "ccc");
  6463. ps.setString(2, "aaa");
  6464. ps.clearParameters();
  6465. ps.executeUpdate();
  6466. con.close();
  6467. //execute方法——执行SQL语句
  6468. Class.forName("com.mysql.jdbc.Driver");
  6469. String url = "jdbc:mysql:///java?username=root&password=root";
  6470. Connection con = DriverManager.getConnection(url);
  6471. String sql = "select * from users";
  6472. PreparedStatement ps = con.prepareStatement(sql);
  6473. boolean bool = ps.execute();
  6474. System.out.println(bool);
  6475. con.close();
  6476. //executeQuery方法——执行SQL查询语句
  6477. Class.forName("com.mysql.jdbc.Driver");
  6478. String url = "jdbc:mysql:///java?username=root&password=root";
  6479. Connection con = DriverManager.getConnection(url);
  6480. String sql = "select * from users";
  6481. PreparedStatement ps = con.prepareStatement(sql);
  6482. ResultSet rs = ps.executeQuery();
  6483. con.close();
  6484. //executeUpdate方法——执行更新语句
  6485. Class.forName("com.mysql.jdbc.Driver");
  6486. String url = "jdbc:mysql:///java?username=root&password=root";
  6487. Connection con = DriverManager.getConnection(url);
  6488. String sql = "insert into users values(?,?)";
  6489. PreparedStatement ps = con.prepareStatement(sql);
  6490. ps.setString(1, "ddd");
  6491. ps.setString(2, "1111");
  6492. int i = ps.executeUpdate();
  6493. System.out.println(i);
  6494. con.close();
  6495. //getMetaData方法——获取ResultSet对象列的信息
  6496. Class.forName("com.mysql.jdbc.Driver");
  6497. String url = "jdbc:mysql:///java?username=root&password=root";
  6498. Connection con = DriverManager.getConnection(url);
  6499. String sql = "insert into users values(?,?)";
  6500. PreparedStatement ps = con.prepareStatement(sql);
  6501. ResultSetMetaData rsmd = ps.getMetaData();
  6502. System.out.println(rsmd.getColumnName(0));
  6503. con.close();
  6504. //getParameterMetaData方法——获取参数信息
  6505. Class.forName("com.mysql.jdbc.Driver");
  6506. String url = "jdbc:mysql:///java?username=root&password=root";
  6507. Connection con = DriverManager.getConnection(url);
  6508. String sql = "insert into users values(?,?)";
  6509. PreparedStatement ps = con.prepareStatement(sql);
  6510. ParameterMetaData pmd = ps.getParameterMetaData();
  6511. con.close();
  6512. //setAsciiStream方法——将指定参数设置为给定输入流
  6513. Class.forName("com.mysql.jdbc.Driver");
  6514. String url = "jdbc:mysql:///java?username=root&password=root";
  6515. Connection con = DriverManager.getConnection(url);
  6516. FileInputStream is1 = new FileInputStream(new File("c:/1.txt"));
  6517. FileInputStream is2 = new FileInputStream(new File("c:/2.txt"));
  6518. String sql = "insert into users values(?,?)";
  6519. PreparedStatement ps = con.prepareStatement(sql);
  6520. ps.setAsciiStream(1, is1);
  6521. ps.setAsciiStream(2, is2);
  6522. ps.executeUpdate();
  6523. ps.close();
  6524. con.close();
  6525. //setBigDecimal方法——设置大数字参数值
  6526. //设置大数字参数值
  6527. //setBinaryStream方法——设置二进制参数值
  6528. //设置二进制参数值
  6529. //setBlob方法——设置Blob型参数
  6530. //设置Blob型参数
  6531. //setBoolean方法——设置布尔型参数
  6532. //设置布尔型参数
  6533. //setByte方法——设置字节型参数
  6534. //设置字节型参数
  6535. //setBytes方法——设置字节数组型参数
  6536. //设置字节数组型参数
  6537. //setCharacterStream方法——将指定参数设置为给定Reader对象
  6538. //将指定参数设置为给定Reader对象
  6539. //setClob方法——设置Clob型参数
  6540. //设置Clob型参数
  6541. //setDate方法——设置日期型参数
  6542. //设置日期型参数
  6543. //setDouble方法——设置双精度型参数
  6544. //设置双精度型参数
  6545. //setFloat方法——设置单精度型参数
  6546. //设置单精度型参数
  6547. //setInt方法——设置整型参数
  6548. //设置整型参数
  6549. //setLong方法——设置长整型参数
  6550. //设置长整型参数
  6551. //setNCharacterStream方法——将指定参数设置为Reader对象
  6552. //将指定参数设置为Reader对象
  6553. //setNClob方法——设置NClob类型的参数
  6554. //设置NClob类型的参数
  6555. //setNString方法——设置String类型的参数
  6556. //设置String类型的参数
  6557. //setShort方法——设置Short类型的参数
  6558. //设置Short类型的参数
  6559. //setString方法——设置String类型的参数
  6560. //设置String类型的参数
  6561. //setTime方法——设置时间类型的参数
  6562. //设置时间类型的参数
  6563. //setTimestamp方法——设置参数值为时间戳
  6564. //设置参数值为时间戳
  6565. //setURL方法——设置参数为URL
  6566. //设置参数为URL
  6567. //javasqlResultSet——结果集接口
  6568. //absolute方法——将光标移动到指定行
  6569. Class.forName("com.mysql.jdbc.Driver");
  6570. String url = "jdbc:mysql:///java?username=root&password=root";
  6571. Connection con = DriverManager.getConnection(url);
  6572. String sql = "select * from users";
  6573. Statement st = con.createStatement();
  6574. ResultSet rs = st.executeQuery(sql);
  6575. boolean b = rs.absolute(5);
  6576. System.out.println(b);
  6577. con.close();
  6578. //afterLast方法——将光标移动到最后一行之后
  6579. Class.forName("com.mysql.jdbc.Driver");
  6580. String url = "jdbc:mysql:///java?username=root&password=root";
  6581. Connection con = DriverManager.getConnection(url);
  6582. String sql = "select * from users";
  6583. Statement st = con.createStatement();
  6584. ResultSet rs = st.executeQuery(sql);
  6585. rs.afterLast();
  6586. con.close();
  6587. //beforeFirst方法——将光标移动到第一行之前
  6588. Class.forName("com.mysql.jdbc.Driver");
  6589. String url = "jdbc:mysql:///java?username=root&password=root";
  6590. Connection con = DriverManager.getConnection(url);
  6591. String sql = "select * from users";
  6592. Statement st = con.createStatement();
  6593. ResultSet rs = st.executeQuery(sql);
  6594. rs.beforeFirst();
  6595. con.close();
  6596. //cancelRowUpdates方法——取消对当前行所做的更新
  6597. Class.forName("com.mysql.jdbc.Driver");
  6598. String url = "jdbc:mysql:///java?username=root&password=root";
  6599. Connection con = DriverManager.getConnection(url);
  6600. String sql = "select * from users";
  6601. PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  6602. ResultSet rs = ps.executeQuery();
  6603. if(rs.next()){
  6604. if(rs.absolute(5)){
  6605. rs.updateString("username", "abc");
  6606. rs.cancelRowUpdates();
  6607. rs.updateRow();
  6608. }
  6609. }
  6610. rs.close();
  6611. con.close();
  6612. //clearWarnings方法——清除所有警告
  6613. Class.forName("com.mysql.jdbc.Driver");
  6614. String url = "jdbc:mysql:///java?username=root&password=root";
  6615. Connection con = DriverManager.getConnection(url);
  6616. String sql = "select * from users";
  6617. PreparedStatement ps = con.prepareStatement(sql);
  6618. ResultSet rs = ps.executeQuery();
  6619. rs.clearWarnings();
  6620. rs.close();
  6621. con.close();
  6622. //close方法——关闭ResultSet对象
  6623. Class.forName("com.mysql.jdbc.Driver");
  6624. String url = "jdbc:mysql:///java?username=root&password=root";
  6625. Connection con = DriverManager.getConnection(url);
  6626. String sql = "select * from users";
  6627. PreparedStatement ps = con.prepareStatement(sql);
  6628. rs.close();
  6629. con.close();
  6630. //deleteRow方法——删除当前行
  6631. Class.forName("com.mysql.jdbc.Driver");
  6632. String url = "jdbc:mysql:///java?username=root&password=root";
  6633. Connection con = DriverManager.getConnection(url);
  6634. String sql = "select * from users";
  6635. PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  6636. ResultSet rs = ps.executeQuery();
  6637. if(rs.next()){
  6638. rs.deleteRow();
  6639. }
  6640. rs.close();
  6641. con.close();
  6642. //findColumn方法——获取ResultSet的列索引
  6643. Class.forName("com.mysql.jdbc.Driver");
  6644. String url = "jdbc:mysql:///java?username=root&password=root";
  6645. Connection con = DriverManager.getConnection(url);
  6646. String sql = "select * from users";
  6647. PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  6648. ResultSet rs = ps.executeQuery();
  6649. if(rs.next()){
  6650. int i = rs.findColumn("username");
  6651. System.out.println(i);
  6652. }
  6653. rs.close();
  6654. con.close();
  6655. //first方法——将光标移动到第一行
  6656. Class.forName("com.mysql.jdbc.Driver");
  6657. String url = "jdbc:mysql:///java?username=root&password=root";
  6658. Connection con = DriverManager.getConnection(url);
  6659. String sql = "select * from users";
  6660. Statement st = con.createStatement();
  6661. ResultSet rs = st.executeQuery(sql);
  6662. boolean bool = rs.first();
  6663. System.out.println(bool);
  6664. rs.close();
  6665. con.close();
  6666. //getAsciiStream方法——获取指定列的ASCII字符流的值
  6667. //获取指定列的ASCII字符流的值
  6668. //getBigDecimal方法——获取BigDecimal型的数据
  6669. //获取BigDecimal型的数据
  6670. //getBinaryStream方法——获取字节流型的数据
  6671. //获取字节流型的数据
  6672. //getBlob方法——获取Blob型的数据
  6673. //获取Blob型的数据
  6674. //getBoolean方法——获取布尔型的数据
  6675. //获取布尔型的数据
  6676. //getByte方法——获取字节型的数据
  6677. //获取字节型的数据
  6678. //getBytes方法——获取字节数组的数据
  6679. //获取字节数组的数据
  6680. //getCharacterStream方法——获取Reader型的数据
  6681. //获取Reader型的数据
  6682. //getClob方法——获取Clob型的数据
  6683. //获取Clob型的数据
  6684. //getConcurrency方法——获取并发模式
  6685. //获取并发模式
  6686. //getCursorName方法——获取SQL光标的名称
  6687. //获取SQL光标的名称
  6688. //getDate方法——获取日期型的数据
  6689. //获取日期型的数据
  6690. //getDouble方法——获取双精度型的数据
  6691. //获取双精度型的数据
  6692. //getFetchDirection方法——获取方向
  6693. //获取方向
  6694. //getFetchSize方法——获取ResultSet对象的当前获取大小
  6695. //获取ResultSet对象的当前获取大小
  6696. //getFloat方法——获取单精度型的数据
  6697. //获取单精度型的数据
  6698. //getHoldability方法——获取可保存性
  6699. //获取可保存性
  6700. //getInt方法——获取整型数据
  6701. //获取整型数据
  6702. //getLong方法——获取长整型数据
  6703. //获取长整型数据
  6704. //getMetaData方法——获取对象的列信息
  6705. //获取对象的列信息
  6706. //getNCharacterStream方法——获取Reader形式的数据
  6707. //获取Reader形式的数据
  6708. //getNClob方法——获取NClob型的数据
  6709. //获取NClob型的数据
  6710. //getNString方法——获取String型的数据
  6711. //获取String型的数据
  6712. //getObject方法——获取object型的数据
  6713. //获取object型的数据
  6714. //getRow方法——获取当前行编号
  6715. //获取当前行编号
  6716. //getShort方法——获取短整型的数据
  6717. //获取短整型的数据
  6718. //getStatement方法——获取Statement对象
  6719. //获取Statement对象
  6720. //getString方法——获取String类型的数据
  6721. //获取String类型的数据
  6722. //getTime方法——获取时间型的数据
  6723. //获取时间型的数据
  6724. //getTimestamp方法——获取时间戳
  6725. //获取时间戳
  6726. //getType方法——获取ResultSet对象的类型
  6727. //获取ResultSet对象的类型
  6728. //getURL方法——获取URL对象形式的数据
  6729. //获取URL对象形式的数据
  6730. //getWarnings方法——获取报告的第一个警告
  6731. //获取报告的第一个警告
  6732. //insertRow方法——插入当前行到数据库中
  6733. Class.forName("com.mysql.jdbc.Driver");
  6734. String url = "jdbc:mysql:///java?username=root&password=root";
  6735. Connection con = DriverManager.getConnection(url);
  6736. String sql = "select * from users";
  6737. PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  6738. ResultSet rs = ps.executeQuery();
  6739. rs.moveToInsertRow();
  6740. rs.updateString("username", "fffff");
  6741. rs.updateString("password", "11111");
  6742. rs.insertRow();
  6743. rs.close();
  6744. con.close();
  6745. //isAfterLast方法——判断光标是否位于最后一行之后
  6746. Class.forName("com.mysql.jdbc.Driver");
  6747. String url = "jdbc:mysql:///java?username=root&password=root";
  6748. Connection con = DriverManager.getConnection(url);
  6749. String sql = "select * from users";
  6750. Statement st = con.createStatement();
  6751. ResultSet rs = st.executeQuery(sql);
  6752. System.out.println(rs.isAfterLast());
  6753. rs.afterLast();
  6754. System.out.println(rs.isAfterLast());
  6755. rs.close();
  6756. con.close();
  6757. //isBeforeFirst方法——判断光标是否位于第一行之前
  6758. Class.forName("com.mysql.jdbc.Driver");
  6759. String url = "jdbc:mysql:///java?username=root&password=root";
  6760. Connection con = DriverManager.getConnection(url);
  6761. String sql = "select * from users";
  6762. Statement st = con.createStatement();
  6763. ResultSet rs = st.executeQuery(sql);
  6764. System.out.println(rs.isBeforeFirst());
  6765. rs.next();
  6766. System.out.println(rs.isAfterLast());
  6767. rs.close();
  6768. con.close();
  6769. //isClosed方法——判断ResultSet对象是否已经关闭
  6770. Class.forName("com.mysql.jdbc.Driver");
  6771. String url = "jdbc:mysql:///java?username=root&password=root";
  6772. Connection con = DriverManager.getConnection(url);
  6773. String sql = "select * from users";
  6774. Statement st = con.createStatement();
  6775. ResultSet rs = st.executeQuery(sql);
  6776. System.out.println(rs.isClosed());
  6777. rs.close();
  6778. System.out.println(rs.isClosed());
  6779. con.close();
  6780. //isFirst方法——判断光标是否位于第一行
  6781. Class.forName("com.mysql.jdbc.Driver");
  6782. String url = "jdbc:mysql:///java?username=root&password=root";
  6783. Connection con = DriverManager.getConnection(url);
  6784. String sql = "select * from users";
  6785. Statement st = con.createStatement();
  6786. ResultSet rs = st.executeQuery(sql);
  6787. System.out.println(rs.isFirst());
  6788. rs.first();
  6789. System.out.println(rs.isFirst());
  6790. con.close();
  6791. //isLast方法——判断光标是否位于最后一行
  6792. Class.forName("com.mysql.jdbc.Driver");
  6793. String url = "jdbc:mysql:///java?username=root&password=root";
  6794. Connection con = DriverManager.getConnection(url);
  6795. String sql = "select * from users";
  6796. Statement st = con.createStatement();
  6797. ResultSet rs = st.executeQuery(sql);
  6798. System.out.println(rs.isLast());
  6799. rs.last();
  6800. System.out.println(rs.isLast());
  6801. con.close();
  6802. //last方法——将光标移动到最后一行
  6803. Class.forName("com.mysql.jdbc.Driver");
  6804. String url = "jdbc:mysql:///java?username=root&password=root";
  6805. Connection con = DriverManager.getConnection(url);
  6806. String sql = "select * from users";
  6807. Statement st = con.createStatement();
  6808. ResultSet rs = st.executeQuery(sql);
  6809. rs.last();
  6810. con.close();
  6811. //moveToCurrentRow方法——将光标移动到记住的光标位置
  6812. Class.forName("com.mysql.jdbc.Driver");
  6813. String url = "jdbc:mysql:///java?username=root&password=root";
  6814. Connection con = DriverManager.getConnection(url);
  6815. String sql = "select * from users";
  6816. PreparedStatement ps = con.prepareStatement(sql);
  6817. ResultSet rs = ps.executeQuery();
  6818. rs.next();
  6819. rs.moveToCurrentRow();
  6820. ps.close();
  6821. rs.close();
  6822. con.close();
  6823. //moveToInsertRow方法——将光标移动到插入行
  6824. Class.forName("com.mysql.jdbc.Driver");
  6825. String url = "jdbc:mysql:///java?username=root&password=root";
  6826. Connection con = DriverManager.getConnection(url);
  6827. String sql = "select * from users";
  6828. PreparedStatement ps = con.prepareStatement(sql);
  6829. ResultSet rs = ps.executeQuery();
  6830. rs.next();
  6831. rs.moveToInsertRow();
  6832. rs.updateString("username", "dddd");
  6833. rs.updateString("passsword", "11111");
  6834. ps.close();
  6835. rs.close();
  6836. con.close();
  6837. //next方法——将光标从当前的位置向下移动一行
  6838. Class.forName("com.mysql.jdbc.Driver");
  6839. String url = "jdbc:mysql:///java?username=root&password=root";
  6840. Connection con = DriverManager.getConnection(url);
  6841. String sql = "select * from users";
  6842. Statement st = con.createStatement();
  6843. ResultSet rs = st.executeQuery(sql);
  6844. while (rs.next()) {
  6845. System.out.println(rs.getString("username"));
  6846. }
  6847. rs.close();
  6848. st.close();
  6849. con.close();
  6850. //previous方法——光标移动到上一行
  6851. Class.forName("com.mysql.jdbc.Driver");
  6852. String url = "jdbc:mysql:///java?username=root&password=root";
  6853. Connection con = DriverManager.getConnection(url);
  6854. String sql = "select * from users";
  6855. Statement st = con.createStatement();
  6856. ResultSet rs = st.executeQuery(sql);
  6857. System.out.println(rs.previous());
  6858. rs.close();
  6859. st.close();
  6860. con.close();
  6861. //relative方法——按相对行数移动光标
  6862. Class.forName("com.mysql.jdbc.Driver");
  6863. String url = "jdbc:mysql:///java?username=root&password=root";
  6864. Connection con = DriverManager.getConnection(url);
  6865. String sql = "select * from users";
  6866. Statement st = con.createStatement();
  6867. ResultSet rs = st.executeQuery(sql);
  6868. rs.relative(2);
  6869. System.out.println(rs.getRow());
  6870. rs.close();
  6871. st.close();
  6872. con.close();
  6873. //setFetchDirection方法——设置行的处理方向
  6874. Class.forName("com.mysql.jdbc.Driver");
  6875. String url = "jdbc:mysql:///java?username=root&password=root";
  6876. Connection con = DriverManager.getConnection(url);
  6877. String sql = "select * from users";
  6878. PreparedStatement ps = con.prepareStatement(sql);
  6879. ResultSet rs = ps.executeQuery();
  6880. rs.setFetchDirection(ResultSet.FETCH_FORWARD);
  6881. rs.close();
  6882. ps.close();
  6883. con.close();
  6884. //setFetchSize方法——设置结果集的最大行数
  6885. Class.forName("com.mysql.jdbc.Driver");
  6886. String url = "jdbc:mysql:///java?username=root&password=root";
  6887. Connection con = DriverManager.getConnection(url);
  6888. String sql = "select * from users";
  6889. PreparedStatement ps = con.prepareStatement(sql);
  6890. ResultSet rs = ps.executeQuery();
  6891. rs.setFetchSize(10);
  6892. rs.close();
  6893. ps.close();
  6894. con.close();
  6895. //updateBigDecimal方法——用BigDecimal值更新指定列
  6896. //用BigDecimal值更新指定列
  6897. //updateBoolean方法——用boolean值更新指定列
  6898. //用boolean值更新指定列
  6899. //updateByte方法——用byte值更新指定列
  6900. //用byte值更新指定列
  6901. //updateBytes方法——用byte数组值更新指定列
  6902. //用byte数组值更新指定列
  6903. //updateDate方法——用Date值更新指定列
  6904. //用Date值更新指定列
  6905. //updateDouble方法——用double值更新指定列
  6906. //用double值更新指定列
  6907. //updateFloat方法——用float值更新指定列
  6908. //用float值更新指定列
  6909. //updateInt方法——用int值更新指定列
  6910. //用int值更新指定列
  6911. //updateLong方法——用long值更新指定列
  6912. //用long值更新指定列
  6913. //updateNString方法——用String值更新指定列
  6914. //用String值更新指定列
  6915. //updateNull方法——用null值更新指定列
  6916. //用null值更新指定列
  6917. //updateRow方法——更新当前行
  6918. //更新当前行
  6919. //updateShort方法——用short值更新指定列
  6920. //用short值更新指定列
  6921. //updateString方法——用String值更新指定列
  6922. //用String值更新指定列
  6923. //updateTime方法——用Time值更新指定列
  6924. //用Time值更新指定列
  6925. //updateTimestamp方法——用时间戳更新指定列
  6926. //用时间戳更新指定列
  6927. <!--安全与加密 ->
  6928. //javasecurity、Key接口
  6929. //getAlgorithm方法——返回当前密钥的算法名称
  6930. KeyGenerator generator = KeyGenerator.getInstance("DES");
  6931. SecretKey key = generator.generateKey();
  6932. String algorithm = key.getAlgorithm();
  6933. System.out.println(algorithm);
  6934. //getEncoded方法——返回当前密钥
  6935. KeyGenerator generator = KeyGenerator.getInstance("DES");
  6936. SecretKey key = generator.generateKey();
  6937. byte[] encoded = key.getEncoded();
  6938. //getFormat方法——返回密钥的基本编码格式
  6939. KeyGenerator generator = KeyGenerator.getInstance("DES");
  6940. SecretKey key = generator.generateKey();
  6941. String format = key.getFormat();
  6942. System.out.println(format);
  6943. //javasecurityKeyPair类
  6944. //getPrivate方法——返回对密钥对的私钥组件的引用
  6945. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  6946. KeyPair keyPair = keyPairGen.generateKeyPair();
  6947. PrivateKey key = keyPair.getPrivate();
  6948. System.out.println(key);
  6949. //getPublic方法——返回对密钥对的公钥组件的引用
  6950. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  6951. KeyPair keyPair = keyPairGen.generateKeyPair();
  6952. PublicKey key = keyPair.getPublic();
  6953. System.out.println(key);
  6954. //javasecurityKeyPairGenerator类
  6955. //generateKeyPair方法——生成一个密钥对
  6956. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  6957. KeyPair keyPair = keyPairGen.generateKeyPair();
  6958. //getAlgorithm方法——返回密钥对生成器算法的标准名称
  6959. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  6960. String algorithm = keyPairGen.getAlgorithm();
  6961. System.out.println(algorithm);
  6962. //getInstance方法——返回生成指定算法密钥对的KeyPairGenerator对象
  6963. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  6964. //initialize方法——初始化密钥对生成器
  6965. DHPublicKey dhPublicKey = null;
  6966. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH");
  6967. keyPairGen.initialize(dhPublicKey.getParams());
  6968. //javasecuritySignature类
  6969. //getAlgorithm方法——返回签名对象的算法名称
  6970. Signature signature = Signature.getInstance("MD5withRSA");
  6971. String algorithm = signature.getAlgorithm();
  6972. System.out.println(algorithm);
  6973. //getInstance方法——返回实现指定签名算法的Signature对象
  6974. Signature signature = Signature.getInstance("MD5withRSA");
  6975. //initSign方法——初始化当前签名的对象
  6976. Signature signature = Signature.getInstance("MD5withRSA");
  6977. PrivateKey privateKey = null;
  6978. signature.initSign(privateKey);
  6979. //initVerify方法——初始化签名验证
  6980. Signature signature = Signature.getInstance("MD5withRSA");
  6981. PrivateKey privateKey = null;
  6982. signature.initVerify(privateKey);
  6983. //sign方法——返回已经更新数据的签名
  6984. byte[] data = null;
  6985. PrivateKey privateKey = null;
  6986. Signature signature = Signature.getInstance("MD5withRSA");
  6987. signature.initSign(privateKey);
  6988. signature.update(data);
  6989. byte[] sign = signature.sign();
  6990. //update方法——更新要签名或验证的数据
  6991. byte[] data = null;
  6992. PrivateKey privateKey = null;
  6993. Signature signature = Signature.getInstance("MD5withRSA");
  6994. signature.initSign(privateKey);
  6995. signature.update(data);
  6996. //verify方法——验证签名是否有效
  6997. PublicKey publicKey = null;
  6998. Signature signature = Signature.getInstance("MD5withRSA");
  6999. signature.initVerify(publicKey);
  7000. //javasecuritySecureRandom类
  7001. //SecureRandom方法——构造实现默认随机数算法的安全随机数生成器
  7002. SecureRandom secureRandom = new SecureRandom();
  7003. //getSeed方法——返回随机数生成器的种子字节数量
  7004. SecureRandom secureRandom = new SecureRandom();
  7005. byte[] seed = secureRandom.getSeed(5);
  7006. for (int i = 0; i < seed.length; i++) {
  7007. System.out.println(seed[i]);
  7008. }
  7009. //nextBytes方法——为字节数组生成随机数
  7010. SecureRandom secureRandom = new SecureRandom();
  7011. byte[] by = new byte[5];
  7012. secureRandom.nextBytes(by);
  7013. for (int i = 0; i < by.length; i++) {
  7014. System.out.println(by[i]);
  7015. }
  7016. //setSeed方法——重新设置随机对象的种子
  7017. SecureRandom secureRandom = new SecureRandom();
  7018. byte[] by = secureRandom.getSeed(5);
  7019. secureRandom.setSeed(by);
  7020. //javaxcryptoCipher类
  7021. //doFinal方法——结束加密或解密的操作
  7022. //结束加密或解密的操作
  7023. //getAlgorithm方法——返回当前Cipher对象的算法名称
  7024. Cipher cipher = Cipher.getInstance("DES");
  7025. String algorithm = cipher.getAlgorithm();
  7026. System.out.println(algorithm);
  7027. //getBlockSize方法——返回块的大小
  7028. Cipher cipher = Cipher.getInstance("DES");
  7029. int blockSize = cipher.getBlockSize();
  7030. System.out.println(blockSize);
  7031. //getInstance方法——返回实现指定转换的Cipher对象
  7032. Cipher cipher = Cipher.getInstance("DES");
  7033. //init方法——用密钥初始化当前Cipher对象
  7034. Key key = null;
  7035. Cipher cipher = Cipher.getInstance("DES");
  7036. cipher.init(Cipher.DECRYPT_MODE, key);
  7037. //update方法——继续多部分加密或解密操作
  7038. //继续多部分加密或解密操作
  7039. //javaxcryptoSecretKeyFactory类
  7040. //generateSecret方法——根据密钥规范生成密钥
  7041. //根据密钥规范生成密钥
  7042. //getAlgorithm方法——返回当前SecretKeyFactory对象的算法名称
  7043. //返回当前SecretKeyFactory对象的算法名称
  7044. //getInstance方法——返回转换指定算法秘密密钥的SecretKeyFactory对象
  7045. SecretKeyFactory factory = SecretKeyFactory.getInstance("des");
  7046. Provider provider = factory.getProvider();
  7047. System.out.println(provider.getInfo());
  7048. //getProvider方法——返回当前SecretKeyFactory对象的提供者
  7049. SecretKeyFactory factory = SecretKeyFactory.getInstance("des");
  7050. Provider provider = factory.getProvider();
  7051. System.out.println(provider.getInfo());
  7052. <!--网络传输 ->
  7053. //javanetInetAddress类
  7054. //getByName方法——根据主机名获取InetAddress对象
  7055. InetAddress address = InetAddress.getByName("hello");
  7056. System.out.println(address);
  7057. //getHostAddress方法——获取IP地址
  7058. InetAddress address = InetAddress.getByName("hello");
  7059. String ip = address.getHostAddress();
  7060. System.out.println(ip);
  7061. //getHostName方法——获取主机名称
  7062. InetAddress address = InetAddress.getLocalHost();
  7063. String name = address.getHostName();
  7064. System.out.println(name);
  7065. //getLocalHost方法——返回本地主机的InetAddress对象
  7066. InetAddress address = InetAddress.getLocalHost();
  7067. System.out.println(address);
  7068. //javanetSocket类
  7069. //Socket方法——创建一个客户端套接字Socket对象
  7070. InetAddress address = InetAddress.getByName("2013-20160705IB");
  7071. Socket socket = new Socket(address,7777);
  7072. //close方法——关闭Socket套接字连接
  7073. InetAddress address = InetAddress.getByName("2013-20160705IB");
  7074. Socket socket = new Socket(address,7777);
  7075. socket.close();
  7076. //connect方法——将Socket套接字连接到指定的服务器
  7077. SocketAddress address = new InetSocketAddress("hello",1995);
  7078. Socket socket = new Socket();
  7079. socket.connect(address);
  7080. InetAddress addre = socket.getInetAddress();
  7081. socket.close();
  7082. //getInetAddress方法——返回Socket套接字连接的地址
  7083. SocketAddress address = new InetSocketAddress("hello",1995);
  7084. Socket socket = new Socket();
  7085. socket.connect(address);
  7086. InetAddress addre = socket.getInetAddress();
  7087. socket.close();
  7088. //getInputStream方法——返回套接字的输入流
  7089. SocketAddress address = new InetSocketAddress("hello",1995);
  7090. Socket socket = new Socket();
  7091. socket.connect(address);
  7092. InputStream stream = socket.getInputStream();
  7093. socket.close();
  7094. //getOutputStream方法——返回套接字的输出流
  7095. SocketAddress address = new InetSocketAddress("hello",1995);
  7096. Socket socket = new Socket();
  7097. socket.connect(address);
  7098. OutputStream stream = socket.getOutputStream();
  7099. socket.close();
  7100. //javanetServerSocket类
  7101. //ServerSocket方法——创建服务器端套接字ServerSocket对象
  7102. ServerSocket serverSocket = new ServerSocket(1995);
  7103. //accept方法——侦听并接受套接字的连接
  7104. ServerSocket serverSocket = new ServerSocket(1995);
  7105. serverSocket.accept();
  7106. //close方法——关闭当前的服务端套接字ServerSocket连接
  7107. ServerSocket serverSocket = new ServerSocket(1995);
  7108. serverSocket.accept();
  7109. serverSocket.close();
  7110. //isClosed方法——返回ServerSocket的关闭状态
  7111. ServerSocket serverSocket = new ServerSocket(1995);
  7112. serverSocket.accept();
  7113. serverSocket.close();
  7114. System.out.println(serverSocket.isClosed());
  7115. //javanetURL类
  7116. //URL方法——使用URL构造方法创建URL对象
  7117. URI url = new URI("www.baidu.com");
  7118. System.out.println(url);
  7119. //getPort方法——获取当前URL的端口号
  7120. URI url = new URI("www.baidu.com");
  7121. int port = url.getPort();
  7122. System.out.println(port);
  7123. //getDefaultPort方法——获取URL使用的当前协议的默认端口号
  7124. //获取URL使用的当前协议的默认端口号
  7125. //getProtocol方法——获取URL的协议名称
  7126. //获取URL的协议名称
  7127. //getHost方法——获取当前URL的主机名称
  7128. URI url = new URI("www.baidu.com");
  7129. String host = url.getHost();
  7130. System.out.println(host);
  7131. //openConnection方法——返回一个URLConnection对象
  7132. //返回一个URLConnection对象
  7133. //openStream方法——打开当前URL的连接
  7134. //打开当前URL的连接
  7135. //javanetURLConnection类
  7136. //connect方法——建立通信连接
  7137. //建立通信连接
  7138. //getContentLength方法——返回URL连接中的内容长度
  7139. //返回URL连接中的内容长度
  7140. //getContentType方法——返回content-type头字段的值
  7141. //返回content-type头字段的值
  7142. //getDate方法——返回date头字段的值
  7143. //返回date头字段的值
  7144. //getHeaderFields方法——返回投资端中不可修改的信息
  7145. //返回投资端中不可修改的信息
  7146. //getInputStream方法——返回URL连接的输入流
  7147. //返回URL连接的输入流
  7148. //getOutputStream方法——返回写入的连接的输出流
  7149. //返回写入的连接的输出流
  7150. //setDoInput方法——设置当前URLConnection的doInput字段值
  7151. //设置当前URLConnection的doInput字段值
  7152. //setDoOutput方法——设置当前URLConnection的doOutput字段值
  7153. //设置当前URLConnection的doOutput字段值

 

转载于:https://my.oschina.net/MoreYoungGavin/blog/707020

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/194008
推荐阅读
相关标签
  

闽ICP备14008679号