package com; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class YanZhengMa { public static void main(String[] args){ new windows(); } } class windows extends JFrame{ windows(){ pan1 p1 =new pan1(); this.add(p1); this.setBounds(500,500,250,200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } class pan1 extends JPanel implements ActionListener{ JButton jb1,jb2; JLabel jl1,jl2,jl3; JTextField jt1; Random r =new Random(); int a =r.nextInt(10); int b =r.nextInt(10); int c =r.nextInt(10); int d =r.nextInt(10); String aa =a+""; String bb =b+""; String cc =c+""; String dd =d+""; String f =aa+bb+cc+dd; pan1(){ setLayout(null); jb1=new JButton(); jb2=new JButton(); jl1=new JLabel(); jl2=new JLabel(); jl3=new JLabel(); jt1=new JTextField(); jb1.setText("确定"); jb2.setText("取消"); jl1.setText("请输入验证码:"); jl2.setText(f); jl3.setText("验证码:"); jl3.setBounds(10,10,100,30); jl3.setFont(new Font("", Font.BOLD,20)); jl2.setBounds(100,0,120,50); jl2.setFont(new Font("", Font.HANGING_BASELINE,50)); jl1.setBounds(10,70,100,30); jb1.setBounds(20,120,70,30); jb2.setBounds(130,120,70,30); jt1.setBounds(110,70,100,30); jb1.addActionListener(this); jb2.addActionListener(this); this.add(jb1); this.add(jb2); this.add(jt1); this.add(jl1); this.add(jl3); this.add(jl2); } @Override public void actionPerformed(ActionEvent e) { String s=e.getActionCommand(); switch (s){ case"确定": way1(); break; case"取消": way2(); break; } } void way1(){ String g =jt1.getText(); if(g.equals(f)){ way3(); }else { JOptionPane.showMessageDialog(null, "这不是正确的验证码", "错误", JOptionPane.ERROR_MESSAGE); } } void way2(){ System.exit(0); } void way3(){ } }