帮同学写了一个公选课要交的作业:
题目1、显示一个三位整数的各位数字
输入一个整数,分别显示其百位、十位和个位数字,图形用户界面如图所示。要求:整数文本框可编辑,其他文本框仅用于显示不可编辑;整数文本框响应文本编辑事件;当输入数据错误时,处理异常,弹出对话框,提示重新输入信息。
题目2、定义一个父类Person,属性包含姓名、地址、城市、国家、邮编,方法包括构造方法、分别设置各个属性、分别返回各个属性、显示所有属性。Person子类有两个:学生类和教师类。
学生类自己的属性包括主修专业、学号、年级、成绩(成绩每名同学包括3门成绩),增加的方法包括设置和返回增加的属性,显示方法以及构造方法,
教师类自己的属性包括部门、收入,增加的方法包括设置和返回增加的属性,显示方法以及构造方法。
其中第一个可以运行,毕竟硬指标在这里检验,但是写的第二个就不知道符合要求了不,只是尽量这么写。
两题代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class DisplayTest extends JFrame implements ActionListener {
public static final int WIDTH = 240;
public static final int HEIGHT = 195;
public static final int COLUMN=25;
private Container container;
private JTextField infTF;
private JPanel panelD;//输入数字所在面板
private JPanel panelB;//百位数所在面板
private JPanel panelS;//十位数所在面板
private JPanel panelG;//个位数所在面板
private JPanel panel;
private JLabel labelD;
private JLabel labelB;
private JLabel labelS;
private JLabel labelG;
private JTextField tfD;// 输入数字的文本框
private JTextField tfB;// 百位数的文本框
private JTextField tfS;
private JTextField tfG;
private JButton button_ok;
private JButton button_quit;
public DisplayTest() {
super("三位数显示");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// 把外观设置成所使用的平台的外观,即这个程序在哪个平台运行,显示的窗口,对话框外观将是哪个平台的外观.
} catch (Exception e) {
}
setSize(WIDTH, HEIGHT);
this.setBackground(Color.lightGray);
infTF = new JTextField("@SCUEC 计科院09自动化 *** ");
infTF.setEditable(false);
container = getContentPane();
panelD=new JPanel();
panelB=new JPanel();
panelS=new JPanel();
panelG=new JPanel();
panel=new JPanel();
labelD = new JLabel("整数");
labelB = new JLabel("百位");
labelS = new JLabel("十位");
labelG = new JLabel("个位");
tfD = new JTextField(COLUMN);
tfB = new JTextField(COLUMN);
tfB.setEditable(false);
tfS = new JTextField(COLUMN);
tfS.setEditable(false);
tfG = new JTextField(COLUMN);
tfG.setEditable(false);
button_ok=new JButton("确定");
button_ok.addActionListener(this);
button_quit=new JButton("退出");
button_quit.addActionListener(this);
setLayout(new GridLayout(6, 1));
panelD.add(labelD);
panelD.add(tfD);
panelB.add(labelB);
panelB.add(tfB);
panelS.add(labelS);
panelS.add(tfS);
panelG.add(labelG);
panelG.add(tfG);
panel.add(button_ok);
panel.add(button_quit);
container.add(panelD);
container.add(panelB);
container.add(panelS);
container.add(panelG);
container.add(panel);
container.add(infTF, BorderLayout.SOUTH);
Dimension frameSize = getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource()==button_ok){
try{
int i = Integer.parseInt(tfD.getText());
if(i>999||i<100){
JOptionPane.showMessageDialog(container, "输入错误,请重新输入一个三位数");
tfD.setText("");
}else{
tfB.setText("" + (i /100));
tfS.setText("" + (i /10 % 10));
tfG.setText("" + i % 10);
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(this,"\""+tfD.getText()+"\" 不能转换成整数,请重新输入!");
tfD.setText("");
}
}
if(event.getSource()==button_quit){
System.exit(0);
}
}
public static void main(String[] args) {
new DisplayTest();
}
}
效果如图:



public class PersonTest {
public static void main(String[] args) {
Person[] people = new Person[2];
people[0] = new Teacher("Yanghai", "minyuanroad 708#", "Wuhan",
"China", 430074, "Science Computer", 100000);
people[1] = new Student("Lirentian", "minyuanroad 708#", "Wuhan",
"China", 430074, "automic", "09064047", 2009, 90, 89, 91);
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription()
+ p.getCity() + "," + p.getCountry() + "," + p.getCode());
}
}
abstract class Person {
public Person(String aName, String aAddress, String aCity, String aCountry,
int aCode) {
name = aName;
address = aAddress;
city = aCity;
country = aCountry;
code = aCode;
}
public abstract String getDescription();
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public int getCode() {
return code;
}
private String name;
private String address;
private String city;
private String country;
private int code;
}
class Teacher extends Person {
public Teacher(String n, String ad, String city, String country, int code,
String dep, double s) {
super(n, ad, city, country, code);
department = dep;
salary = s;
}
public double getSalary() {
return salary;
}
public String getDepartment() {
return department;
}
public String getDescription() {
return String.format("an employee with a salary of $%.2f", salary)
+ " at " + department + " department,SCUEC ";
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
private double salary;
private String department;
}
class Student extends Person {
public Student(String n, String ad, String city, String country, int code,
String m, String num, int g, int s1, int s2, int s3) {
// pass n to superclass constructor
super(n, ad, city, country, code);
major = m;
number = num;
grade = g;
score1 = s1;
score2 = s2;
score3 = s3;
}
public String getDescription() {
return "a student majoring in " + major + "," + "he is at grade "
+ grade + ",and his number is " + number
+ ", his three course's score:" + "\"" + score1 + ", " + score2
+ ", " + score3 + "\"" + "\n" + " at SCUEC,";
}
private String major;
private String number;
private int grade;
private int score1, score2, score3;
}
本文介绍了一个Java GUI应用程序,用于展示用户输入的三位整数的各位置数字,并实现了一个面向对象的设计案例,包括一个抽象基类Person及其实现为学生和教师的具体类。
5万+

被折叠的 条评论
为什么被折叠?



