java swing 设置了JTextArea的大小无法显示滚动条!?

大伙给个解决方法吧,多谢啦!
代码如下:

import java.awt.*;
import javax.swing.*;

public class MyScroll extends JFrame {

/************创建组件*************/
JPanel centerPanel = new JPanel();

JTextArea textArea1 = new JTextArea();
JLabel blankLabel = new JLabel(" ");
JScrollPane scroll = new JScrollPane(textArea1);

/******************主函数*********************/
public static void main(String[] args) {
new MyScroll("月满西楼");
}

/**************构造函数**************/
public MyScroll(String title) {
super( "与 " + title + " 聊天中...");
centerPanel.setBackground(new Color(110,204,255));
centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.X_AXIS));
textArea1.setPreferredSize(new Dimension(200,200));

add(scroll);
add(centerPanel);

//点关闭,释放窗口
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 设置Window的布局管理器为BoxLayout
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
this.setSize(300, 240);
this.setLocationRelativeTo(this);
this.setResizable(false);
this.setVisible(true);
}

}

textArea1.setPreferredSize(new Dimension(200,200));
改成下面的试试
scroll.setPreferredSize(new Dimension(200,200));
至于原因自己想想吧。。
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答