应将JTextArea置于JScrollPanel中
若要使只有垂直滚动条而没有水平滚动条,使用JTextArea.setLineWrap(true),自动换行。
文本换行代码片段如下:
JTextArea adressTextArea = new JTextArea(11, 35);
//设置自动换行
adressTextArea.setLineWrap(true);
//定义带滚动条的panel,并将JTextArea存入到panel中,使textarea具有滚动条显示功能。
JScrollPane scrollpane = new JScrollPane(adressTextArea);
//取消显示水平滚动条
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//显示垂直滚动条
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 以下为个人测试画的一个简单页面,仅供参考:
package JFrameTest; import java.awt.BorderLayout; @SuppressWarnings("all")
public class ApplyCertTest extends JFrame { private JPanel contentPane; /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ApplyCertTest frame = new ApplyCertTest();
frame.setVisible(true);
frame.setTitle("申请新许可");
frame.getContentPane().setLayout(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public ApplyCertTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setResizable决定了窗体是否可以放大。true则可以最大化,false则不可变
setResizable(false);
setBounds(100, 100, 510, 520);
contentPane = new JPanel();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT)); getContentPane().add(contentPane, BorderLayout.NORTH); JLabel lblNewLabel = new JLabel(" 生成新的许可需填写下列信息:");
lblNewLabel.setLabelFor(contentPane);
contentPane.add(lblNewLabel); JPanel lPanel = new JPanel();
lPanel.setBorder (BorderFactory.createTitledBorder ("申请文件信息"));
lPanel.setLayout(new BorderLayout(5, 0));
getContentPane().add(lPanel); //用户名称
JPanel mlPanel = new JPanel();
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel userName = new JLabel("用户名称: ");
mlPanel.add(userName);
JTextField userTextField = new JTextField(35);
mlPanel.add(userTextField); //公司名称
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel companyName = new JLabel("公司名称: ");
mlPanel.add(companyName);
JTextField companyTextField = new JTextField(35);
mlPanel.add(companyTextField);
lPanel.add(mlPanel); //电话号码
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel phone = new JLabel("电话号码: ");
mlPanel.add(phone);
JTextField phoneTextField = new JTextField(35);
mlPanel.add(phoneTextField);
lPanel.add(mlPanel); //电子邮件
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel email = new JLabel("电子邮件: ");
mlPanel.add(email);
JTextField emailTextField = new JTextField(35);
mlPanel.add(emailTextField);
lPanel.add(mlPanel); //联系地址
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel adress = new JLabel("联系地址: ");
mlPanel.add(adress);
//JTextField adressTextField = new JTextField(35);
JTextArea adressTextArea = new JTextArea(11, 35);
//设置自动换行
adressTextArea.setLineWrap(true);
//定义带滚动条的panel,并将JTextArea存入到panel中,使textarea具有滚动条显示功能。
JScrollPane scrollpane = new JScrollPane(adressTextArea);
//取消显示水平滚动条
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//显示垂直滚动条
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mlPanel.add(scrollpane);
lPanel.add(mlPanel); //适用IP
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel ipLabel = new JLabel("适用IP: ");
mlPanel.add(ipLabel);
JTextField ipTextField = new JTextField(35);
mlPanel.add(ipTextField);
lPanel.add(mlPanel); //适用mac地址
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel macLabel = new JLabel("适用mac地址:");
mlPanel.add(macLabel);
JTextField macTextField = new JTextField(35);
mlPanel.add(macTextField);
lPanel.add(mlPanel); //使用时限(天)
mlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel dayLimit = new JLabel("使用时限(天): ");
mlPanel.add(dayLimit);
JTextField dayLimitTextField = new JTextField(35);
mlPanel.add(dayLimitTextField);
lPanel.add(mlPanel); JPanel bPanel = new JPanel();
bPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton saveButton = new JButton("保存");
JButton resetButton = new JButton("重置");
JButton quitButton = new JButton("退出");
bPanel.add(saveButton);
bPanel.add(resetButton);
bPanel.add(quitButton); getContentPane().add(bPanel, BorderLayout.SOUTH); }
}