【问题标题】:How to set maximum size of a JTextArea and also if equal or bigger of that size it could add a slider如何设置 JTextArea 的最大大小,如果等于或大于该大小,它可以添加一个滑块
【发布时间】:2018-06-13 16:40:26
【问题描述】:

我目前正在使用 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;
}

【问题讨论】:

  • 1- 查看 JTextArea 的构造函数,它允许您以独立于平台的方式指定行/列; 2- 在 JScrollPane 中嵌入 JTextArea
  • 我尝试过以不同的方式使用它,例如 (12,1), (12,10), (40,40), (100,0)
  • 你需要一个布局管理器,它可以支持文本区域的首选大小,可能是 GridBagLayout
  • 您需要记住,这些信息只是系统可以用来确定组件应该如何布局的提示,如果需要,系统也可以忽略它们

标签: java scroll slider size jtextarea


【解决方案1】:

我用下面的方法解决了这个问题!!

/**
 * 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2011-01-25
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多