import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Example extends JFrame {static Example frm=new Example();static JTextField txf=new JTextField();static TextArea txa=new TextArea("",8,14,TextArea.SCROLLBARS_VERTICAL_ONLY);static JTextArea txa1=new JTextArea();public static void main(String[] args) {Button btn=new Button("抽取");Button btn1=new Button("退出");frm.setTitle(" ");frm.setSize(600,400);frm.setLayout(null);frm.setBackground(Color.gray);frm.setResizable(false);btn.setBounds(500,250,50,30);btn1.setBounds(500,320,50,30);txf.setBounds(20,50,200,30);txa.setBounds(20,100,200,250);txa1.setBounds(240,50,200,300);txa1.setEditable(false);txa.setEditable(false);txa.setBackground(Color.white);txf.addKeyListener(new Ky());btn.addActionListener(new Act());frm.add(btn);frm.add(btn1);frm.add(txf);frm.add(txa);frm.add(txa1);frm.setVisible(true);}static class Ky implements KeyListener{public void keyPressed(KeyEvent a){if(a.getKeyChar()!='\n')return;String name=txf.getText();if(name.isEmpty())return;txa.append(name+"\n");txa.selectAll();}public void keyReleased(KeyEvent e) { }public void keyTyped(KeyEvent e) {}}static class Act implements ActionListener{public void actionPerformed(ActionEvent a){String perstring=txa.getText();String[] personnelArray=perstring.split("\n");int index=(int)(Math.random()*personnelArray.length);String formatArg="本次抽取观众人员:\n\t%1$s\n成为本次观众抽奖的大奖得主。"+"\n\n我们将为%1$s颁发:\n\t过期的酸牛奶二十箱。";String info=String.format(formatArg,personnelArray[index]);txa1.setText(info);}}}
添加回答
举报
0/150
提交
取消