码迷,mamicode.com
首页 > Windows程序 > 详细

人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane

时间:2017-10-10 21:44:04      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:def   string   panel   大小   可见   textfield   tcl   out   textarea   

import java.awt.BorderLayout;
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_Frame2 extends JFrame {
    private static final long serialVersionUID = 1L;

    public RP_Frame2() {
        setBounds(200, 200, 500, 300);// 设置窗体大小位置
        setTitle("人品计算器");// 设置窗体标题

        JPanel pnBasic = new JPanel();// 生成一个大画布
        setContentPane(pnBasic);// 放在窗体中
        pnBasic.setLayout(new GridLayout(2, 1));// 画布按照两行一列网格布局,行与行列与列间隔5像素
        JPanel pnGreen = new JPanel();// 再生成一个小绿画布
        JPanel pnYellow = new JPanel();// 再生成一个小黄画布
        pnYellow.setBackground(Color.YELLOW);// 画布设置颜色
        pnGreen.setBackground(Color.GREEN);// 画布设置颜色
        pnBasic.add(pnYellow);
        pnBasic.add(pnGreen);
        // 下边一行绿色画布增加标签,作为输出
        JLabel result = new JLabel();
        pnGreen.add(result);
        result.setText("输入姓名后,  点击 ‘测试人品‘ 按钮, 查看人品值!");
        // 上边一行黄色画布重新布局
        pnYellow.setLayout(new BorderLayout());
        JLabel label = new JLabel();// 生成标签
        label.setText("输入姓名");
        pnYellow.add(label, BorderLayout.WEST);// 放到Yellow画布左边
        label.setBackground(Color.YELLOW);
        JTextField text = new JTextField(15);// 生成长度15的文本框
        text.setBackground(Color.YELLOW);
        pnYellow.add(text, BorderLayout.CENTER);
        JButton btn = new JButton();// 生成按钮
        btn.setBackground(Color.YELLOW);
        pnYellow.add(btn, BorderLayout.EAST);// 放到Yellow画布右边
        btn.setText("测试人品");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random ran = new Random();
                int index = ran.nextInt(101);
                if (index >= 90) {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "大神");
                } else if (index >= 80) {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "大牛");
                } else if (index >= 60) {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "程序猿");
                } else if (index >= 40) {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "码农");
                } else if (index >= 20) {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "码畜");
                } else {
                    result.setText(text.getText() + " 你的人品值为 " + index + " 等级为 " + "菜鸟");
                }
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗体时程序停止运行
        setVisible(true);// 设置窗体可见,否则什么都不会显示
    }

    public static void main(String[] args) {
        new RP_Frame2();
    }

}

 

人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane

标签:def   string   panel   大小   可见   textfield   tcl   out   textarea   

原文地址:http://www.cnblogs.com/qingyundian/p/7647766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!