无聊之作。。随机组句。。
YOYO
posted @ 2010年3月17日 21:12
in 【Java SE】
, 2685 阅读
《大象》的第一章里作者提到了一个小游戏:小的时候,每个人各填一张纸条,丢进代表主体、地点、动作、客体的箱子中,再在里面分别抽一张组成一句完整的话。
因为很无聊,于是就写写看~
功能很简单,就是可以输入主体、地点、动作、客体(也可以从文件导入,或是导出)。随机在列表中组成一句话。
嗯,单击结果会自动复制到剪贴板。
package org.yoyo.game; 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; /** * * @author YOYO * */ @SuppressWarnings("serial") public class RandomGame extends JFrame { /** * Labels' String */ private String[] title = new String[] { "主体:", "地点:", "动作:", "客体:" }; /** * Text Field */ private JTextField[] field = new JTextField[4]; /** * Buttons */ 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"); /** * Data Vector */ @SuppressWarnings("unchecked") private Vector[] data = new Vector[4]; private Vector<String> history = new Vector<String>(); /** * JList */ private JList[] list = new JList[4]; private JList historyList = new JList(); /** * Result Field */ private JTextField result = new JTextField(30); /** * Randomer */ 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)); // input Panel 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]); } }); } }); } } }); // result Panel 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(); } } } /** * @param args */ public static void main(String[] args) { new RandomGame(); } }
附几个txt:
主体
YOYO Cyin Skittles Lee Die Mistoetle Vanish Duck
地点
教室 图书馆 宿舍 食堂 实验室 篮球场 学生街 健身房
动作
吃 喝 玩 打 听 说 读 写 唱 爬 跑 跳 看 养
客体
房子 小毛驴 兔子 游戏 电脑 牛排 奶茶 咖啡 豆浆 怪味豆 苏打绿 电视 仓鼠 课本 MP3 楼梯 山 程序 论文 CET6词汇 PSP
- 无匹配