[关闭]
@Sarah 2015-11-05T17:55:24.000000Z 字数 6261 阅读 914

JAVA 图形界面

java笔记
j2ee/andriod(widget)
pc:c++/.net
html特别简单,就是超文本,没有逻辑,不是语言,就像写论文排版格式 用dream weaver
js

图形界面

GUI:
1awt:原始
2swing:丰富 复杂 慢

awt

1组件component:窗口,按钮,文字 文本框 菜单
2事件监听:
3布局管理器:

frame :容器
panel:

菜单:menubar/ menu /menuitem

windowlistener:7个抽象 7函数实现,windowAdapter实现

此处输入图片的描述
此处输入图片的描述

List 列表
choice 下拉菜单
checkbox 男女
textArea 文本框 多行
Diolog:容器

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class Test implements ActionListener{
  4. Button[] bb;
  5. TextField tf1=new TextField("0",10);
  6. TextField tf2=new TextField("0",10);
  7. public static void main(String[] args) {
  8. new Test().go();//为了后面的this
  9. }
  10. void go(){
  11. Frame f=new Frame("Test Frame");//0*0,unvisible
  12. MenuBar mb=new MenuBar();
  13. f.setMenuBar(mb);
  14. Menu m1=new Menu("Game");
  15. mb.add(m1);
  16. MenuItem mi1=new MenuItem("New game");
  17. MenuItem mi2=new MenuItem("Exit");
  18. m1.add(mi1);
  19. m1.addSeparator();
  20. m1.add(mi2);
  21. f.setSize(300, 400);
  22. //f.setLayout(new GridLayout());
  23. Button b=new Button("click me");
  24. Panel panS=new Panel();
  25. Panel panN=new Panel();
  26. Panel panW=new Panel();
  27. Panel panE=new Panel();
  28. Panel panC=new Panel();
  29. panN.add(new Label(" "));
  30. panW.add(new Label(" "));
  31. panE.add(new Label(" "));
  32. panC.setLayout(new FlowLayout(FlowLayout.CENTER,0,1));
  33. panS.add(new Label("Score:"));
  34. panS.add(tf1);
  35. panS.add(new Label("Time:"));
  36. panS.add(tf2);
  37. bb=new Button[10*10];
  38. for(int i=0;i<10;i++)
  39. for(int j=0;j<10;j++){
  40. bb[i*10+j]=new Button("");
  41. bb[i*10+j].addActionListener(this);
  42. //前面go()是为了这个this
  43. //bb.setSize(20,20);
  44. panC.add(bb[i*10+j]);
  45. }
  46. f.add(panC,BorderLayout.CENTER);
  47. f.add(panN,BorderLayout.NORTH);
  48. f.add(panW,BorderLayout.WEST);
  49. f.add(panE,BorderLayout.EAST);
  50. f.add(panS,BorderLayout.SOUTH);
  51. f.setVisible(true);
  52. f.addWindowListener(new MyWindowListener());
  53. }
  54. @Override
  55. public void actionPerformed(ActionEvent arg0) {
  56. String t=tf1.getText();
  57. System.out.println(t);
  58. Button b=(Button)arg0.getSource();
  59. for(int i=0;i<100;i++)
  60. if(bb[i]==b)
  61. System.out.println(i);
  62. }
  63. }
  1. import java.awt.event.WindowAdapter;
  2. import java.awt.event.WindowEvent;
  3. import java.awt.event.WindowListener;//抽象类,需要给抽象函数重写具体函数
  4. public class MyWindowListener extends WindowAdapter {
  5. @Override
  6. public void windowClosing(WindowEvent arg0) {
  7. // TODO Auto-generated method stub
  8. int i=javax.swing.JOptionPane.showConfirmDialog(null, "Are you exit game?","confirm",javax.swing.JOptionPane.YES_NO_OPTION);
  9. if(i==javax.swing.JOptionPane.YES_OPTION)
  10. System.exit(0);
  11. }
  12. }
  13. //WindowAdapter是windowListner的子类,所以这里只需要把需要用到的函数重写然后覆盖即可,而不用整个全都重写。

上次遗留作业完成

  1. package reminder;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.sql.Time;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. public class ReminderTest {
  14. BufferedReader kbr;
  15. public ReminderTest() {
  16. this.kbr = new BufferedReader(new InputStreamReader(System.in));
  17. }
  18. // 读取文件到array list
  19. public List txtToList() {
  20. List<String> todoList = new ArrayList<String>();
  21. try {
  22. FileReader fr = new FileReader("日程.txt");
  23. BufferedReader br = new BufferedReader(fr);
  24. while (true) {
  25. String s = br.readLine();
  26. if (s == null)
  27. break;
  28. todoList.add(s);
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. return todoList;
  34. }
  35. // 写数组到txt
  36. public void listToTxt(List<String> todoList) {
  37. try {
  38. File f = new File("日程.txt");
  39. BufferedWriter bw = new BufferedWriter(new FileWriter(f));
  40. for (int i = 0; i < todoList.size(); i++) {
  41. bw.write(todoList.get(i) + "\n");
  42. }
  43. bw.close();
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. // 查看
  50. public void check() {
  51. List<String> todoList = txtToList();
  52. for (String s : todoList) {
  53. String ss[] = s.split(";");
  54. System.out.println("name:" + ss[0]);
  55. System.out.println("address:" + ss[1]);
  56. System.out.println("date:" + ss[2]);
  57. System.out.println("time:" + ss[3]);
  58. if (ss.length > 4)
  59. System.out.println("alarm:" + ss[4]);
  60. System.out.println("-----------------------------------");
  61. }
  62. }
  63. public String inputTodo() {
  64. StringBuffer todo = new StringBuffer();
  65. String s = null;
  66. try {
  67. System.out.print("name:");
  68. s = kbr.readLine();
  69. todo.append(s).append(";");
  70. System.out.print("address:");
  71. s = kbr.readLine();
  72. todo.append(s).append(";");
  73. while (true) {
  74. try {
  75. System.out.print("date(yyyy-mm-dd):");
  76. s = kbr.readLine();
  77. String s1 = s.substring(0, 4);
  78. int i = Integer.parseInt(s1);
  79. String s2 = s.substring(5, 7);
  80. int j = Integer.parseInt(s2);
  81. String s3 = s.substring(8, 10);
  82. int k = Integer.parseInt(s3);
  83. System.out.println(i+" "+j+ " "+k);
  84. Date d=new Date(i-1900,j-1,k);
  85. System.out.println(d);
  86. todo.append(s).append(";");
  87. break;
  88. } catch (Exception e) {
  89. // TODO Auto-generated catch block
  90. System.out.println("date error,please rewrite");
  91. }
  92. }
  93. while (true) {
  94. try {
  95. System.out.print("time(hh:mm):");
  96. s = kbr.readLine();
  97. String s3 = s.substring(0, 2);
  98. int i = Integer.parseInt(s3);
  99. String s4 = s.substring(3, 5);
  100. int j = Integer.parseInt(s4);
  101. Time t=new Time(i,j,0);
  102. todo.append(s);
  103. break;
  104. } catch (Exception e) {
  105. System.out.println("time error,please rewrite");
  106. }
  107. }
  108. System.out.print("need alarm?(y/n)");
  109. s = kbr.readLine();
  110. if (s.equals("y") || s.equalsIgnoreCase("Y")) {
  111. System.out.print("alarm time:");
  112. s = kbr.readLine();
  113. todo.append(";").append(s);
  114. }
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. return todo.toString();
  119. }
  120. // 录入
  121. public void input() {
  122. List<String> todoList = txtToList();
  123. todoList.add(inputTodo());
  124. listToTxt(todoList);
  125. }
  126. // 删除
  127. public void delete() {
  128. System.out.println("plese input the name to delete:");
  129. List<String> todoList = txtToList();
  130. String j = "";
  131. try {
  132. j = kbr.readLine();
  133. for (String s : todoList) {
  134. if (s.startsWith(j)) {
  135. todoList.remove(s);
  136. System.out.println("done.");
  137. break;
  138. }
  139. }
  140. ;
  141. listToTxt(todoList);
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146. // 修改
  147. public void change() {
  148. int k = 0;
  149. List<String> todoList = txtToList();
  150. String x = null;
  151. try {
  152. System.out.println("input the name to change...");
  153. x = kbr.readLine();
  154. for (String s : todoList) {
  155. if (s.startsWith(x)) {
  156. todoList.remove(s);
  157. todoList.add(inputTodo());
  158. System.out.println("done.");
  159. break;
  160. }
  161. }
  162. ;
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. }
  166. listToTxt(todoList);
  167. }
  168. // 设置闹钟
  169. public void alarm() {
  170. String x = new String();
  171. List<String> todoList = txtToList();
  172. try {
  173. System.out.println("set the alarm time HH:mm");
  174. x = kbr.readLine();
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. todoList.set(4, x);
  179. listToTxt(todoList);
  180. }
  181. public static void main(String[] args) {
  182. ReminderTest rm = new ReminderTest();
  183. //new AlarmThread(rm).start();
  184. rm.go();
  185. }
  186. void go() {
  187. while (true) {
  188. System.out.println("1:check todo list");
  189. System.out.println("2:input new todo");
  190. System.out.println("3:delete todo list");
  191. System.out.println("4:change todo list");
  192. System.out.print("0:exit>>>>");
  193. String s = null;
  194. try {
  195. s = kbr.readLine();
  196. } catch (IOException e) {
  197. e.printStackTrace();
  198. }
  199. if (s.equals("1")) {
  200. check();
  201. } else if (s.equals("2")) {
  202. input();
  203. } else if (s.equals("3")) {
  204. delete();
  205. } else if (s.equals("4")) {
  206. change();
  207. } else if (s.equals("5")) {
  208. alarm();
  209. } else if (s.equals("0")) {
  210. System.exit(0);
  211. }
  212. }
  213. }
  214. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注