栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

欢迎创建我的Swing组件的建议

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

欢迎创建我的Swing组件的建议

我喜欢您引用的Grouchnikov文章,但不确定是否要更改UI委托。因为这将是一个不可变对象的视图,所以我倾向于使用组合而不是继承。我倾向于想到您作为渲染器描述的组件,如本例所示。您可以添加

InputVerifier
DocumwntListener
以获得所需的验证。

附录:这是一个使用

JFormattedTextField
和的示例
MaskFormatter
。您需要调整格式掩码以匹配您的比例和长度。

public class TableGrid extends JPanel {    private DecimalFormat df;    private MaskFormatter mf;    private JFormattedTextField tf;    public TableGrid() {        df = new DecimalFormat("0.00");        try { mf = new MaskFormatter("#.##");        } catch (ParseException ex) { ex.printStackTrace();        }        tf = new JFormattedTextField(mf);        TableModel dataModel = new TableModel();        JTable table = new JTable(dataModel);        table.setCellSelectionEnabled(true);        table.setRowHeight(32);        table.setDefaultRenderer(BigDecimal.class, new DecRenderer(df));        table.setDefaultEditor(BigDecimal.class, new DecEditor(tf, df));        this.add(table);    }    private static class TableModel extends AbstractTableModel {        private static final int SIZE = 4;        private BigDecimal[][] matrix = new BigDecimal[SIZE][SIZE];        public TableModel() { for (Object[] row : matrix) {     Arrays.fill(row, BigDecimal.valueOf(0)); }        }        @Override        public int getRowCount() { return SIZE;        }        @Override        public int getColumnCount() { return SIZE;        }        @Override        public Object getValueAt(int row, int col) { return matrix[row][col];        }        @Override        public void setValueAt(Object value, int row, int col) { matrix[row][col] = (BigDecimal) value;        }        @Override        public Class<?> getColumnClass(int col) { return BigDecimal.class;        }        @Override        public boolean isCellEditable(int row, int col) { return true;        }    }    private static class DecRenderer extends DefaultTableCellRenderer {        DecimalFormat df;        public DecRenderer(DecimalFormat df) { this.df = df; this.setHorizontalAlignment(JLabel.CENTER); this.setBackground(Color.lightGray); this.df.setParseBigDecimal(true);        }        @Override        protected void setValue(Object value) { setText((value == null) ? "" : df.format(value));        }    }    private static class DecEditor extends DefaultCellEditor {        private JFormattedTextField tf;        private DecimalFormat df;        public DecEditor(JFormattedTextField tf, DecimalFormat df) { super(tf); this.tf = tf; this.df = df; tf.setHorizontalAlignment(JFormattedTextField.CENTER);        }        @Override        public Object getCellEditorValue() { try {     return new BigDecimal(tf.getText()); } catch (NumberFormatException e) {     return BigDecimal.valueOf(0); }        }        @Override        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { tf.setText((value == null) ? "" : df.format((BigDecimal) value)); if (isSelected) tf.selectAll(); return tf;        }    }    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     Jframe f = new Jframe("TableGrid");     f.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);     f.add(new TableGrid());     f.pack();     f.setVisible(true); }        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号