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