二人团队项目增删改查
这周主要是二人合作四则运算的升级版
很长时间没有接触java之后,发现很多地方都已经忘记,这周主要学习了容器跟正则表达式
正则表达式是比较重要的一部分,我是根据别人的博客来学习的https://blog.csdn.net/zhangpeterx/article/details/83058800
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.] \w+)*\.\w+([-.]\w+)*
^[A-Za-z]+$ //匹配由26 个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母 的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个 英文字母或者下划线组成的字符串
匹配帐号是否合法(字母开头,允许5-16 字节,允许字母数字下划线):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
提取信息中的中国手机号码:(86)*0*13\d{9}
匹配所有的整数:^\-?[0-9]+$
这是我自己总结的一些正则表达式。
其中还有容器的学习
Panel面板 Window窗口 Frame框架 Dialog对话框 container是component的子类; 容器的特点:1、容器有一定的范围,一般的容器是矩形的,有些可以用边框框出来 2、容器有一定是位置; 3、容器通常有一个背景,背景填充整个容器 4、容器可以包含其他的基本组件,当容器显示出来的时候包含的基本组件会显示出来,否则则不会; Container类常用方法 add() 像容器中添加其他组件,可以是普通组件也可以是容器 getComponent()返回指定的组件 getComponentCount() 返回该容器内组件的个数 getComponents()返回该容器的所有组件
setLocation(int x,int y)设置组件位置
setSize(int width,int hight)设置组件大小
setBounds(int x,int y,int width,int hight)同时设置组件的位置,大小
setVisible(boolean b)设置组件的可见性;
按钮类 public static void main(String[] args) { //创建一个窗口 Frame frame=new Frame(); frame.setTitle("添加按钮");//设置窗口的程序 frame.setBounds(150,150,400,300);//窗口的起始位置x=150,y=150宽400,高300 Button btn=new Button("确定");//定义按钮对象 frame.add(btn);//将按钮添加到Frame中 frame.setVisible(true);//显示(true)或隐藏窗口(false) }
BorderLayout边界布局管理器是Frame的默认的布局管理器,将窗口划分成东、西、南、北、中五部分,将容器划分成5个区域,只能容纳5个组件 public static void main(String[] args) { //创建一个窗口 Frame frame=new Frame("Bourderyout布局管理器");//窗口的名字 frame.setBounds(150,150,400,300);//窗口的起始位置x=150,y=150宽400,高300 //LayoutManager布局管理器接口 LayoutManager lm=new BorderLayout();//创建BorderLayout布局管理器对象 frame.setLayout(lm);//为Frame设置布局管理器,默认为BorderLayout //add函数第一个参数是要添加的组件对象,第二个是组件在容器里面的位置,位置参数是类中的常量 frame.add(new Button("北"),BorderLayout.NORTH); frame.add(new Button("南"),BorderLayout.SOUTH); frame.add(new Button("西"),BorderLayout.WEST); frame.add(new Button("东"),BorderLayout.EAST); frame.add(new Button("中"),BorderLayout.CENTER); frame.setVisible(true);//显示(true)或隐藏窗口(false) }
Flowyout布局管理器会按照加入组件的顺序从左向右排列; hgap水平间距 vgap垂直间距 align组件的排列方向 构建方法 FlowLayout() FlowLayout(int align)使用指定排列方式 FlowLayout(int align,int hgap,int vgap) align中使用的参数有FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT public static void main(String[] args) { //创建一个窗口 Frame frame=new Frame(); frame.setTitle("FlowLayout布局管理器"); frame.setBounds(150,150,400,300);//窗口的起始位置x=150,y=150宽400,高300 //LayoutManager布局管理器接口 frame.setLayout(new FlowLayout());//为Frame设置布局管理器,如果不设置默认为BorderLayout for(int i=0;i<10;i++) { frame.add(new Button("按钮"+i)); } frame.setVisible(true);//显示(true)或隐藏窗口(false) }
将窗口划分成很多网格,每一格中放入一个组件,按照从左到右从上到下的顺序; GridLayout(int rows,int cols)指定行数列数默认横向行间距纵向行间距 GardLayout(int rows,int cols,int hgap,int vgap)指定行数列数指定横向行间距纵向行间距 public static void main(String[] args) { //创建一个窗口 Frame frame=new Frame(); frame.setTitle("GridLayout布局管理器"); frame.setBounds(150,150,400,300);//窗口的起始位置x=150,y=150宽400,高300 //设置网格的行与列5行5列 frame.setLayout(new GridLayout(5,5));//为Frame设置布局管理器,如果不设置默认为BorderLayout for(int i=1;i<=25;i++) frame.add(new Button("按钮"+i)); frame.setVisible(true);//显示(true)或隐藏窗口(false) }
CardLayout布局管理器是以时间来管理组件将加入容器里面的组件看成一叠卡片只有最上面的可以被看见 CardLayout()默认的构造方法 CardLayout(int hgap,int vgap)指定左右边界间距,上下边界间距 first(Container target)显示target容器中的第一个卡片 last(Container target)显示target容器中的最后一个卡片 previous(Container target)显示target容器中的前一个卡片 next()显示target容器中的后一个卡片 show(Container target,String name)显示容器中指定名字的卡片 public static void main(String[] args) { //创建一个窗口 Frame frame=new Frame(); frame.setTitle("CardLayout布局管理器"); frame.setBounds(150,150,400,300);//窗口的起始位置x=150,y=150宽400,高300 CardLayout layout=new CardLayout(); frame.setLayout(layout);//为Frame设置布局管理器 Panel p1=new Panel(); p1.setBackground(Color.RED);//设置第一个面板为红色 p1.add(new Label("背景为红色的面板")); Panel p2=new Panel(); p2.setBackground(Color.BLUE); p2.add(new Label("背景为蓝色的面板")); Panel p3=new Panel(); p3.setBackground(Color.GREEN); p3.add(new Label("背景为绿色的面板")); frame.add("redpanel",p1);//将面板添加到frame中 frame.add("bluepanel",p2); frame.add("greenpanel",p3); layout.last(frame); frame.setVisible(true);//显示(true)或隐藏窗口(false) }
其中我们的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.awt.BorderLayout; 2 import java.awt.EventQueue; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.Font; 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel; 10 import javax.swing.JTextField; 11 import javax.swing.SwingConstants; 12 13 import szys.frame; 14 15 import java.awt.EventQueue; 16 17 public class frame { 18 19 private JFrame frame;//图形界面,简单的window窗口 20 private JTextField textField;//文本框 21 private JTextField textField_1; 22 private JTextField textField_2; 23 private JTextField textField_3; 24 private JTextField textField_4; 25 private JTextField textField_5; 26 frame f; 27 28 int a; 29 int b; 30 char []op={'+','-','*','/'}; 31 String Sa; 32 String Sb; 33 int result; 34 static int i = (int) (Math.random() * 4);//大于0小于4的随机数 35 36 37 Expression expression = new Expression(); 38 private JButton btnNewButton_1;//按钮 39 40 41 /** 42 * Launch the application. 43 */ 44 public static void main(String[] args) 45 { 46 EventQueue.invokeLater(new Runnable() { 47 public void run() { 48 try { 49 frame window = new frame(); 50 window.frame.setVisible(true); 51 System.out.print(i); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 }); 57 } 58 59 /** 60 * Create the application. 61 * @throws Yichang 62 */ 63 public frame() throws Yichang { 64 initialize(); 65 } 66 67 /** 68 * Initialize the contents of the frame. 69 */ 70 private void initialize() throws Yichang{ 71 frame = new JFrame(); 72 frame.setBounds(100, 100, 517, 352); 73 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 74 75 JPanel panel = new JPanel(); 76 frame.getContentPane().add(panel, BorderLayout.CENTER); 77 panel.setLayout(null); 78 79 JLabel lblNewLabel = new JLabel("\u56DB\u5219\u8FD0\u7B97\u5668");//四则运算器 80 lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 81 lblNewLabel.setBounds(141, 0, 208, 57); 82 panel.add(lblNewLabel); 83 84 textField = new JTextField();//用来放随机数的第一个 85 textField.setEditable(false); 86 textField.setHorizontalAlignment(SwingConstants.CENTER);//设置文本是水平对齐方式 87 textField.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 88 textField.setBounds(56, 82, 88, 45); 89 panel.add(textField);//将组件插入到面板 90 textField.setColumns(10); 91 92 textField_1 = new JTextField();//用来放随机数的第二个 93 textField_1.setEditable(false); 94 textField_1.setHorizontalAlignment(SwingConstants.CENTER); 95 textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));//设置字体格式 96 textField_1.setColumns(10);//设置文本框长度 97 textField_1.setBounds(220, 82, 88, 45); 98 panel.add(textField_1); 99 100 textField_2 = new JTextField("="); 101 textField_2.setHorizontalAlignment(SwingConstants.CENTER); 102 textField_2.setEditable(false);//设置选项不可用 103 textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 104 textField_2.setColumns(10); 105 textField_2.setBounds(318, 82, 36, 45);//前两个是左上角在容器的坐标,后两个是宽度跟高度 106 panel.add(textField_2); 107 108 textField_3 = new JTextField();//写入自己的答案 109 textField_3.setHorizontalAlignment(SwingConstants.CENTER); 110 textField_3.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 111 textField_3.setColumns(10); 112 textField_3.setBounds(371, 82, 88, 45); 113 panel.add(textField_3); 114 115 JButton btnNewButton = new JButton("\u505A\u5B8C\u4E86");//做完了 116 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 117 btnNewButton.setBounds(188, 156, 131, 45); 118 panel.add(btnNewButton); 119 120 JLabel lblNewLabel_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");//计算结果 121 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 122 lblNewLabel_1.setBounds(56, 222, 131, 35); 123 panel.add(lblNewLabel_1); 124 125 textField_4 = new JTextField(); 126 textField_4.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 127 textField_4.setColumns(10); 128 textField_4.setBounds(188, 222, 161, 35); 129 panel.add(textField_4); 130 131 textField_5 = new JTextField();//运算符 132 textField_5.setHorizontalAlignment(SwingConstants.CENTER); 133 textField_5.setEditable(false); 134 textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 135 textField_5.setText(String.valueOf(op[i])); 136 textField_5.setColumns(10); 137 textField_5.setBounds(159, 82, 44, 45); 138 panel.add(textField_5); 139 140 a = expression.geta(); 141 Sa = String.valueOf(a); 142 textField.setText(Sa); 143 144 b = expression.getb(); 145 Sb = String.valueOf(b); 146 textField_1.setText(Sb); 147 148 btnNewButton_1 = new JButton("\u518D\u6765 ");//u518D\u6765 再来的意思 149 btnNewButton_1.addActionListener(new ActionListener() {//让按钮具备关闭窗口的功能 150 public void actionPerformed(ActionEvent e) { 151 152 try { 153 f = new frame(); 154 } catch (Yichang e1) { 155 // TODO 自动生成的 catch 块 156 e1.printStackTrace(); 157 } 158 frame.dispose(); 159 f.frame.setVisible(true);//显示窗体 160 161 } 162 }); 163 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 30)); 164 btnNewButton_1.setBounds(366, 222, 107, 36); 165 panel.add(btnNewButton_1); 166 167 168 btnNewButton.addActionListener(new ActionListener() { 169 public void actionPerformed(ActionEvent arg0){ 170 int n3=Integer.valueOf(textField_3.getText().toString()); //获得textField中输入的值; 171 switch(op[i]) 172 { 173 case '+':result = expression.expressionadd();break; 174 case '-':result = expression.expressionmin();break; 175 case '*':result = expression.expressionchen();break; 176 case '/':result = expression.expressionchu();break; 177 } 178 if(textField_3.getText()!="") 179 { 180 if(result==n3) 181 { 182 textField_4.setText(" 正确"); 183 } 184 else 185 { 186 textField_4.setText(" 错误!答案为"+result); 187 } 188 } 189 try { 190 if(textField_3.getText()=="") 191 throw new Yichang("不能为空!"); 192 } catch (Yichang e) { 193 // TODO 自动生成的 catch 块 194 e.printStackTrace(); 195 } 196 197 /*try { 198 if(n3<0) 199 200 throw new Yichang("不可能是负数!"); 201 } catch (Yichang e) { 202 // TODO 自动生成的 catch 块 203 e.printStackTrace(); 204 }*/ 205 206 } 207 }); 208 } 209 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.*; 2 3 public class Input { 4 public int a; 5 static Scanner in=new Scanner(System.in); 6 public void seta(int intput){ 7 a = intput; 8 } 9 public Input() 10 { 11 try{ 12 a = in.nextInt(); 13 } 14 catch(InputMismatchException e) 15 { 16 System.err.println("\n错误! 请你输入一个整数!\n"); 17 } 18 } 19 public int getinput() 20 { 21 return a; 22 } 23 24 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package szys; 2 3 import java.util.*; 4 5 import szys.Input; 6 public class Expression 7 { 8 private int a = (int)(Math.random() * Math.pow(10,2)-1); 9 private int b = (int)(Math.random() * Math.pow(10,2)-1); 10 private int op; 11 static int c;//用户答案 12 int answer;//答案 13 int answer1; 14 static Scanner in=new Scanner(System.in); 15 16 public int geta() 17 { 18 return a; 19 } 20 21 public void seta(int a) 22 { 23 this.a = a; 24 } 25 26 public int getb() 27 { 28 return b; 29 } 30 31 public void setb(int b) 32 { 33 this.b = b; 34 } 35 36 public int expressionadd() 37 { 38 answer = a+b; 39 return answer; 40 } 41 public int expressionmin() 42 { 43 answer = a-b; 44 return answer; 45 } 46 public int expressionchen() 47 { 48 answer = a*b; 49 return answer; 50 } 51 public int expressionchu() 52 { 53 b = (b == 0 ? b = 1 +(int)(Math.random() * Math.pow(10,2)-1):b);//排除除法被除数为0的情况 54 answer = a/b; 55 return answer; 56 } 57 public static void main(String[] args){ 58 int answer; 59 Expression expression = new Expression(); 60 61 //answer = expression.answer; 62 //Input input = new Input(); 63 //Expression.c = input.a; 64 /*try{ 65 Expression.c = in.nextInt(); 66 } 67 catch(InputMismatchException e) 68 { System.err.println("\n错误! ,请你输入一个整数"); 69 }*/ 70 //if(answer==c) 71 /* { 72 System.out.print("答对了"); 73 } 74 else System.out.print("答错了");*/ 75 //System.out.print("answer="+answer); 76 } 77 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Yichang extends Exception 2 { 3 public Yichang(String msg) 4 { 5 super(msg); 6 } 7 }