测RP

写一段代码测测你的RP

package day01;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RP extends JFrame{
    private JTextField txtInput;
    private JLabel lbResult;
    public RP(){
        JPanel p=new JPanel();
        p.setColor.GREEN);
         GridLayout lyGird = new GridLayout(2,1);
        p.setLayout(lyGird);
        JPanel g=new JPanel();
        g.setColor.GREEN);
         JLabel x = new JLabel("请输入姓名:");
        txtInput = new JTextField(15);
        JButton btnCommit = new JButton("提交");
        ButtonActionListener btn=new ButtonActionListener();
        btnCommit.addActionListener(btn);
        JPanel y=new JPanel();
        g.add(x);
        g.add(txtInput);
        g.add(btnCommit);
        y.setColor.YELLOW);
         lbResult = new JLabel();
        lbResult.setText("等待用户输入姓名。。。");
        y.add(lbResult);
        p.add(g);
        p.add(y);
        setContentPane(p);
        setTitle("我的小窗体");
        setBounds(200, 200, 400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setResizable(false);
        setVisible(true);
    }
    private class ButtonActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String strName = txtInput.getText();
            if(strName.trim().isEmpty() || strName.trim().length()==0 || strName.trim().equals("")){
                return;
            }
            Random ran = new Random();
            int niRan=ran.nextInt(100)+1;
            String strLV="";
            if(strName.trim().contains("张")){
                niRan=0;
                strLV="畜生都不如";
            }else if(niRan>=80){
                strLV="好人";
            }else if(niRan>=60){
                strLV = "还行";
                      
            }else if(niRan>=30){
                strLV = "弱爆了";
            }else {
                strLV = "可以去死了";
            }
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.append(strName).append(",你的人品值为:").append(niRan).append(",评语为:").append(strLV);
            lbResult.setText(strBuilder.toString());
            txtInput.setText("");
        }
              
    }
    public static void main(String[] args) {
        new RP();
    }
}

结果显示:


你可能感兴趣的:(java)