来源:
《大象》的第一章里作者提到了一个小游戏:小的时候,每个人各填一张纸条,丢进代表主体、地点、动作、客体的箱子中,再在里面分别抽一张组成一句完 整的话。
因为很无聊,于是就写写看~
功能很简单,就是可以输入主体、地点、动作、客体(也可以从文件导入,或是导出)。随机在列表中组成一句话。
嗯,单击结果会自动复制到剪贴板。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 import  java.awt.Dimension;import  java.awt.FlowLayout;import  java.awt.GridLayout;import  java.awt.Toolkit;import  java.awt.datatransfer.StringSelection;import  java.awt.event.ActionEvent;import  java.awt.event.ActionListener;import  java.io.BufferedReader;import  java.io.File;import  java.io.FileNotFoundException;import  java.io.FileReader;import  java.io.IOException;import  java.io.PrintWriter;import  java.util.Random;import  java.util.Vector;import  javax.swing.JButton;import  javax.swing.JFileChooser;import  javax.swing.JFrame;import  javax.swing.JLabel;import  javax.swing.JList;import  javax.swing.JPanel;import  javax.swing.JScrollPane;import  javax.swing.JTextField;import  javax.swing.ListSelectionModel;import  javax.swing.event.ListSelectionEvent;import  javax.swing.event.ListSelectionListener;import  javax.swing.filechooser.FileFilter;@SuppressWarnings ("serial" )public  class  RandomGame  extends  JFrame           private  String[] title = new  String[] { "主体:" , "地点:" , "动作:" , "客体:"  };          private  JTextField[] field = new  JTextField[4 ];          private  JButton[] addBtn = new  JButton[4 ];     private  JButton[] delBtn = new  JButton[4 ];     private  JButton[] importBtn = new  JButton[4 ];     private  JButton[] exportBtn = new  JButton[4 ];     private  JButton generateBtn = new  JButton("Generate" );     private  JButton exportResultBtn = new  JButton("Export Result" );          @SuppressWarnings ("unchecked" )     private  Vector[] data = new  Vector[4 ];     private  Vector<String> history = new  Vector<String>();          private  JList[] list = new  JList[4 ];     private  JList historyList = new  JList();          private  JTextField result = new  JTextField(30 );          private  Random randomer = new  Random();          public  RandomGame ()           this .setTitle("Random Game" );         this .setSize(630 , 560 );         this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         Toolkit tools = Toolkit.getDefaultToolkit();         int  screenWidth = tools.getScreenSize().width;         int  screenHeight = tools.getScreenSize().height;         this .setLocation((screenWidth - this .getWidth()) / 2 ,                 (screenHeight - this .getHeight()) / 2 );         this .fillComponents();         this .setVisible(true );     }          private  void  fillComponents ()           this .setLayout(new  FlowLayout(FlowLayout.CENTER));                  this .add(new  JPanel() {             {                 this .setPreferredSize(new  Dimension(630 , 270 ));                 this .setLayout(new  GridLayout(1 , 4 ));                 for  (int  i = 0 ; i < 4 ; ++i) {                     final  int  j = i;                     this .add(new  JPanel() {                         {                             this .add(new  JLabel(title[j]));                             addBtn[j] = new  JButton("+" );                             this .add(addBtn[j]);                             delBtn[j] = new  JButton("-" );                             this .add(delBtn[j]);                             field[j] = new  JTextField(12 );                             this .add(field[j]);                             data[j] = new  Vector<String>();                             addBtn[j].addActionListener(new  ActionListener() {                                 @Override                                  public  void  actionPerformed (ActionEvent arg0)                                       String text = field[j].getText().trim();                                     importData(j, text);                                 }                             });                             delBtn[j].addActionListener(new  ActionListener() {                                 @Override                                  public  void  actionPerformed (ActionEvent arg0)                                       String text = field[j].getText().trim();                                     if  (!text.isEmpty()                                             && data[j].contains(text)) {                                         data[j].remove(text);                                         list[j].setListData(data[j]);                                         field[j].setText("" );                                         field[j].requestFocus();                                     }                                 }                             });                             list[j] = new  JList();                             list[j]                                     .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);                             list[j].setFixedCellWidth(130 );                             list[j].setFixedCellHeight(20 );                             list[j]                                     .addListSelectionListener(new  ListSelectionListener() {                                         @Override                                          public  void  valueChanged (                                                  ListSelectionEvent e)                                              field[j].setText((String) list[j]                                                     .getSelectedValue());                                         }                                     });                             JScrollPane pane = new  JScrollPane(list[j]);                             pane                                     .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);                             pane                                     .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);                             this .add(pane);                             importBtn[j] = new  JButton("Import" );                             this .add(importBtn[j]);                             exportBtn[j] = new  JButton("Export" );                             this .add(exportBtn[j]);                             importBtn[j]                                     .addActionListener(new  ActionListener() {                                         @Override                                          public  void  actionPerformed (                                                  ActionEvent e)                                              JFileChooser chooser = new  JFileChooser();                                             chooser.removeChoosableFileFilter(chooser.getFileFilter());                                             chooser.addChoosableFileFilter(new  FileFilter() {                                                 @Override                                                  public  boolean  accept (File file)                                                       if  (file.isDirectory()) return  true ;                                                     return  file.getName().endsWith(".txt" );                                                 }                                                 @Override                                                  public  String getDescription ()                                                       return  ".txt" ;                                                 }                                             });                                             int  returnVal = chooser                                                     .showOpenDialog(RandomGame.this );                                             if  (returnVal == JFileChooser.APPROVE_OPTION) {                                                 File file = chooser                                                         .getSelectedFile();                                                 BufferedReader reader = null ;                                                 try  {                                                     reader = new  BufferedReader(                                                             new  FileReader(file));                                                     String line;                                                     while  ((line = reader                                                             .readLine()) != null ) {                                                         importData(j, line);                                                     }                                                 } catch  (FileNotFoundException e1) {                                                     e1.printStackTrace();                                                 } catch  (IOException e2) {                                                     e2.printStackTrace();                                                 }                                                 try  {                                                     reader.close();                                                 } catch  (IOException e1) {                                                     e1.printStackTrace();                                                 }                                             }                                         }                                     });                             exportBtn[j]                                     .addActionListener(new  ActionListener() {                                         @Override                                          public  void  actionPerformed (                                                  ActionEvent e)                                              saveFile(data[j]);                                         }                                     });                         }                     });                 }             }         });                  this .add(new  JPanel() {             {                 this .setPreferredSize(new  Dimension(600 , 80 ));                 this .setLayout(new  GridLayout(2 , 1 ));                 this .add(new  JPanel() {                     {                         this .add(generateBtn);                         this .add(exportResultBtn);                         generateBtn.addActionListener(new  ActionListener() {                             @Override                              public  void  actionPerformed (ActionEvent e)                                   for  (int  i = 0 ; i < 4 ; ++i) {                                     if  (data[i].size() < 1 ) {                                         result.setText("请先填入内容" );                                         return ;                                     }                                 }                                 String person = (String) data[0 ].get(randomer                                         .nextInt(data[0 ].size()));                                 String address = (String) data[1 ].get(randomer                                         .nextInt(data[1 ].size()));                                 String dosth = (String) data[2 ].get(randomer                                         .nextInt(data[2 ].size()));                                 String object = (String) data[3 ].get(randomer                                         .nextInt(data[3 ].size()));                                 result.setText(person + "在"  + address + dosth                                         + object);                                 history.add(result.getText());                                 historyList.setListData(history);                                 int  rowIndex = history.size() - 1 ;                                 historyList.scrollRectToVisible(historyList.getCellBounds(rowIndex,                                         rowIndex));                             }                         });                         exportResultBtn.addActionListener(new  ActionListener() {                             @Override                              public  void  actionPerformed (ActionEvent e)                                   saveFile(history);                             }                         });                     }                 });                 result.setEditable(false );                 this .add(result);             }         });         this .add(new  JPanel() {             {                 historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);                 historyList.setFixedCellWidth(600 );                 historyList.setFixedCellHeight(18 );                 historyList.addListSelectionListener(new  ListSelectionListener() {                     @Override                      public  void  valueChanged (ListSelectionEvent e)                           StringSelection stsel = new  StringSelection((String) historyList.getSelectedValue());                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);                     }                 });                 JScrollPane pane = new  JScrollPane(historyList);                 pane                         .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);                 pane                         .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);                 this .add(pane);             }         });     }     @SuppressWarnings ("unchecked" )     public  void  importData (int  index, String text)           if  (!text.isEmpty() && !data[index].contains(text)) {             data[index].add(text);             list[index].setListData(data[index]);             int  rowIndex = data[index].size() - 1 ;             list[index].scrollRectToVisible(list[index].getCellBounds(rowIndex,                     rowIndex));             field[index].setText("" );             field[index].requestFocus();         }     }     @SuppressWarnings ("unchecked" )     private  void  saveFile (Vector content)           JFileChooser chooser = new  JFileChooser();         chooser.removeChoosableFileFilter(chooser.getFileFilter());         chooser.addChoosableFileFilter(new  FileFilter() {             @Override              public  boolean  accept (File file)                   if  (file.isDirectory()) return  true ;                 return  file.getName().endsWith(".txt" );             }             @Override              public  String getDescription ()                   return  ".txt" ;             }         });         int  returnVal = chooser.showSaveDialog(RandomGame.this );         if  (returnVal == JFileChooser.APPROVE_OPTION) {             File file = chooser.getSelectedFile();             PrintWriter writer = null ;             try  {                 String ext = "" ;                 if  (!file.getName().endsWith(".txt" )) {                     ext = ".txt" ;                 }                 writer = new  PrintWriter(file.getPath() + ext);                 for  (Object line : content) {                     writer.write(line + "\r\n" );                 }                 writer.flush();                 writer.close();             } catch  (FileNotFoundException e1) {                 e1.printStackTrace();             }         }     }          public  static  void  main (String[] args)           new  RandomGame();     } } 
附几个txt:
主体
1 2 3 4 5 6 7 8 9 博丽灵梦 雾雨魔理沙 露米娅 琪露诺 红美铃 帕秋莉诺蕾姬 十六夜咲夜 蕾米莉亚 芙兰朵露 
地点
1 2 3 4 5 6 7 8 教室 图书馆 宿舍 食堂 实验室 篮球场 学生街 健身房 
动作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 吃 喝 玩 打 听 说 读 写 唱 爬 跑 跳 看 养 
客体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 房子 小毛驴 兔子 游戏 电脑 牛排 奶茶 咖啡 豆浆 怪味豆 苏打绿 电视 仓鼠 课本 MP3 楼梯 山 程序 论文 CET6词汇 PSP 
 
    
    
    
         Publishing this article is for the purpose of conveying more information, and does not mean agreeing with its views or confirming its description, nor does it mean that we are responsible for its authenticity. Should you have any questions or doubts about the content of the post, please don't hesitate to contact us. We will respond to you and deal with it as quickly as possible.