Java 中的简单文本格式(例如上标)?

发布于 2025-01-07 20:03:47 字数 487 浏览 3 评论 0原文

可能的重复:
如何为复选框文本编写上标单词java?

所以我有一个非常简单的程序,它基本上由一个带有几个文本字段和一个按钮的 GUI 组成。

这个想法是用户在三个字段中输入数字并按下按钮。然后会在后台执行一些数学过程,并将结果答案显示在第四个文本字段中。

现在,这已经完成了工作,但是格式看起来很糟糕。至少,我希望部分输出带有上标。我对这些事情几乎没有经验,但我以为我能够自己掌握这些,但我被困住了。我想我需要使用 AttributedString 和可能的 Font,但我无法让任何东西工作。而且我没有找到任何教程。

有人有快速指点吗?这将是最有帮助的!

Possible Duplicate:
How do I write superscript word for checkbox text in java?

So I have this very simple program that basically consists of a GUI with a few text fields and a button.

The idea is that the user enters numbers into three fields and presses the button. Then what happens is a few mathematical procedures are carried out in the background, and the resuling answer is presented in a fourth text field.

Now, this does the job, but the formatting looks awful. At the very least, I would like to have part of the output superscripted. I have next to no experience with these things, but thought I would be able to get the hang of this on my own, but I'm stuck. I think I need to use AttributedString and possibly Font, but I can't get anything to work. And I've found no tutorials.

Does anyone have any quick pointers? That'd be most helpful!

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

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

发布评论

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

评论(3

み零 2025-01-14 20:03:47

您需要输出字段可编辑吗?如果没有,请尝试使用带有 HTML 代码的 JLabel。类似于:

jLabel4.setText("<html>ax<sup>2</sup>+bx+c</html>");

您可以向该 JLabel 添加边框,使其看起来像文本字段。

Do you need the output field to be editable? If not, try using a JLabel with HTML code. Something like:

jLabel4.setText("<html>ax<sup>2</sup>+bx+c</html>");

You can add a border to that JLabel to make it look like a text field.

待天淡蓝洁白时 2025-01-14 20:03:47

为什么不使用其他支持 HTML 的 swing 组件来显示答案,例如 JLabel。例如,如果您使用 JLabel,您可以使用内联 html 来格式化您的答案

Why not use other swing components that support HTML to display the answer like JLabel. If you use a JLabel for instance you can use inline html to format your answer

2025-01-14 20:03:47

您遇到困难的原因与缺乏默认的排版系统有关。字体选择等通常提供非常有限的方法来进行正确的数学排版(这是一般排版的专门子集)。

我不知道 Java 库中已经内置了任何数学特定的排版,也就是说,也许您可​​以 将 ExTex 项目集成到您的 Java 组件中或使用 2d API 推出您自己的解决方案(查看基线偏移)。

其他替代方案是生成图形并显示其渲染图像。如果显示是静态的,这可能是一个更容易的选择。

The reasons you are having difficulty has to do with the lack of a default typsetting system. Font selection and such typically provides a very limited means to do proper math typesetting (which is a specialized subset of general typesetting).

I don't know of any math specific typesetting already built-in to the Java libraries, that said, perhaps you can integrate the ExTex project into your Java components or roll your own solution using the 2d API (look at baseline offsetting).

Other alternatives are to generate a graphic, and display it's rendered image. If the display is static, this might be a much easier choice.

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