本帖最后由 pzb13659008260 于 2012-12-16 00:36:16 编辑

解决方案 »

  1.   

    看得真累,帮你整理下:import java.awt.event.*;
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;class Demo extends JFrame implements ActionListener // 以继承JFrame的方式构建窗体是比较推荐的风格
    {
    private Container c;
    JLabel label1 = new JLabel("用户名"); // 定义显示为用户名的标签
    JLabel label2 = new JLabel("密     码"); // 定义显示为密码的标签
    JTextField username = new JTextField(10); // 定义一个单行文本输入框,长度(列数)为10
    JPasswordField password = new JPasswordField(10); // 定义一个密码输入框,列数为10
    JButton ok = new JButton("登陆"); // 定义一个显示为OK的按钮
    JButton cancel = new JButton("取消"); // 定义一个显示为登陆的按钮
    JLabel messageLabel = new JLabel(); // 定义一个显示取消的按钮
    public Demo() // 将对窗体的基本设置都定义在构造方法中
    {
    // 第一步,设置窗体的基本属性 setTitle("学生成绩录入系统"); // 设置窗体标题
    setSize(500, 350); // 设置窗体大小,单位是像素
    setLocation(300, 150); // 设置窗体起始位置
    setDefaultCloseOperation(EXIT_ON_CLOSE); // 设置点击关闭按钮(右上角)的默认行为 // 设置窗体的图标 ImageIcon ii = new ImageIcon("cuit-logo2.jpg"); // 从一个图形文件中建立imageIcon对象
    setIconImage(ii.getImage()); // 将该对象的图像设置为窗体图标 // 第二步:设置容器准备添加组件 c = getContentPane(); // 获取整个窗体的容器
    c.setLayout(new GridLayout(4, 1)); // 对窗体设置布局为网格型(3行1列) // 整个窗体里设置3个中间容器,格式都是JPanel JPanel jp1 = new JPanel(new FlowLayout (FlowLayout.CENTER, 15, 10));
    JPanel jp2 = new JPanel(new FlowLayout (FlowLayout.CENTER, 15, 10));
    JPanel jp3 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
    JPanel jp4 = new JPanel(new BorderLayout(10, 10)); // 定义在界面中需要使用的组件,包括标签,单行文本输入框,密码输入框,按钮 label1.setFont(new Font("", Font.BOLD, 22)); // 设置该标签的字体为黑体,大小为22
    label2.setFont(new Font("", Font.BOLD, 22)); // 设置该标签的字体为黑体,大小为22
    username.setFont(new Font("", Font.BOLD, 22)); // 设置字体为黑体,大小22
    password.setFont(new Font("", Font.BOLD, 22)); // 设置字体为黑体,大小22 ok.addActionListener(this);
    cancel.addActionListener(this); // 为按钮添加相应的监听 // 向指定的容器添加组件 jp1.add(label1);
    jp1.add(username); // 第一组容器添加一个标签和一个单行文本输入框 jp2.add(label2);
    jp2.add(password); // 第二个容器添加一个标签和一个密码输入框 jp3.add(ok);
    jp3.add(cancel); // 第三个容器添加连个按钮 jp4.add("Center", messageLabel); // 将中间容器统一添加到窗体容器(顶层容器)中 c.add(jp1);
    c.add(jp2);
    c.add(jp3);
    c.add(jp4); } public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == ok) {
    checkPassword();
    } else if (ae.getSource() == cancel) {
    exitWindow();
    }
    } private void checkPassword() {
    String username = this.username.getText().trim();
    String password = new String(this.password.getPassword());
    {
    if ((username.equals("java")) && (password.equals ("oo"))) { new LoginDemo1();
    this.dispose();
    } else {
    JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误",
    JOptionPane.ERROR_MESSAGE);
    this.setVisible(true);
    }
    }
    } // 定义检查用户名和密码的方法
    private void exitWindow() {
    this.dispose();
    System.exit(0);
    } // 关闭窗口 public static void main(String[] args) {
    Demo fTest = new Demo(); // 创建一个窗体对象
    fTest.setVisible(true); // 记住:一定要将整个窗体设置为可见
    }
    }class LoginDemo1 extends JFrame implements ActionListener // 以继承JFrame的方式构建窗体是比较推荐的风格
    {
    private Container c;
    int i = 0;
    JLabel label1 = new JLabel("学生姓名"); // 定义显示为用户名的标签
    JLabel label2 = new JLabel("平时成绩"); // 定义显示为口里的标签
    JLabel label3 = new JLabel("考试成绩"); // 定义显示为口里的标签
    JTextField stuname = new JTextField(10); // 定义一个单行文本输入框,长度(列数)为10
    JTextField comscore = new JTextField(10); // 定义一个单行文本输入框,长度(列数)为10
    JTextField tescore = new JTextField(10); // 定义一个单行文本输入框,长度(列数)为10
    JButton ok = new JButton("录入"); // 定义一个显示为录入的按钮
    JButton cancel = new JButton("取消"); // 定义一个显示为取消的按钮
    JButton output = new JButton("导出"); // 定义一个显示为导出的按钮
    JLabel messageLabel = new JLabel(); // 定义一个显示消息的标签 public LoginDemo1() // 将对窗体的基本设置都定义在构造方法中
    {
    // 第一步,设置窗体的基本属性 setTitle("学生成绩录入系统"); // 设置窗体标题
    setSize(500, 350); // 设置窗体大小
    setLocation(300, 150); // 设置窗体起始位置
    setDefaultCloseOperation(EXIT_ON_CLOSE); // 设置点击关闭按钮(右上角)的默认行为
    setVisible(true); // 设置窗体的图标 ImageIcon ii = new ImageIcon("cuit-logo2.jpg"); // 从一个图形文件中建立imageIcon对象
    setIconImage(ii.getImage()); // 将该对象的图像设置为窗体图标 // 第二步:设置容器准备添加组件 c = getContentPane(); // 获取整个窗体的容器
    c.setLayout(new GridLayout(5, 2)); // 对窗体设置布局为网格型(4行1列) // 整个窗体里设置3个中间容器,格式都是JPanel JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
    JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
    JPanel jp3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
    JPanel jp4 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
    JPanel jp5 = new JPanel(new BorderLayout(10, 10)); // 定义在界面中需要使用的组件,包括标签,单行文本输入框,密码输入框,按钮 label1.setFont(new Font("", Font.BOLD, 22)); // 设置该标签的字体为黑体,大小为22
    label2.setFont(new Font("", Font.BOLD, 22)); // 设置该标签的字体为黑体,大小为22
    label3.setFont(new Font("", Font.BOLD, 22)); // 设置该标签的字体为黑体,大小为22
    stuname.setFont(new Font("", Font.BOLD, 22)); // 设置字体为黑体,大小22
    comscore.setFont(new Font("", Font.BOLD, 22)); // 设置字体为黑体,大小22
    tescore.setFont(new Font("", Font.BOLD, 22)); // 设置字体为黑体,大小22 ok.addActionListener(this);
    cancel.addActionListener(this);
    output.addActionListener(this); // 为各个按钮添加相应的监听 // 向指定的容器添加组件 jp1.add(label1);
    jp1.add(stuname); // 第一组容器添加一个标签和一个单行文本输入框 jp2.add(label2);
    jp2.add(comscore);
    jp3.add(label3);
    jp3.add(tescore); // 第二个容器添加一个标签和一个密码输入框 jp4.add(ok);
    jp4.add(cancel);
    jp4.add(output); // 第三个容器添加连个按钮 jp5.add("Center", messageLabel); // 第四个容器添加一个 // 将中间容器统一添加到窗体容器(顶层容器)中 c.add(jp1);
    c.add(jp2);
    c.add(jp3);
    c.add(jp4);
    c.add(jp5);
    } public void actionPerformed(ActionEvent ae) {
    try {
    if (ae.getSource() == ok) {
    String name = this.stuname.getText();
    String com = this.comscore.getText();
    String tes = this.tescore.getText();
    int aa = Integer.valueOf(com);
    int bb = Integer.valueOf(tes);
    float cc = 0.30f * aa + 0.70f * bb;
    try {
    {
    if (0 <= aa && aa <= 100 && 0 <= bb && bb <= 100) {
    JOptionPane.showMessageDialog(null, "已录入", "正确",
    JOptionPane.INFORMATION_MESSAGE);
    reset();
    File f1 = new File("f:/java1.txt");
    f1.createNewFile();
    FileWriter f = new FileWriter(f1, true);
    if (cc < 100 && cc >= 80)
    f.write(name + "    " + aa + "   " + bb + "   "
    + cc + "   " + "A " + "\n");
    if (cc < 80 && cc >= 70)
    f.write(name + "    " + aa + "   " + bb + "   "
    + cc + "   " + "B " + "\n");
    if (cc < 70 && cc >= 60)
    f.write(name + "    " + aa + "   " + bb + "   "
    + cc + "   " + "C " + "\n");
    if (cc < 60 && cc >= 0)
    f.write(name + "    " + aa + "   " + bb + "   "
    + cc + "   " + "D " + "\n");
    f.close();
    } else {
    JOptionPane.showMessageDialog(null, "错误输入", "错误",
    JOptionPane.ERROR_MESSAGE);
    reset();
    this.setVisible(true);
    }
    }
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "错误输入", "错误",
    JOptionPane.ERROR_MESSAGE);
    reset();
    }
    } else if (ae.getSource() == cancel) {
    exitWindow();
    } else if (ae.getSource() == output) {
    showScore();
    } } catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(null, "错误输入", "错误",
    JOptionPane.ERROR_MESSAGE);
    reset();
    }
    } private void exitWindow() {
    this.dispose();
    System.exit(0);
    } public void showScore() {
    String s = " ";
    try {
    i++;
    FileReader rf = new FileReader("f:/java1.txt");
    BufferedReader brf = new BufferedReader(rf);
    String rs;
    while ((rs = brf.readLine()) != null) {
    s = s + rs + "\n";
    }
    brf.close();
    } catch (IOException e) {
    System.out.println(e);
    }
    {
    for (int k = 0; k < 5; k++) {
    for (int j = 0; j < i; j++) {
    String a[][] = new String[i][5];
    a[i][5] = s;
    if (Float.parseFloat(a[j][3]) < Float
    .parseFloat(a[j + 1][3])) {
    String temp = a[j][3];
    a[j][3] = a[j + 1][3];
    a[j][3] = temp;
    System.out
    .println("------------------------------------------------");
    System.out.println("姓名    平时成绩    考试成绩    综合成绩    等级");
    }
    System.out.println(a[j][k]); }
    }
    }
    } public void reset() {
    stuname.setText("");
    comscore.setText("");
    tescore.setText("");
    }}
      

  2.   

    又从文件中把数据读取出来,经过排序处理后输出?你想怎么排序?
    错误代码在下面函数:报空指针异常!而且For循环中的K<5,中的5是哪里来的?你的代码逻辑很混乱~~~ public void showScore() {
    String s = " ";
    try {
    i++;
    FileReader rf = new FileReader("r:/java1.txt");
    BufferedReader brf = new BufferedReader(rf);
    String rs;
    while ((rs = brf.readLine()) != null) {
    s = s + rs + "\n";
    }
    brf.close();
    } catch (IOException e) {
    System.out.println(e);
    }
    {
    for (int k = 0; k < 5; k++) {
    for (int j = 0; j < i; j++) {
    String a[][] = new String[i][5];
    a[i][5] = s;
    if (Float.parseFloat(a[j][3]) < Float
    .parseFloat(a[j + 1][3])) {
    String temp = a[j][3];
    a[j][3] = a[j + 1][3];
    a[j][3] = temp;
    System.out
    .println("------------------------------------------------");
    System.out.println("姓名    平时成绩    考试成绩    综合成绩    等级");
    }
    System.out.println(a[j][k]); }
    }
    }
    }