qq_45178850
我还以为你从来都不会选我
2019-06-21 13:35
采纳率: 75%
浏览 396
已采纳

java 窗口信息录入 麻烦帮我完善下几个很简单问题 本人刚开始学Java?

代码如下:问题我注释说明
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class My1 extends JFrame implements ActionListener {
JMenuBar z1;
JMenu z2;
JMenuItem z3,z4;
JLabel z5,z6,z0;
JTextField z7,z8,z9;
JButton b1,b2;
CardLayout c1;
JPanel c2,c3,c4;
JTextArea ta1;
File f = null;
JScrollPane sp;
My1(){
f = new File("b.txt");
z1 = new JMenuBar();
z2 = new JMenu("菜单选项");
z3 = new JMenuItem("录入");
z4 = new JMenuItem("显示");
z3.addActionListener(this);
z4.addActionListener(this);
this.setJMenuBar(z1);
z1.add(z2);
z2.add(z3);
z2.add(z4);
z5 = new JLabel("姓名:");
z5.setFont(new Font("楷体",Font.BOLD,20));
z6 = new JLabel("email:");
z6.setFont(new Font("楷体",Font.BOLD,20));
z0 = new JLabel("电话:");
z0.setFont(new Font("楷体",Font.BOLD,20));
b1 = new JButton("录入");
b1.setFont(new Font("楷体",Font.BOLD,20));
b2 = new JButton("清空");
b2.setFont(new Font("楷体",Font.BOLD,20));
b1.addActionListener(this);
b2.addActionListener(this);
z7 = new JTextField(20);
z8 = new JTextField(20);
z9 = new JTextField(20);
ta1 = new JTextArea(20,30);
sp = new JScrollPane(ta1);
c2 = new JPanel();
c3 = new JPanel();
c4 = new JPanel();
c1 = new CardLayout();
c2.add(z5);
c2.add(z7);
c2.add(z6);
c2.add(z8);
c2.add(z7);
c2.add(z0);
c2.add(z9);
c2.add(b1);
c2.add(b2);
c2.setLayout(null);
z5.setBounds(50, 20, 100, 30);
z7.setBounds(220, 20, 120,30);
z6.setBounds(50, 70, 100,30);
z8.setBounds(220, 70, 120,30);
z0.setBounds(50, 120, 100,30);
z9.setBounds(220, 120, 120,30);
b1.setBounds(50, 170, 100,30);
b2.setBounds(220, 170, 100,30);
this.setLayout(new FlowLayout());
c4.setLayout(c1);
c4.add("qwe1",c2);
c4.add("qwe2",c3);
c3.add(sp);
add(c4);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==z3) {
c1.show(c4,"qwe1");
}
if(e.getSource()==z4) {
c1.show(c4, "qwe2");
try {
BufferedReader in = new BufferedReader(new FileReader("b.txt"));
String name = null;
int number = 1;
ta1.setText("");
while((name = in.readLine()) != null){
ta1.append("\n"+number+": "+name);
ta1.append("\n------------------------------------------------------------------------ ");
number++;
}
in.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==b1){
try{
if(z7.getText().equals("")){
JOptionPane.showMessageDialog(null, "\t姓名不能为空!","提示",JOptionPane.INFORMATION_MESSAGE);
}
else if(z8.getText().equals("")){
JOptionPane.showMessageDialog(null, "\temail不能为空!","提示",JOptionPane.INFORMATION_MESSAGE);
}// 设置下email格式要求,格式不对弹出提示窗口显示“email格式错误”格式要求:______@.com结尾
else if(z9.getText().equals("")){
JOptionPane.showMessageDialog(null, "\t电话不能为空!","提示",JOptionPane.INFORMATION_MESSAGE);
}// 设置下手机号码l格式要求,格式不对弹出提示窗口显示“手机号码格式错误”格式要求开头是1,必须是11位数
else{
JOptionPane.showMessageDialog(null, "\t信息录入成功!","提示",JOptionPane.OK_CANCEL_OPTION);
BufferedWriter out = new BufferedWriter(new FileWriter("b.txt",true));
out.write("姓名:");
out.write(z7.getText());
out.write(" ,email:");
out.write(z8.getText());
out.write(" ,电话:");
out.write(z9.getText());
out.newLine();
out.close();
}
}
catch (Exception ee) {
ee.printStackTrace();
}

    }
        if(e.getSource()==b2){
        try{
            z7.setText("");
            z8.setText("");
            z9.setText("");
        }
        catch (Exception ee) {
        ee.printStackTrace();
        }
    }

}

}
public class Test2 {
public static void main(String[] args) {
new My1();

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • as_csdn
    代码的灵魂是bug! 2019-06-21 16:20
    已采纳

    把这两个方法放在需要验证的地方

    public static boolean isEmail(String email) {
            String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
            Pattern p = Pattern.compile(str);
            Matcher m = p.matcher(email);
            return m.matches();
            }
    
      public static boolean isPhoneNumberValid(String phoneNumber) {
            boolean isValid = false;
    
            String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
            CharSequence inputStr = phoneNumber;
    
            Pattern pattern = Pattern.compile(expression);
            Matcher matcher = pattern.matcher(inputStr);
    
            if (matcher.matches()) {
                isValid = true;
            }
    
                return isValid;
        }
    
    点赞 评论

相关推荐