如何在不增加大小的情况下自动滚动文本区域
how to auto scroll a text area without increasing its size
public void NewMessage(){
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter message:");
JTextArea msgBodyContainer = new JTextArea(10,20);
msgBodyContainer.setAutoscrolls(true);
panel.add(label);
panel.add(msgBodyContainer);
String[] options = new String[]{"OK", "Cancel"};
int option = JOptionPane.showOptionDialog(null, panel, "Message "+searchedProfileFirstName,
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
if(option == 0) // pressing OK button
{
}
}
这是我在我定义的方法 NewMessage() 中使用的代码。
我的问题是我想防止这种情况发生:
Picture of problem
1-可见,文本区域自动放大并且超出面板边界不可见
2-标签"Enter message"向下移动到垂直居中与文本区域对齐
JPanel
默认使用 FlowLayout
- 像
JTextArea
这样的文本组件确实应该包裹在 JScrollPane
中,以允许它们变得比可用的 space 更大
推荐
- 改用
GridBagLayout
,它会让您更好地控制布局
- 使用
JScrollPane
将 JTextArea
包裹在 中
例如...
JPanel panel = new JPanel(new GridBagLayout());
JLabel label = new JLabel("Enter message:");
JTextArea msgBodyContainer = new JTextArea(10, 20);
msgBodyContainer.setAutoscrolls(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(label, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
panel.add(new JScrollPane(msgBodyContainer), gbc);
String[] options = new String[]{"OK", "Cancel"};
int option = JOptionPane.showOptionDialog(null, panel, "Message ",
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
参见:
了解更多详情
public void NewMessage(){
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter message:");
JTextArea msgBodyContainer = new JTextArea(10,20);
msgBodyContainer.setAutoscrolls(true);
panel.add(label);
panel.add(msgBodyContainer);
String[] options = new String[]{"OK", "Cancel"};
int option = JOptionPane.showOptionDialog(null, panel, "Message "+searchedProfileFirstName,
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
if(option == 0) // pressing OK button
{
}
}
这是我在我定义的方法 NewMessage() 中使用的代码。 我的问题是我想防止这种情况发生: Picture of problem
1-可见,文本区域自动放大并且超出面板边界不可见 2-标签"Enter message"向下移动到垂直居中与文本区域对齐
JPanel
默认使用FlowLayout
- 像
JTextArea
这样的文本组件确实应该包裹在JScrollPane
中,以允许它们变得比可用的 space 更大
推荐
- 改用
GridBagLayout
,它会让您更好地控制布局 - 使用
JScrollPane
将JTextArea
包裹在 中
例如...
JPanel panel = new JPanel(new GridBagLayout());
JLabel label = new JLabel("Enter message:");
JTextArea msgBodyContainer = new JTextArea(10, 20);
msgBodyContainer.setAutoscrolls(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(label, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
panel.add(new JScrollPane(msgBodyContainer), gbc);
String[] options = new String[]{"OK", "Cancel"};
int option = JOptionPane.showOptionDialog(null, panel, "Message ",
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
参见:
了解更多详情