driver.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 72 行
JAVA
72 行
import java.awt.Label;import java.awt.event.*;import javax.swing.*;/** Text Message Permutation (Figure 12.6) * Author: David Riley * Date: January, 2005 */public class Driver implements ActionListener { JFrame window; JTextField inField; Label inLabel, outLabel; JButton button; /** post: window is instantiated to contain inField, inLabel * outLabel and button * and outLabel.getText() == 矣 * and button誷 events are delagated to this */ public Driver() { window = new JFrame("Word Scramble"); window.setBounds(20, 20, 300, 200); window.setLayout(null); window.setVisible(true); inLabel = new Label("Type your message below."); inLabel.setBounds(10, 10, 200, 20); window.add(inLabel, 0); inField = new JTextField(); inField.setBounds(30, 40, 250, 25); window.add(inField, 0); button = new JButton("Click here to scramble"); button.setBounds(20, 90, 260, 25); button.addActionListener(this); window.add(button, 0); outLabel = new Label(""); outLabel.setBounds(30, 130, 200, 20); window.add(outLabel, 0); window.repaint(); } /** post: outLabel is updated to be a randomly chosen * permutation of inField.getText() */ public void actionPerformed(ActionEvent e) { String inStr = inField.getText(); String outStr; char[] message = new char[ inStr.length() ]; int rnd1, rnd2; char temp; for (int j=0; j!=message.length; j++ ) { message[j] = inStr.charAt(j); } for (int r=1; r != inStr.length()*2+1; r++) { rnd1 = (int)( Math.random()*message.length ); rnd2 = (int)( Math.random()*message.length ); temp = message[rnd1]; message[rnd1] = message[rnd2]; message[rnd2] = temp; } outStr = ""; for (char c : message) { outStr = outStr + c; } outLabel.setText(outStr); window.repaint(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?