java中JTextArea设置的滚动条不显示怎么办

我写一个关于JTextArea显示的程序,想显示滚动条,可是不显示,大家帮忙看看,是代码错了吗?还是怎么回事/** * @(#)LabelTextArea.java * * * @author * @version 1.00 2010/6/23 */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class LabelTextArea extends JFrame implements ActionListener { JLabel l; JTextArea ta; JButton b; Container cp; JScrollPane sp; public LabelTextArea() { super("Show Text"); b=new JButton("OK"); l=new JLabel("请输入"); ta=new JTextArea(2,10); //ta.setFont(new Font("Serif",Font.PLAIN,10)); //自动换行功能 //ta.setLineWrap(true); //断行不断字功能 //ta.setWrapStyleWord(true); sp=new JScrollPane(ta); cp=getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b); cp.add(l); //cp.add(ta); cp.add(sp,BorderLayout.CENTER); //ta.addActionListener(this); b.addActionListener(this); setSize(400,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b) l.setText(ta.getText()); //l.setText(ta.getText()); } public static void main(String[] args){ new LabelTextArea(); } }

你要加一个JFrame
f=new
JFrame();
f.add(cp);
f.setSize(400,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
另外再加一句:
f.pack();//自动调整为最佳窗口大小
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜