Swing JList 字体宽度

发布于 2024-10-04 08:57:42 字数 120 浏览 7 评论 0原文

我正在制作一个java程序,在列表框中生成一个接收器,它将显示商品数量、商品名称和商品价格。我需要填充字符串,使名称在中间变得蓬乱,而项目数量和成本位于以太侧。你能找到字符串的像素,然后我可以计算达到所需格式所需的空格数。谢谢

I am making a java program with produces a recept in a listbox, it will display the number of items, the item name and the price of the item. I need to pad the string so that the name is ruffly in the middle and the number of items and the cost are at ether side. Can you find the with in pixels of the strings then I can calculate the number of spaces needed to acheive the desired format. Thanks

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

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

发布评论

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

评论(1

第七度阳光i 2024-10-11 08:57:42

这就是获取字符串宽度的方法:

Graphics2D g2d = (Graphics2D)g;
FontMetrics fontMetrics = g2d.getFontMetrics();

int width = fontMetrics.stringWidth("aString");
int height = fontMetrics.getHeight();

...

但是,当我再次阅读您的问题时,我想,为什么不使用 ListCellRendererJList?它可以按您想要的方式工作:

http://img189.imageshack.us/img189/7509/ jlistexample.jpg

这是它的代码:

public static void main(String... args) {

    JFrame frame = new JFrame("Test");

    JList list = new JList(new String[] { 
            "Hello", "World!", "as", "we", "know", "it" });

    list.setCellRenderer(new ListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(
                JList list, 
                Object value,
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) {

            JPanel panel = new JPanel(new GridBagLayout());

            if (isSelected)
                panel.setBackground(Color.LIGHT_GRAY);

            panel.setBorder(BorderFactory.createMatteBorder(
                    index == 0 ? 1 : 0, 1, 1, 1, Color.BLACK));

            GridBagConstraints gbc = new GridBagConstraints();

            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4,4,4,4);

            // index
            gbc.weightx = 0;
            panel.add(new JLabel("" + index), gbc);

            // "name"
            gbc.weightx = 1;
            panel.add(new JLabel("" + value), gbc);

            // cost
            gbc.weightx = 0;
            String cost = String.format("$%.2f", Math.random() * 100);
            panel.add(new JLabel(cost), gbc);


            return panel;
        }
    });

    frame.add(list);

    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

This is how you get the width of a string:

Graphics2D g2d = (Graphics2D)g;
FontMetrics fontMetrics = g2d.getFontMetrics();

int width = fontMetrics.stringWidth("aString");
int height = fontMetrics.getHeight();

...

But, as I read your question again I tought, why not use the ListCellRenderer in JList? It works as you want:

http://img189.imageshack.us/img189/7509/jlistexample.jpg

And here is the code for it:

public static void main(String... args) {

    JFrame frame = new JFrame("Test");

    JList list = new JList(new String[] { 
            "Hello", "World!", "as", "we", "know", "it" });

    list.setCellRenderer(new ListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(
                JList list, 
                Object value,
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) {

            JPanel panel = new JPanel(new GridBagLayout());

            if (isSelected)
                panel.setBackground(Color.LIGHT_GRAY);

            panel.setBorder(BorderFactory.createMatteBorder(
                    index == 0 ? 1 : 0, 1, 1, 1, Color.BLACK));

            GridBagConstraints gbc = new GridBagConstraints();

            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4,4,4,4);

            // index
            gbc.weightx = 0;
            panel.add(new JLabel("" + index), gbc);

            // "name"
            gbc.weightx = 1;
            panel.add(new JLabel("" + value), gbc);

            // cost
            gbc.weightx = 0;
            String cost = String.format("$%.2f", Math.random() * 100);
            panel.add(new JLabel(cost), gbc);


            return panel;
        }
    });

    frame.add(list);

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