简体   繁体   中英

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. 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.

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.

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[]. This includes your GUI variables. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM