zoukankan      html  css  js  c++  java
  • java

    package day02;
    
    import java.awt.BorderLayout;
    
    public class Lucky extends JFrame {
    
        private JPanel contentPane;
        private JTextField textField;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Lucky frame = new Lucky();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Lucky() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            final JTextArea textArea_1 = new JTextArea();
            textArea_1.setBounds(175, 21, 158, 205);
            contentPane.add(textArea_1);
            
            
            final JTextArea textArea = new JTextArea();
            textArea.setBounds(10, 65, 135, 161);
            contentPane.add(textArea);
            
            JButton btnNewButton_1 = new JButton("u9000u51FA");
            btnNewButton_1.setBounds(341, 186, 93, 23);
            contentPane.add(btnNewButton_1);
            
            textField = new JTextField();
            textField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyChar()!='
    ')return;
                    String name=textField.getText();
                    if(name.isEmpty())return;
                    textArea.append(name+"
    ");
                    textField.selectAll();
                }
            });
            textField.setBounds(10, 22, 135, 21);
            contentPane.add(textField);
            textField.setColumns(10);
            
            
            
            JButton btnNewButton = new JButton("u62BDu53D6");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    String perString=textArea.getText();
                    String[] arr=perString.split("
    {1,}");
                    int index=(int)(Math.random()*arr.length);
                    String out="本次抽取观众人员:
    	%1$s
    恭喜%1$s成为本次观众抽奖的大奖得主。"
                    +"
    
    我们将为%1$s颁发:
    	过期的二十箱酸奶。";
                    String info=String.format(out, arr[index]);
                    textArea_1.setText(info);
                }
            });
            btnNewButton.setBounds(341, 153, 93, 23);
            contentPane.add(btnNewButton);
            
        }
    }

    String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。

    format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。

    format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。

    显示不同转换符实现不同数据类型到字符串的转换,如图所示。

    转  换  符

    说    明 

    示    例

    %s

    字符串类型

    "mingrisoft"

    %c

    字符类型

    'm'

    %b

    布尔类型

    true

    %d

    整数类型(十进制)

    99

    %x

    整数类型(十六进制)

    FF

    %o

    整数类型(八进制)

    77

    %f

    浮点类型

    99.99

    %a

    十六进制浮点类型

    FF.35AE

    %e

    指数类型

    9.38e+5

    %g

    通用浮点类型(f和e类型中较短的)

     

    %h

    散列码

     

    %%

    百分比类型

    %n

    换行符

     

    %tx

    日期与时间类型(x代表不同的日期与时间转换符

     

    详情http://blog.csdn.net/lonely_fireworks/article/details/7962171

    还有就是关于.split(" {1,}");按照 至少1个“ ”来分割

  • 相关阅读:
    JFreeChart API 说明(转)
    杭电1064
    杭电2734
    杭电1170
    杭电1197
    杭电2629
    杭电1062
    杭电2088
    杭电1205
    杭电1004
  • 原文地址:https://www.cnblogs.com/newadi/p/5368220.html
Copyright © 2011-2022 走看看