帮同学写了一个公选课要交的作业:
题目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(); } }
效果如图: