为了账号安全,请及时绑定邮箱和手机立即绑定

java中%1$s什么意思啊?

java中%1$s什么意思啊?

潇湘沐 2019-07-15 08:04:48
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);}}}
查看完整描述

2 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

被格式化的参数索引%1$s和%2$s分别表示第一位和第二位占位符,$s表示是字符串

查看完整回答
反对 回复 2019-07-16
  • 2 回答
  • 0 关注
  • 1030 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信