如何在Swing JComponents中渲染分数 [英] How to rendering fraction in Swing JComponents

查看:131
本文介绍了如何在Swing JComponents中渲染分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上Swing JComponents能够以这种形式显示分数的数字 2 2/3 。如何以最好的形式绘制分数,例如2⅔?

Basically Swing JComponents are able to display numbers in fractions in this form 2 2/3. How can I paint fraction in the nicest form, for example 2⅔?

EDIT

看到我只有一种方式 JTable 里面 JSpinner 一个 TableColumn TableRow (可以模拟普通 JtextField ),其中 TableRenderer 可能是 JTextComponent 格式化的一部分通过使用Html并在 TableCellEdit 事件中将 TableEditor 转换为普通的 JFormattedTextField

as see I have only one way JTable inside JSpinner with one TableColumn and TableRow (that could simulated plain JtextField too), where TableRenderer could be some of JTextComponent formatted by using Html and on TableCellEdit event the TableEditor to swith to the plain JFormattedTextField,

还有另一种方式,是否可以使用普通的 J(格式化)TextField ???

is there another way, could it be possible with plain J(Formatted)TextField too ???

推荐答案

在反思中, Latin-1 Supplement 数字表格提供有限的覆盖范围,并且花式方程式可能过度。此示例使用 HTML in Swing Components

On reflection, Unicode fractions among the Latin-1 Supplement and Number Forms offer limited coverage, and fancy equations may be overkill. This example uses HTML in Swing Components.

附录:显示的方法非常适合渲染混合数字。要进行编辑,请键绑定 + / 。我使用了 org.jscience.mathematics。 number.Rational 用于对有理数进行建模,这个解析器可以用来评估理性表达式。

Addendum: The approach shown lends itself fairly well to rendering mixed numbers. For editing, key bindings to + and / could be added for calculator-style input in a text component. I've used org.jscience.mathematics.number.Rational to model rational numbers, and this parser could be adapted to evaluating rational expressions.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/7448216 */
public class HTMLFractions extends JPanel {

    private static int N = 8;

    public HTMLFractions() {
        this.setLayout(new GridLayout(N, N, N, N));
        this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
        for (int r = 0; r < N; r++) {
            for (int c = 0; c < N; c++) {
                this.add(create(r + N, r + 1, c + 2));
            }
        }
    }

    private JLabel create(int w, int n, int d) {
        StringBuilder sb = new StringBuilder();
        sb.append("<html><body>");
        sb.append(w);
        sb.append("<sup>");
        sb.append(n);
        sb.append("</sup>");
        sb.append("<font size=+1>/<font size=-1>");
        sb.append("<sub>");
        sb.append(d);
        sb.append("</sub>");
        sb.append("</html></body>");
        JLabel label = new JLabel(sb.toString(), JLabel.CENTER);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        return label;
    }

    private void display() {
        JFrame f = new JFrame("HTMLFractions");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new HTMLFractions().display();
            }
        });
    }
}

这篇关于如何在Swing JComponents中渲染分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆