Java图形界面
·从窗口界面输入学生信息(创建学生对象。如未录取,则显示“该生分数不足,未录取”。·点击“统计”按钮,显示已录取的所有学生信息(包括学号,姓名,分数等)。·从窗口界面输入学生信息(创建学生对象)。
·输入后,点击界面“录取”按钮,如录取,则显示对话框提示“学号XX
姓名XX已录取”。
如未录取,则显示“该生分数不足,未录取”。
·点击“统计”按钮,显示已录取的所有学生信息(包括学号,姓名,分数等)。
界面有菜单,分别有(录取,统计,退出3个菜单项),“录取”及“统计”功能和同名按钮功能一样。“退出”菜单项,关闭窗口并退出程
序。
public class student
{
public
double zong,tiyu;
String xuehao,name;
public student(String xuehao,String name,double zong,double tiyu)
{
this.zong=zong;
this.name=name;
this.xuehao=xuehao;
this.tiyu=tiyu;
}
public double getZong()
{
return zong;
}
public void setZong(double zong)
{
this.zong = zong;
}
public double getTiyu()
{
return tiyu;
}
public void setTiyu(double tiyu)
{
this.tiyu = tiyu;
}
public String getXuehao()
{
return xuehao;
}
public void setXuehao(String xuehao)
{
this.xuehao = xuehao;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
这部分为学生类的创建包括变量的set和get方法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import javax.swing.JOptionPane;
public class chuangkoss
{
public static void main(String args[])
{
new MYWin ();
}
}
class MYWin extends JFrame
{
student[] a1=new student[6];
private static final long serialVersionUID = -5195884673007466869L;
JTextField atext,btext,ctext,dtext;
JButton a,b,c;
JDialog lu;
JTextArea q1;
int i=0;
Box baseBox,boxV1,boxV2,boxV3;
MYWin ()
{
boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("总成绩"));
boxV1.add(Box.createVerticalStrut(40));
boxV1.add(new JLabel("体育"));
boxV1.add(Box.createVerticalStrut(40));
boxV1.add(new JLabel("学号"));
boxV1.add(Box.createVerticalStrut(40));
boxV1.add(new JLabel("姓名"));
atext=new JTextField(10);
btext=new JTextField(10);
ctext=new JTextField(10);
dtext=new JTextField(10);
a=new JButton("录取");
b=new JButton("统计");
c=new JButton("退出");
boxV2=Box.createVerticalBox();
boxV2.add(atext);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(btext);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ctext);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(dtext);
boxV3=Box.createVerticalBox();
boxV3.add(a);
boxV3.add(Box.createVerticalStrut(8));
boxV3.add(b);
boxV3.add(Box.createVerticalStrut(8));
boxV3.add(c);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV3);
JTextArea q1=new JTextArea (10,20);
setLayout(new FlowLayout());
add(baseBox);
add(q1);
validate();
setBounds(100,100,600,250);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String s1 = atext.getText();
String s2 = btext.getText();
String s3 = ctext.getText();
String s4 = dtext.getText();
try
{
Double b1 = Double.parseDouble(s1);
Double b2 = Double.parseDouble(s2);
student temp = new student(s3,s4,b1,b2);
if(temp.getZong()>300)
{
JOptionPane.showMessageDialog(null, temp.name+temp.xuehao+"恭喜您被录取");
a1[++i] = temp;
}
else
if(temp.zong>290&&temp.tiyu>95)
{
JOptionPane.showMessageDialog(null, temp.name+temp.xuehao+"恭喜您被录取");
}
else
{
JOptionPane.showMessageDialog(null, "很抱歉,您未被录取");
}
}
catch(NullPointerException ee)
{
System.out.println("空指针异常 22");
}
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{for(int j=i;j>0;j--)
{
q1.append(a1[j].name+"被录取") ;
q1.append("\n") ;
}
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
}
这里为窗口部分的创建以及功能的实现