JAVA swing学生信息系统(最初版)

1 定义学生类: 
 (1)成员变量:学号(String),姓名(String),专业(String)。
 (2)构造方法:已知学号,姓名,专业创建学生对象。 
 (3) 编写获取学生信息的方法public String info(),学生信息格式要求如下: 学号***,姓名***,专业***。
(4)在main方法中测试你写的类: 创建三个学生对象,并输出他们的信息。

 2、定义矩形类:
 (1)成员变量:矩形长度和宽度(整数)。 
 (2)构造方法:已知长宽创建矩形对象。
 (3) 编写计算周长和面积的两个方法。 
 (4)在main方法中测试你写的类: 创建一个矩形对象,并输出它的周长和面积。


代码:作业1:

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

class Students {
  String NO,name,major,grade;
  public Students(String NO,String name,String major,String grade) {
    this.NO=NO;
    this.name=name;
    this.major=major;
    this.grade=grade;
  }
  public String info() {
    return "学号:\t"+this.NO+"\n姓名:\t"+this.name+"\n专业班级:\t"+this.major+this.grade+"\n";
  }

}

public class Main
{
   public static void main(String[] args) {
          JFrame yuheng = new JFrame("作业1.1 学生类 -by于衡");
          yuheng.setSize(350, 600);
          yuheng.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 40));
          Label stuNO = new Label("学号:");
          yuheng.add(stuNO);
          final TextField tfNO = new TextField(30);
          yuheng.add(tfNO);
          
          Label stuName = new Label("姓名:");
          yuheng.add(stuName);
          final TextField tfName = new TextField(30);
          yuheng.add(tfName);
          
          Label stuMajor = new Label("专业:");
          yuheng.add(stuMajor);
          final TextField tfMajor = new TextField(30);
          yuheng.add(tfMajor);
          
          Label stuClass = new Label("班级:");
          yuheng.add(stuClass);
          final TextField tfClass = new TextField(30);
          yuheng.add(tfClass);
          
          Button printt = new Button("输出学生信息");
          yuheng.add(printt);
          Button quitt = new Button("退出系统");
          yuheng.add(quitt);
          final TextArea info = new TextArea(15,40);
          yuheng.add(info);

          printt.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
              	Students student=new Students(tfNO.getText().trim(), tfName.getText().trim(), tfMajor.getText().trim(),tfClass.getText().trim());
                  info.append("输出学生信息\n"+student.info());
                  tfNO.setText("");
                  tfMajor.setText("");
                  tfName.setText("");
                  tfClass.setText("");
              }
          });
          quitt.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                  System.exit(0);
              }
          });
          yuheng.setVisible(true);
      }
}

代码:作业2

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

class rectangle {
  int length,wide;
  public rectangle(int length,int wide) {
    this.length=length;
    this.wide=wide;
  }
  public int getPerimeter() {
    return 2*length+2*wide;
  }
  public int getArea() {
    return length*wide;
  }
}

public class Main
{
   public static void main(String[] args) {
          JFrame yuheng = new JFrame("作业1.2 矩形类 -by于衡");
          yuheng.setSize(350, 450);
          yuheng.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 40));
          Label len = new Label("长度:");
          yuheng.add(len);
          final TextField tfLen = new TextField(30);
          yuheng.add(tfLen);
          
          Label wide = new Label("宽度:");
          yuheng.add(wide);
          final TextField tfWide = new TextField(30);
          yuheng.add(tfWide);
         
          Button printt = new Button("输出矩形信息");
          yuheng.add(printt);
          Button quitt = new Button("退出系统");
          yuheng.add(quitt);
          final TextArea info = new TextArea(10,40);
          yuheng.add(info);

          printt.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
              	rectangle rec=new rectangle(Integer.parseInt(tfLen.getText()),Integer.parseInt(tfWide.getText()));
                  info.append("输入程序中的矩形参数\n");
                  info.append("长为:"+rec.length+"\t宽为:"+rec.wide);
                  info.append("\n该矩形的周长和面积为:\n");
                  info.append("周长:"+rec.getPerimeter()+"\n面积为:"+rec.getArea()+"\n");
                  tfLen.setText("");
                  tfWide.setText("");
              }
          });
          quitt.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                  System.exit(0);
              }
          });
          yuheng.setVisible(true);
      }
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

3 × 1 =