java font字体类型,我怎么能在java中使用不同的字体类型和大小相同的文本区域...

博客围绕Java表单中的文本区域展开,提问如何使用不同字体类型和大小。给出解决方案,建议用能处理复杂格式的JTextPane代替JTextArea,若要表格形式展示数据,可让JTextArea用等宽字体并格式化文本,或用JTable替代。还建议合理命名变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值