日期:2014-05-20  浏览次数:21215 次

JTextArea不能正常显示
我想做一个汇率计算程序,我用了一个JTextArea并添加到滚动面板上之后添加到窗体上,用的是网格包布局,可是JTextArea在刚显出来时只有一行,可我设的是5行,我加了一个pack方法之后能正常显示出5行来,可是整个窗体又变小了,请各位大哥帮帮忙,问题解决,立即给分.小弟先谢了.
附源码:
import   java.awt.*;
import   javax.swing.*;
import   java.awt.event.*;
import   javax.swing.*;
import   java.text.*;

class   HuiluCalculator   extends   JFrame   implements   ActionListener
{
JTextArea   jieguoJTextArea;
JComboBox   bizhong;
JTextField   rmb;
JTextField   huilu;
JButton   jishuan;
JLabel   lblrmb;
JLabel   lblhuilu;
JLabel   lblbizhong;
JLabel   lbljieguo;
Container   con;
GridBagLayout   gb;
GridBagConstraints   gbc;

public   HuiluCalculator()
{
setTitle( "汇率计算器 ");
setBounds(400,300,300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
gb=new   GridBagLayout();
gbc=new   GridBagConstraints();

gbc.fill=GridBagConstraints.BOTH;
jieguoJTextArea=new   JTextArea( "人民币\t兑换金额\t币种 ",5,20);
rmb=new   JTextField(10);
huilu=new   JTextField(10);
jishuan=new   JButton( "计算 ");
lblrmb=new   JLabel( "人民币: ");
lblhuilu=new   JLabel( "兑换汇率: ");
lblbizhong=new   JLabel( "外币种类: ");
lbljieguo=new   JLabel( "人民币及兑换外币金额: ");
String   bz[]={ "美元 ", "日元 ", "英磅 ", "法币 "};
bizhong=new   JComboBox(bz);

con=getContentPane();
con.setLayout(gb);
addComponent(lblrmb,0,0,1,1);
addComponent(rmb,0,1,1,2);
addComponent(jishuan,0,3,1,1);

addComponent(lblhuilu,1,0,1,1);
addComponent(huilu,1,1,1,2);

addComponent(lblbizhong,2,0,1,1);
addComponent(bizhong,2,1,1,2);

addComponent(lbljieguo,3,0,1,2);
addComponent(jieguoJTextArea,4,0,20,4);
jishuan.addActionListener(this);
}
public   void   addComponent(Component   c,int   row,int   col,int   nrow,int   ncol)
{
gbc.gridx=col;
gbc.gridy=row;

gbc.gridwidth=ncol;
gbc.gridheight=nrow;

gb.setConstraints(c,gbc);
con.add(c);
}

public   void   actionPerformed(ActionEvent   e)
{
if(jieguoJTextArea.getLineCount()> 1)
{
jieguoJTextArea.setText( "人民币\t兑换金额\t币种 ");
}
Float   x,y;
x=new   Float(rmb.getText());
y=new   Float(huilu.getText());
String   s=new   String();
String   s1=new   DecimalFormat( "0.00 ").format(x.floatValue()*y.floatValue());
s= "\n "   +   rmb.getText()+   "\t "   +   s1   +   "\t "   +   bizhong.getSelectedItem().toString();
jieguoJTextArea.append(s);
}

public   static   void   main(String   args[])
{
new   HuiluCalculator().show();
}
}

------解决方案--------------------
你repaint()一下试试