计算每个金额的字段总和并显示

时间:2014-02-11 17:06:54

标签: java swing import applet

有一个程序可以构建一个15x20的矩阵,并在其字段中将字段从最小到最大排序。帮助实现显示每个字段总和的函数。 Java代码:

import javax.swing.JApplet;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Arrays;
import java.util.Comparator;

@SuppressWarnings("serial") 

public class MatrixApplet extends JApplet {

private int[][] matrix;
private int rows;
private int columns;
private int minVal;
private int maxVal;
private Random random;
private JTable table;

private int getBoundedRandom() {
    return random.nextInt(maxVal - minVal + 1) + minVal;
}

private void fillMatrix() { 
    for ( int i = 0; i < rows; ++i ) {
        matrix[i][columns] = 0;
        for ( int j = 0; j < columns; ++j ) {
            matrix[i][j] = getBoundedRandom();
            matrix[i][columns] += matrix[i][j];
        }
    }
}

public void init() { 
    try {
        SwingUtilities.invokeAndWait(new Runnable() { 
            public void run() { 
                random = new Random(); 
                rows = 20; 
                columns = 15;
                minVal = 0;
                maxVal = 20;

                matrix = new int[rows][columns + 1];
                fillMatrix(); 

                table = new JTable(new AbstractTableModel() {
                    public int getRowCount() { 
                        return rows;
                    }
                    public int getColumnCount() {
                        return columns;
                    }
                    public Integer getValueAt(int r, int c) {
                        return new Integer(matrix[r][c]);
                    }
                });

                JButton fillButton = new JButton("Fill");
                fillButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        fillMatrix();
                        table.repaint();
                    }
                });

                JButton sortButton = new JButton("Regularize");
                sortButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        Arrays.sort(matrix, new Comparator<int[]>() {
                            public int compare(int[] a, int[] b) {
                                return a[columns] - b[columns];
                            }
                        });
                        table.repaint();
                    }
                });

                Box buttonsBox = Box.createHorizontalBox();
                buttonsBox.add(Box.createHorizontalGlue());
                buttonsBox.add(fillButton);
                buttonsBox.add(Box.createHorizontalGlue());
                buttonsBox.add(sortButton);
                buttonsBox.add(Box.createHorizontalGlue());
                buttonsBox.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
                Box mainBox = Box.createVerticalBox(); 
                mainBox.add(table);
                mainBox.add(buttonsBox);
                getContentPane().add(mainBox);
                setSize(600, 360);
            }
        });
    }
    catch ( Exception e ) {
        e.printStackTrace();
    }
}

}

帮助实现此applet尝试,但不知道该怎么做。谢谢你的帮助!

0 个答案:

没有答案