【问题标题】:How to display Gujarati text in the JTable cell?如何在 JTable 单元格中显示古吉拉特语文本?
【发布时间】:2020-05-08 07:08:45
【问题描述】:

我的代码是这样的:

这是用于根据搜索创建JTable 的代码。

public void myMethod(){
    table_6 = new JTable(dataModel);
    JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setBounds(16, 170, 1000, 300);
    frame.getContentPane().add(scrollPane);
    scrollPane.setViewportView(table_6);
    scrollPane.setVisible(true);
    table_6.setFillsViewportHeight(true);
    table_6.getTableHeader().setReorderingAllowed(false);
    table_6.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    table_6.getColumnModel().setColumnSelectionAllowed(false);
    table_6.getTableHeader().setResizingAllowed(false);
}

【问题讨论】:

  • 您需要一种能够呈现字符的字体。设置字体是一项足够常见的任务
  • scrollPane.setBounds(16, 170, 1000, 300); 1) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。在String 中硬编码一些古吉拉特语文本。

标签: java swing fonts internationalization jtable


【解决方案1】:

问题在于提供支持渲染相关语言字形的Font,或者在系统上找到兼容的字体。

后者的示例,用于这台机器(安装了 250 种字体)。

注意:不要只选择其中一种字体。它可能在用户的计算机上不可用。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GujaratiText {

    private JComponent ui = null;
    String text = "તરસ્યો કૂતરો સાદડી પર બેસે છે";

    GujaratiText() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout(0,2,10,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        String[] fontFamilies = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for(String fontFamily: fontFamilies) {
            Font font = new Font(fontFamily, Font.PLAIN, 20);
            if (font.canDisplayUpTo(text)<0) {
                JLabel label = new JLabel(text);
                label.setFont(font);
                ui.add(label);
                ui.add(new JLabel(fontFamily));
            }
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                GujaratiText o = new GujaratiText();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

  • 很高兴你把它整理好了。 :) 如果它有助于解决问题,请accept the answer
猜你喜欢
  • 1970-01-01
  • 2017-04-17
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
相关资源
最近更新 更多