简体   繁体   English

如何在 java 中为同一文本区域使用不同的字体类型和大小

[英]how can i use different font types and sizes for the same text area in java

i have a text area in my java form.我的 java 表单中有一个文本区域。 how can i use different font types and sizes如何使用不同的字体类型和大小

 public double roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
}
    int ches = 0;
    ref = Integer.parseInt(jTextField6.getText());
    nam = jTextField7.getText();
    add = jTextArea3.getText();
    ph = jTextField8.getText();

    bill(jTextArea4);

    total();

    double tt = 0;
    if (flag == 1) {
        uclist(nam);
        ches = jComboBox2.getSelectedIndex();
    } else if (flag == 0) {
        iclist(nam, add, ph);
        ches = not();
    }

    for (int i = 0; i < no(); i++) {
        if (qty[i] != 0) {
            t[i] = amt[i] / 1.135;
            tv[i] = amt[i] - t[i];
            isalest((i + 1), ches, qty[i], price[i]);
            uSale(nItem[i], qty[i]);
            ustock((i + 1), qty[i]);
            jTextArea4.append("\n" + nItem[i] + "  " + qty[i] + "  "
                    + price[i] + "  " + r(t[i]));
        }
        tt += tv[i];
    }

    jTextArea4.append("\n\tTotal(including vat @13.5%" + r(tt));// TODO add your handling code here:

Better perhaps to use another text component that can handle more complex formatting such as a JTextPane rather than a JTextArea since the latter is more for the display of simple text only.最好使用另一个可以处理更复杂格式的文本组件,例如 JTextPane 而不是 JTextArea,因为后者更多地仅用于显示简单文本。

But having said that, if you want to display data in table form, consider using a JTextArea that has a monospaced font such as Font.MONOSPACED and using String.format(...) to format the text, or even better, use a JTable in place of the JTextArea.但是话虽如此,如果您想以表格形式显示数据,请考虑使用具有等宽字体(如 Font.MONOSPACED)的 JTextArea 并使用 String.format(...) 来格式化文本,或者更好的是,使用JTable 代替 JTextArea。

On a side note, consider renaming all of your variables so that their names make logical sense.附带说明一下,考虑重命名所有变量,以便它们的名称具有逻辑意义。 It's hard for us to make heads or tails out of a method named r() or an array named t[].我们很难从名为 r() 的方法或名为 t[] 的数组中找出正面或反面。 This includes your GUI variables.这包括您的 GUI 变量。 It appears that you're using NetBeans to generate your GUI (and that's the subject of another recommendation for some other time), and this IDE will easily let you rename your GUI variables, and again this is something I strongly recommend.看来您正在使用 NetBeans 来生成您的 GUI(这是另一个建议的主题),而这个 IDE 将很容易让您重命名您的 GUI 变量,这也是我强烈推荐的。 A few weeks from now when you review this code, it will make much more sense if you rename jTextField7 to clientLastNameField or some-such.几周后,当您查看此代码时,将 jTextField7 重命名为 clientLastNameField 或类似名称会更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用两种不同的字体大小的按钮文字? - How to use two different font sizes for button text in Android? 当添加的文本是两种不同的字体大小时,如何在 JPanel 中开始换行 - How can I start a newline in a JPanel when the added text are two different font sizes 一个java项目中下载的字体如何在不同的PC上使用? - How can I use a downloaded font in a java Project on different PCs? 如何使用不同类型的屏幕缩放字体? - How can I scale my font with different types of screen? 如何使用继承和构造函数使用具有2个不同大小的1个数组 - How can I use 1 array with 2 different sizes using inheritance and constructors 我如何在 Java 的不同类中使用相同的 object - How can i use same object in different classes in Java 如何在 Java 中使用自定义字体? - How can I use a custom font in Java? Java模板,如何使用两个具有相同名称和不同类型的类 - Java Templates, how to use two classes with the same name and different types Java - 将不同类型的2个键映射到相同的值(任何类型)。 我该怎么办呢? - Java - map 2 keys of different types to the same value (any type). How can I go about it? 如何访问相同的文本区域变量? - How can I access the same text area variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM