下面是我的第一个GUI界面的小程序,仅供玩赏。
源代码如下:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class RenPing extends JFrame{
private JTextField JTF1=new JTextField(15); //姓名;
private JLabel JTF2=new JLabel(); //得分
private JTextArea JA=new JTextArea("请谨言慎行!");
private String s1="您要谨言慎行,您的人品太低,典型的人渣型败类,得加强自身的身心修养。";
private String s2="一般,做个人还行,不过不要太骄纵,否则惹火上身。";
private String s4="不错,您的品行不错,可以为廉官一品,但因些许毛病,需要自勉喽。";
private String s3="还可以,但做事得认真仔细,否则小心被领导 K 喽。";
private String s5="very good,您可以被称为再世孔孟,称您圣贤一点也不为过!";
public RenPing(){
Container c=getContentPane();
c.setLayout(new GridLayout(3,1,3,3));
JPanel JP1=new JPanel(new FlowLayout());
JPanel JP2=new JPanel(new FlowLayout());
JPanel JP3=new JPanel(new FlowLayout());
JLabel JL1=new JLabel("请输入您的姓名:");
JButton JB=new JButton("测算");
JB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str1=JTF1.getText();
if(str1.length()!=0){ //如果没有输入名字,则按钮不反应,即不测算
int s=(int)(Math.random()*100);
String ss=Integer.toString(s);
if(str1.contentEquals("周武华")){
JTF2.setText("99.99");
JA.setText(JTF1.getText()+","+s5);}
if(!str1.contentEquals("周武华")&&s<30) {
JTF2.setText(ss);
JA.setText(JTF1.getText()+","+s1);
}
if(!str1.contentEquals("周武华")&&s>=30&&s<60){
JTF2.setText(ss);
JA.setText(JTF1.getText()+","+s2);
}
if(!str1.contentEquals("周武华")&&s>=60&&s<80){
JTF2.setText(ss);
JA.setText(JTF1.getText()+","+s3);
}
if(!str1.contentEquals("周武华")&&s>=80&&s<99){
JTF2.setText(ss);
JA.setText(JTF1.getText()+","+s2);
}
}
//当单击“测算”按钮后,清空输入框中的内容。
JTF1.setText("");
}
});
JP1.add(JL1); JP1.add(JTF1); JP1.add(JB);
JLabel JL2=new JLabel("您的人品得分:");
JP2.add(JL2); JP2.add(JTF2);
JLabel JL3=new JLabel("给您的忠告:");
JP3.add(JL3); JP3.add(JA);
c.add(JP1); c.add(JP2); c.add(JP3);
setVisible(true);
setBounds(150, 150, 500, 300);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("测测你的人品");
}
public static void main(String[] args) {
new RenPing();
}
}
如:
当然这里有些小bug,细心的读者就会发现,在此不述。
评论