界面实验任务
Posted CaptainAI
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了界面实验任务相关的知识,希望对你有一定的参考价值。
源程序代码
1 import javax.swing.JOptionPane; 2 3 import org.omg.CORBA.StringHolder; 4 5 /* 6 * jiemian.java 7 * 8 * Created on __DATE__, __TIME__ 9 */ 10 11 /** 12 * 13 * @author __USER__ 14 */ 15 public class jiemian extends javax.swing.JFrame { 16 17 /** Creates new form jiemian */ 18 public jiemian() { 19 initComponents(); 20 } 21 22 /** This method is called from within the constructor to 23 * initialize the form. 24 * WARNING: Do NOT modify this code. The content of this method is 25 * always regenerated by the Form Editor. 26 */ 27 //GEN-BEGIN:initComponents 28 // <editor-fold defaultstate="collapsed" desc="Generated Code"> 29 private void initComponents() { 30 31 label1 = new java.awt.Label(); 32 textField1 = new java.awt.TextField(); 33 label2 = new java.awt.Label(); 34 textField2 = new java.awt.TextField(); 35 label3 = new java.awt.Label(); 36 textField3 = new java.awt.TextField(); 37 button1 = new java.awt.Button(); 38 button2 = new java.awt.Button(); 39 40 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 41 addWindowListener(new java.awt.event.WindowAdapter() { 42 public void windowOpened(java.awt.event.WindowEvent evt) { 43 open(evt); 44 } 45 }); 46 47 label1.setText("\\u8d26\\u53f7"); 48 49 label2.setText("\\u5bc6\\u7801"); 50 51 label3.setText("\\u9a8c\\u8bc1\\u7801"); 52 53 button1.setLabel("\\u767b\\u5f55"); 54 button1.addActionListener(new java.awt.event.ActionListener() { 55 public void actionPerformed(java.awt.event.ActionEvent evt) { 56 button1ActionPerformed(evt); 57 } 58 }); 59 60 button2.setLabel("\\u9000\\u51fa"); 61 62 javax.swing.GroupLayout layout = new javax.swing.GroupLayout( 63 getContentPane()); 64 getContentPane().setLayout(layout); 65 layout.setHorizontalGroup(layout 66 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 67 .addGroup( 68 layout.createSequentialGroup() 69 .addGap(29, 29, 29) 70 .addGroup( 71 layout.createParallelGroup( 72 javax.swing.GroupLayout.Alignment.LEADING) 73 .addComponent( 74 label2, 75 javax.swing.GroupLayout.Alignment.TRAILING, 76 javax.swing.GroupLayout.DEFAULT_SIZE, 77 97, Short.MAX_VALUE) 78 .addGroup( 79 layout.createSequentialGroup() 80 .addComponent( 81 label1, 82 javax.swing.GroupLayout.PREFERRED_SIZE, 83 46, 84 javax.swing.GroupLayout.PREFERRED_SIZE) 85 .addPreferredGap( 86 javax.swing.LayoutStyle.ComponentPlacement.RELATED)) 87 .addGroup( 88 layout.createSequentialGroup() 89 .addComponent( 90 label3, 91 javax.swing.GroupLayout.PREFERRED_SIZE, 92 68, 93 javax.swing.GroupLayout.PREFERRED_SIZE) 94 .addPreferredGap( 95 javax.swing.LayoutStyle.ComponentPlacement.RELATED))) 96 .addGroup( 97 layout.createParallelGroup( 98 javax.swing.GroupLayout.Alignment.LEADING, 99 false) 100 .addComponent( 101 textField3, 102 javax.swing.GroupLayout.DEFAULT_SIZE, 103 javax.swing.GroupLayout.DEFAULT_SIZE, 104 Short.MAX_VALUE) 105 .addComponent( 106 textField2, 107 javax.swing.GroupLayout.DEFAULT_SIZE, 108 javax.swing.GroupLayout.DEFAULT_SIZE, 109 Short.MAX_VALUE) 110 .addComponent( 111 textField1, 112 javax.swing.GroupLayout.DEFAULT_SIZE, 113 132, Short.MAX_VALUE)) 114 .addGap(142, 142, 142)) 115 .addGroup( 116 javax.swing.GroupLayout.Alignment.TRAILING, 117 layout.createSequentialGroup() 118 .addGap(45, 45, 45) 119 .addComponent(button1, 120 javax.swing.GroupLayout.PREFERRED_SIZE, 121 76, 122 javax.swing.GroupLayout.PREFERRED_SIZE) 123 .addPreferredGap( 124 javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125 96, Short.MAX_VALUE) 126 .addComponent(button2, 127 javax.swing.GroupLayout.PREFERRED_SIZE, 128 72, 129 javax.swing.GroupLayout.PREFERRED_SIZE) 130 .addGap(111, 111, 111))); 131 layout.setVerticalGroup(layout 132 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 133 .addGroup( 134 layout.createSequentialGroup() 135 .addGap(29, 29, 29) 136 .addGroup( 137 layout.createParallelGroup( 138 javax.swing.GroupLayout.Alignment.LEADING) 139 .addComponent( 140 textField1, 141 javax.swing.GroupLayout.PREFERRED_SIZE, 142 javax.swing.GroupLayout.DEFAULT_SIZE, 143 javax.swing.GroupLayout.PREFERRED_SIZE) 144 .addComponent( 145 label1, 146 javax.swing.GroupLayout.PREFERRED_SIZE, 147 javax.swing.GroupLayout.DEFAULT_SIZE, 148 javax.swing.GroupLayout.PREFERRED_SIZE)) 149 .addGap(32, 32, 32) 150 .addGroup( 151 layout.createParallelGroup( 152 javax.swing.GroupLayout.Alignment.LEADING) 153 .addComponent( 154 textField2, 155 javax.swing.GroupLayout.PREFERRED_SIZE, 156 javax.swing.GroupLayout.DEFAULT_SIZE, 157 javax.swing.GroupLayout.PREFERRED_SIZE) 158 .addComponent( 159 label2, 160 javax.swing.GroupLayout.PREFERRED_SIZE, 161 javax.swing.GroupLayout.DEFAULT_SIZE, 162 javax.swing.GroupLayout.PREFERRED_SIZE)) 163 .addGap(32, 32, 32) 164 .addGroup( 165 layout.createParallelGroup( 166 javax.swing.GroupLayout.Alignment.TRAILING) 167 .addComponent( 168 textField3, 169 javax.swing.GroupLayout.PREFERRED_SIZE, 170 javax.swing.GroupLayout.DEFAULT_SIZE, 171 javax.swing.GroupLayout.PREFERRED_SIZE) 172 .addComponent( 173 label3, 174 javax.swing.GroupLayout.PREFERRED_SIZE, 175 javax.swing.GroupLayout.DEFAULT_SIZE, 176 javax.swing.GroupLayout.PREFERRED_SIZE)) 177 .addGap(52, 52, 52) 178 .addGroup( 179 layout.createParallelGroup( 180 javax.swing.GroupLayout.Alignment.TRAILING) 181 .addComponent( 182 button2, 183 javax.swing.GroupLayout.PREFERRED_SIZE, 184 javax.swing.GroupLayout.DEFAULT_SIZE, 185 javax.swing.GroupLayout.PREFERRED_SIZE) 186 .addComponent( 187 button1, 188 javax.swing.GroupLayout.PREFERRED_SIZE, 189 javax.swing.GroupLayout.DEFAULT_SIZE, 190 javax.swing.GroupLayout.PREFERRED_SIZE)) 191 .addContainerGap(63, Short.MAX_VALUE))); 192 193 pack(); 194 }// </editor-fold> 195 //GEN-END:initComponents 196 197 private void open(java.awt.event.WindowEvent evt) { 198 // TODO add your handling code here: 199 String b = ""; 200 for (int i = 0; i < 6; i++) { 201 String c; 202 c = sj(); 203 b = b + c; 204 } 205 label3.setText(b); 206 } 207 208 private void button1ActionPerformed(java.awt.event.ActionEvent evt) { 209 210 if (textField3.getText() .equals(label3.getText())) { 211 JOptionPane.showMessageDialog(null, "验证码正确登录成功"); 212 213 } 214 215 else { 216 JOptionPane.showMessageDialog(null, "验证码错误登录失败,验证码已经重新刷新"); 217 218 String b = ""; 219 for (int i = 0; i < 6; i++) { 220 String c; 221 c = sj(); 222 b = b + c; 223 } 224 label3.setText(b); 225 226 } 227 228 } 229 230 /** 231 * @param args the command line arguments 232 */ 233 public static String sj() { 234 int a = (int) (Math.random() * 26); 235 return cb(a); 236 } 237 238 public static String cb(int a) { 239 String b[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", 240 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", 241 "X", "Y", "Z" }; 242 return b[a]; 243 } 244 245 public static void main(String args[]) { 246 java.awt.EventQueue.invokeLater(new Runnable() { 247 public void run() { 248 new jiemian().setVisible(true); 249 } 250 }); 251 } 252 253 //GEN-BEGIN:variables 254 // Variables declaration - do not modify 255 private java.awt.Button button1; 256 private java.awt.Button button2; 257 private java.awt.Label label1; 258 private java.awt.Label label2; 259 private java.awt.Label label3; 260 private java.awt.TextField textField1; 261 private java.awt.TextField textField2; 262 private java.awt.TextField textField3; 263 // End of variables declaration//GEN-END:variables 264 265 }
该代码画出来的,最后在登录位置设置验证码,首先随机26个数,定义字符串从A到Z,然后返回任意字符串中的一个,利用for循环,显示六个字符如果标签中的字符与随机字符相同则成功,若不同则重新输入,且验证码进行改变。
实验截图
实验不足
1.验证码没有像熟悉的那样在右侧,且没有了验证码字符显示。
2.验证码只有大写的英文字母。
3.界面不好看。
以上是关于界面实验任务的主要内容,如果未能解决你的问题,请参考以下文章