人品計算機 JFrame 表單軟體版 JPanel JTextField JTextArea JButtton JLabel setContentPane

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.