• 人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane Swing包(用户界面工具包)


    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();//别忘了生成窗体框架对象
        }
    
    }

    容器类 非容器类 属性类

    setTitle("人品计算器");// 设置窗体标题

    可以通过

     new RP_Frame2("窗体标题")

    然后构造方法中接收

    public RP_Frame2(String title) {

    然后使用super方法传给父类

    super(title);

    这种方法也可以设置窗体标题

    
    
  • 相关阅读:
    重写移动端滚动条[iScroll.js核心代码]
    利用canvas将网页元素生成图片并保存在本地
    微信小程序的拖拽、缩放和旋转手势
    设计模式之访问者(visitor)模式
    设计模式之原型(prototype)模式
    设计模式之享元(flyweight)模式
    设计模式之职责链模式
    设计模式之组合(compose)模式
    leetcode16
    校招真题练习013 找零(头条)
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7647766.html
Copyright © 2020-2023  润新知