⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableprintdemo1.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * <p>     * This is protected so that a subclass can return an instance     * of a different {@code JTable} subclass. This is interesting     * only for {@code TablePrintDemo3} where we want to return a     * subclass that overrides {@code getPrintable} to return a     * custom {@code Printable} implementation.     */    protected JTable createTable(TableModel model) {        return new JTable(model);    }    /**     * Create and return a cell renderer for rendering the pass/fail column.     * This is protected so that a subclass can further customize it.     */    protected TableCellRenderer createPassedColumnRenderer() {        return new PassedColumnRenderer();    }    /**     * Print the grades table.     */    private void printGradesTable() {        /* Fetch printing properties from the GUI components */        MessageFormat header = null;                /* if we should print a header */        if (headerBox.isSelected()) {            /* create a MessageFormat around the header text */            header = new MessageFormat(headerField.getText());        }        MessageFormat footer = null;                /* if we should print a footer */        if (footerBox.isSelected()) {            /* create a MessageFormat around the footer text */            footer = new MessageFormat(footerField.getText());        }        boolean fitWidth = fitWidthBox.isSelected();        boolean showPrintDialog = showPrintDialogBox.isSelected();        boolean interactive = interactiveBox.isSelected();        /* determine the print mode */        JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH                                         : JTable.PrintMode.NORMAL;        try {            /* print the table */            boolean complete = gradesTable.print(mode, header, footer,                                                 showPrintDialog, null,                                                 interactive, null);            /* if printing completes */            if (complete) {                /* show a success message */                JOptionPane.showMessageDialog(this,                                              "Printing Complete",                                              "Printing Result",                                              JOptionPane.INFORMATION_MESSAGE);            } else {                /* show a message indicating that printing was cancelled */                JOptionPane.showMessageDialog(this,                                              "Printing Cancelled",                                              "Printing Result",                                              JOptionPane.INFORMATION_MESSAGE);            }        } catch (PrinterException pe) {            /* Printing failed, report to the user */            JOptionPane.showMessageDialog(this,                                          "Printing Failed: " + pe.getMessage(),                                          "Printing Result",                                          JOptionPane.ERROR_MESSAGE);        }    }    /**     * Start the application.     */    public static void main(final String[] args) {        /* Schedule for the GUI to be created and shown on the EDT */        SwingUtilities.invokeLater(new Runnable() {            public void run() {                /* Don't want bold fonts if we end up using metal */                UIManager.put("swing.boldMetal", false);                try {                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());                } catch (Exception e) {                }                new TablePrintDemo1().setVisible(true);            }        });    }    /**     * A table model containing student grades.     */    private static class GradesModel implements TableModel {        private final Object[][] GRADES = {            {"Agnew", "Kieran", 80, 74, 75, 93},            {"Albertson", "Jack", 90, 65, 88, 79},            {"Anderson", "Mischa", 34, 45, 48, 59},            {"Andrews", "Danielle", 50, 56, 55, 44},            {"Baja", "Ron", 32, 23, 55, 67},            {"Barry", "Douglas", 46, 66, 77, 90},            {"Davis", "Lacy", 99, 100, 98, 97},            {"Egelstein", "Harold", 34, 58, 76, 89},            {"Elens", "Xavier", 35, 66, 49, 47},            {"Elmer", "Thomas", 50, 32, 51, 60},            {"Farras", "Elena", 77, 51, 75, 55},            {"Filison", "Paulo", 88, 87, 77, 52},            {"Gabon", "Parvati", 9, 15, 35, 86},            {"Hickey", "Shannon", 95, 89, 95, 100},            {"Ingles", "Jaime", 75, 65, 55, 95},            {"Instein", "Payton", 51, 56, 51, 84},            {"Jackson", "Donald", 94, 78, 97, 13},            {"Jefferson", "Lynn", 21, 51, 20, 74},            {"Johnson", "Tanya", 11, 52, 80, 48},            {"Kimble", "James", 18, 50, 90, 54},            {"Kolson", "Laura", 98, 88, 97, 99},            {"Leigh", "Tasha", 85, 78, 48, 100},            {"Lombardi", "Leonard", 68, 54, 97, 24},            {"Manning", "Havvy", 59, 54, 9, 18},            {"McNichol", "Vivian", 88, 99, 58, 87},            {"Michaels", "Daniel", 97, 95, 54, 99},            {"Nicholson", "Alex", 24, 100, 55, 100},            {"Nimble", "Tonya", 41, 33, 33, 81},            {"Onning", "Wehhoh", 45, 65, 32, 24},            {"Opals", "Diamond", 98, 59, 12, 11},            {"Osser", "Michael", 55, 54, 31, 87},            {"Paton", "Geoff", 68, 22, 31, 80},            {"Plumber", "Ester", 58, 20, 28, 98},            {"Robbins", "Noah", 99, 12, 87, 64},            {"Robinson", "Jenny", 65, 10, 98, 66},            {"Ronald", "Dmitri", 25, 9, 98, 61},            {"Sabo", "Polly", 20, 68, 82, 50},            {"Silverstone", "Dina", 31, 62, 54, 58},            {"Singleton", "Alyssa", 50, 30, 98, 88},            {"Stevens", "Cameron", 89, 8, 81, 56},            {"Talbert", "Will", 34, 80, 81, 84},            {"Trimble", "Vicky", 58, 65, 98, 54},            {"Tussle", "Paulo", 55, 55, 88, 55},            {"Umber", "Gus", 90, 87, 85, 55},            {"Valhalla", "Yohan", 60, 77, 62, 89},            {"Viola", "Michel", 31, 84, 62, 68},            {"Wanderer", "Sean", 24, 51, 85, 95},            {"West", "North", 88, 23, 81, 87},            {"Xavier", "Kerry", 91, 99, 24, 84},            {"Xu", "Richard", 99, 58, 20, 24},            {"Ying", "Lina", 85, 99, 89, 90},            {"Yee", "Tong", 95, 65, 74, 64},        };        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {}        public void addTableModelListener(TableModelListener l) {}        public void removeTableModelListener(TableModelListener l) {}        public boolean isCellEditable(int rowIndex, int columnIndex) {            return false;        }        public Class<?> getColumnClass(int col) {            switch(col) {                case 0:                case 1:                    return String.class;                case 2:                case 3:                case 4:                case 5:                case 6:                    return Integer.class;                case 7:                    return Boolean.class;            }            throw new AssertionError("invalid column");        }                public int getRowCount() {            return GRADES.length;        }                public int getColumnCount() {            return 8;        }                public String getColumnName(int col) {            switch(col) {                case 0: return "Last Name";                case 1: return "First Name";                case 2: return "Assign. 1";                case 3: return "Midterm";                case 4: return "Assign. 2";                case 5: return "Exam";                case 6: return "Final";                case 7: return "Passed";            }                        throw new AssertionError("invalid column");        }                public Object getValueAt(int row, int col) {            switch(col) {                case 0:                case 1:                case 2:                case 3:                case 4:                case 5:                    return GRADES[row][col];                case 6:                case 7:                    int fin = 0;                    fin += (Integer)GRADES[row][2];                    fin += (Integer)GRADES[row][3];                    fin += (Integer)GRADES[row][4];                    fin += (Integer)GRADES[row][5];                    fin = Math.round((fin / 4.0f));                    if (col == 6) {                        return fin;                    } else {                        return fin > 50;                    }            }            throw new AssertionError("invalid column");        }    }    /**     * A custom cell renderer for rendering the "Passed" column.     */    protected static class PassedColumnRenderer extends DefaultTableCellRenderer {            public Component getTableCellRendererComponent(JTable table,                                                           Object value,                                                           boolean isSelected,                                                           boolean hasFocus,                                                           int row,                                                           int column) {            super.getTableCellRendererComponent(table, value, isSelected,                                                hasFocus, row, column);            setText("");            setHorizontalAlignment(SwingConstants.CENTER);            /* set the icon based on the passed status */            boolean status = (Boolean)value;            setIcon(status ? passedIcon : failedIcon);            return this;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -