【问题标题】:How to set size of a component in a GridLayout and expand JTextArea while typing?如何在 GridLayout 中设置组件的大小并在键入时扩展 JTextArea?
【发布时间】:2019-10-27 11:16:16
【问题描述】:

我正在创建一个聊天,但这没关系。我还没有问题,一切正常,但我想更改 UI 外观,以便用户可以拥有更大的 JTextArea,而不是按钮占用比需要更多的空间。另外,我想调整 JTextArea 的大小,而不是在一行中写几个文本。最后,我要感谢你花时间帮助我:D

目标:

  • 设置 JLabel 或 JTextArea 的大小,使按钮保持较小。

  • 当文本达到正确限制时展开 JLabel 并将其放入新行(有点 WhatsApp)。

代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

class MyFirstChatSO extends JFrame {

  JLabel title;
  JTextArea messages;
  JTextField text;
  JButton send;

    public MyFirstChatSO () {
      setLayout(new BorderLayout(0,10));

        title = new JLabel("-Chat-");
          title.setHorizontalAlignment(JLabel.CENTER);
          title.setFont(new Font("Gabriola",Font.ITALIC,34));
        add(title, BorderLayout.NORTH);

        messages = new JTextArea();
          messages.setEnabled(false);
          messages.setBorder(BorderFactory.createLineBorder(new Color(168,168,168),2,true));
        add(messages, BorderLayout.CENTER);

        JPanel subPanel = new JPanel();
          subPanel.setLayout(new GridLayout(0,2));
            text = new JTextField();
          subPanel.add(text);
            send = new JButton("Send");
          subPanel.add(send);
        add(subPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        MyFirstChatSO chat = new MyFirstChatSO();
          chat.setSize(600,450);
          chat.setLocationRelativeTo(null);
          chat.setVisible(true);
          chat.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

如果有任何其他方法(例如使用不同的 layoutsManagers)是解决方案,最好了解一下。

又一次失败,但这次是使用 GridBagLayout(我不知道如何使用它,请帮助我):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

class MyFirstChatSO extends JFrame {

  JLabel title;
  JTextArea messages;
  JTextField text;
  JButton send;

    public MyFirstChatSO () {
      setLayout(new BorderLayout(0,10));

        title = new JLabel("-Chat-");
          title.setHorizontalAlignment(JLabel.CENTER);
          title.setFont(new Font("Gabriola",Font.ITALIC,34));
        add(title, BorderLayout.NORTH);

        messages = new JTextArea();
          messages.setEnabled(false);
          messages.setBorder(BorderFactory.createLineBorder(new Color(168,168,168),2,true));
        add(messages, BorderLayout.CENTER);

    JPanel subPanel = new JPanel();
      subPanel.setLayout(new GridBagLayout());
      GridBagConstraints gb = new GridBagConstraints();
      gb.weightx = 1;
      gb.weighty = 1;
      gb.fill = GridBagConstraints.HORIZONTAL;
        text = new JTextField();
        gb.gridx = 0;
        gb.gridy = 0;
        gb.gridwidth = 6;
        gb.fill = GridBagConstraints.HORIZONTAL;
      subPanel.add(text,gb);
        send = new JButton("Send");
        gb.gridx = 7;
        gb.gridy = 0;
      subPanel.add(send,gb);
    add(subPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        MyFirstChatSO chat = new MyFirstChatSO();
          chat.setSize(600,450);
          chat.setLocationRelativeTo(null);
          chat.setVisible(true);
          chat.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

【问题讨论】:

  • “设置 JLabel 或 JTextArea 的大小,使按钮保持较小。” 我会考虑使用GridBagLayout 或复合布局的组合
  • “当文本达到正确的限制时展开 JLabel 并将其放入新行(有点像 WhatsApp)。” - 可能的想法。也许考虑使用只读的JTextArea 不嵌入它自己的JScrollPane。有趣的是,GridBagLayout 将允许它根据需要增长。您也可以使用JLabel 并将文本包装在html
  • 哦,谢谢...但是,¿您能把它作为官方答案发布吗?
  • 另外,我在这里尝试使用 gridbaglayout 进行尝试(仍然不起作用):code.sololearn.com/c0TfyKCNq9Th/#java

标签: java user-interface


【解决方案1】:

又一次失败,但这次是使用 GridBagLayout(我不知道如何使用它,请帮助我):

是的,您需要了解它的工作原理。始终拥有How to Use GridBagLayout 和可用的 JavaDocs ;)

所以,我开始...

JPanel subPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4, 4, 4, 4);

text = new JTextArea(1, 80);
text.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
subPanel.add(text, gbc);

gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
send = new JButton("Send");
subPanel.add(send, gbc);

我最终得到了类似...

但是等一下,这个例子使用的是JTextArea?!为什么!?

啊,那是因为我是个鬼鬼祟祟的裤子;)

通过这种方式使用JTextArea,您可以免费获得一个自动扩展的文本字段。现在,重要的是要记住,这是不受限制的,这意味着用户可以按住 enter 并使 JTextArea 填满越来越多的空间。

要解决这个问题变得很复杂,我会向Scrollable interface 寻求一些想法

【讨论】:

    猜你喜欢
    • 2016-09-12
    • 2013-03-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多