繁体   English   中英

如何设置JTextArea的最大大小,并且如果等于或大于该大小,则可以添加一个滑块

[英]How to set maximum size of a JTextArea and also if equal or bigger of that size it could add a slider

我目前正在使用JTextArea。 我的问题是我应该使用JTextArea做什么,以便可以设置特定的大小,并且如果文本内容太大而无法在JTextArea上添加滑块?

第一个示例图片 在此处输入图片说明

这是创建我的JTextArea的方法:

public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(12,1);
    textArea.setFont(new Font("Serif", Font.PLAIN, 25));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setText("\n\n     Server:");
    textArea.append("\n          Hello User !");

    panel.add(textArea, BorderLayout.SOUTH);

    return panel;
}

我使用以下方法解决了问题!

/**
 * Reference : https://stackoverflow.com/questions/10177183/java-add-scroll-into-text-area
 * Reference : https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#sizing
 * @return a panel of a JTextArea inside an ScrollPane
 */
public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(15,0);
    textArea.setFont(new Font("Serif", Font.BOLD, 20));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setLineWrap(true);

    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    System.out.println("textArea height = " + textArea.getSize().width);
    textArea.setText("    Server>>>   ");
    textArea.append("Connection Succesful !");

    panel.add(areaScrollPane, BorderLayout.SOUTH);

    return panel;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM