Java 标签中的文本对齐方式

发布于 2024-10-07 23:51:41 字数 162 浏览 0 评论 0原文

我正在开发一个以充满数字的表格为中心的 Java 应用程序。在某些列中,我想对齐列中的数字(当然是文本),以便小数点在单元格的中心垂直对齐。

我没有看到任何适用于这种情况的对齐常量。 Java中有没有官方的机制来处理这种情况?

预先感谢您的任何帮助。

约翰·多纳

I am developing a Java app centered on a table full of numbers. In some columns, I would like to align the numbers (text, of course) in a column so that the decimal points are aligned vertically at the center of the cells.

I don't see any of the alignment constants which apply to this sort of situation. Is there any official mechanism in Java to handle this situation?

Thanks in advance for any help.

John Doner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-10-14 23:51:41

我对此并不专业,但它确实可以像我上面提到的那样工作,通过使用 JTextPane 作为自定义单元格渲染器并将选项卡属性设置为小数点对齐。如果执行此操作,则必须注意将数据表示为字符串,并且在编辑数据时将制表符添加到数据字符串的开头。

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

@SuppressWarnings("serial")
public class TabStopDecimalAlign extends JPanel {
    private static final String[][] DATA = {{"One", "1.234"}, {"Two", "232.3223"},
    {"Three", "0.2323"}, {"Four", "12.345"}, {"Five", "10000.0"}};
    private static final String[] COLUMNS = {"Count", "Data"};
    private static final int ROW_HEIGHT = 24;
    private DecimalAlignRenderer decimalAlignRenderer = new DecimalAlignRenderer();
    private DefaultTableModel model = new DefaultTableModel(DATA, COLUMNS);
    private JTable table = new JTable(model) {
        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == 1) {
                return decimalAlignRenderer; 
            }
            return super.getCellRenderer(row, column);
        }
    };

    public TabStopDecimalAlign() {
        setLayout(new BorderLayout());
        add(new JScrollPane(table), BorderLayout.CENTER);
        table.setRowHeight(ROW_HEIGHT);
    }

    private static class DecimalAlignRenderer implements TableCellRenderer {
        private static final float POS = 40f;
        private static final int ALIGN = TabStop.ALIGN_DECIMAL;
        private static final int LEADER = TabStop.LEAD_NONE;
        private static final SimpleAttributeSet ATTRIBS = new SimpleAttributeSet();
        private static final TabStop TAB_STOP = new TabStop(POS, ALIGN, LEADER);
        private static final TabSet TAB_SET = new TabSet(new TabStop[] { TAB_STOP });

        private StyledDocument document = new DefaultStyledDocument();
        private JTextPane pane = new JTextPane(document);

        public DecimalAlignRenderer() {
            StyleConstants.setTabSet(ATTRIBS, TAB_SET);
            pane.setParagraphAttributes(ATTRIBS, false);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
            if (value == null) {
                pane.setText("\t0.0");
            } else {
                pane.setText("\t" + value.toString());
            }
            return pane;
        }


    }



    private static void createAndShowUI() {
        JFrame frame = new JFrame("TabStopDecimalAlign");
        frame.getContentPane().add(new TabStopDecimalAlign());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

I'm no pro at this, but it does work as I noted above by using a JTextPane as a custom cell renderer and setting tab attribute to be decimal-aligned. If you do this, you must take care that the data is represented as a String and that when editing data that a tab character be prepended to the beginning of the data String.

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

@SuppressWarnings("serial")
public class TabStopDecimalAlign extends JPanel {
    private static final String[][] DATA = {{"One", "1.234"}, {"Two", "232.3223"},
    {"Three", "0.2323"}, {"Four", "12.345"}, {"Five", "10000.0"}};
    private static final String[] COLUMNS = {"Count", "Data"};
    private static final int ROW_HEIGHT = 24;
    private DecimalAlignRenderer decimalAlignRenderer = new DecimalAlignRenderer();
    private DefaultTableModel model = new DefaultTableModel(DATA, COLUMNS);
    private JTable table = new JTable(model) {
        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == 1) {
                return decimalAlignRenderer; 
            }
            return super.getCellRenderer(row, column);
        }
    };

    public TabStopDecimalAlign() {
        setLayout(new BorderLayout());
        add(new JScrollPane(table), BorderLayout.CENTER);
        table.setRowHeight(ROW_HEIGHT);
    }

    private static class DecimalAlignRenderer implements TableCellRenderer {
        private static final float POS = 40f;
        private static final int ALIGN = TabStop.ALIGN_DECIMAL;
        private static final int LEADER = TabStop.LEAD_NONE;
        private static final SimpleAttributeSet ATTRIBS = new SimpleAttributeSet();
        private static final TabStop TAB_STOP = new TabStop(POS, ALIGN, LEADER);
        private static final TabSet TAB_SET = new TabSet(new TabStop[] { TAB_STOP });

        private StyledDocument document = new DefaultStyledDocument();
        private JTextPane pane = new JTextPane(document);

        public DecimalAlignRenderer() {
            StyleConstants.setTabSet(ATTRIBS, TAB_SET);
            pane.setParagraphAttributes(ATTRIBS, false);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
            if (value == null) {
                pane.setText("\t0.0");
            } else {
                pane.setText("\t" + value.toString());
            }
            return pane;
        }


    }



    private static void createAndShowUI() {
        JFrame frame = new JFrame("TabStopDecimalAlign");
        frame.getContentPane().add(new TabStopDecimalAlign());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
唯憾梦倾城 2024-10-14 23:51:41

如何使用 DecimalFormat 对象使每个数字在小数点后具有相同的位数,并右对齐列。

What about using a DecimalFormat object to make each number have the same count of digits after the decimal and just right aligning the column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文