用Java实现的简单人品测试程序

这个程序里面用到了为面板(JPanel)设置的布局管理器,以及如何获取系统的当前日期等技术,而且有一定的趣味性。读者可以自己再加一些判断,比如当计算出的人品值很低时给出趣味性的提示:“是我不好,不应该诱惑你来测试你的人品。。。”,这样的话可能会更有意思。更全面的人品测试器将在后续的文章里与大家见面。
package text;

import javax.swing.*;
import java.util.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextGraphics extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;

	protected JPanel panel=new JPanel();
    
    protected JLabel label=new JLabel("今日人品测试(满分为100分)");
    protected JTextArea displayArea=new JTextArea(15,30);
    protected JLabel info=new JLabel("请输入您的姓名");
    protected JTextField inputName=new JTextField(15);
    protected JButton beginText=new JButton("开始测试");
    
    protected Calendar date=Calendar.getInstance();//获取当期系统时间,作为计算人品你的依据
    protected int year=date.get(Calendar.YEAR);
    protected int month=date.get(Calendar.MONTH)+1;
    protected int day=date.get(Calendar.DATE);
    
    public TextGraphics(){            
        super("Text Graphics");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(300, 20, 600, 500);
        this.setVisible(true);
        this.addPanel();
        this.beginText.addActionListener(this);
        this.add(panel);
    }
    
    private void addPanel(){
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));//设置布局管理器
        panel.add(label);
        panel.add(displayArea);
        panel.add(info);
        panel.add(inputName);
        panel.add(beginText);
    }
    
    protected int calcRP(String str){
    	char[] name=str.toCharArray();
    	int sum=0;
    	for(int i=0;i<name.length;i++){
    		sum+=name[i];
    	}
    	//人品计算公式:年+月+日+SUM(姓名)%100
    	sum+=year+month+day;
    	sum%=100;
    	return sum;
    }
    
    protected void showRP(){
    	String name=inputName.getText();
    	if (name==null || name.equals("")){
    		JOptionPane.showMessageDialog(this, "请输入合法姓名,不要挑战人品测试器的权威!!");
    		return;
    	}
    	inputName.setText("");
    	displayArea.setText(name+"\n"+"您"+year+"年"+month+"月"+day+"日"+"的人品是"+"\n"+calcRP(name));
    }
    
    public void actionPerformed(ActionEvent arg0) {
    	beginText.setText("重新测试");
		showRP();
    }
    
	public static void main(String[] args) {
		TextGraphics tg=new TextGraphics();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值