增加 JPanel 的高度及其内容
我有一个示例面板,其中包含其他组件,在我的例子中是 JTextArea。我想随着 JTextArea 高度的增加而增加面板的高度,并且宽度固定。我已经为面板设置了固定宽度。
public class Panel extends JPanel {
public Panel() {
setPreferredSize(new Dimension(300, 85));
setLayout(new BorderLayout());
JPanel pic = new JPanel(new FlowLayout(FlowLayout.LEFT));
pic.setBackground(Color.GRAY);
pic.add(new JLabel(new ImageIcon("img/icon.png")));
JPanel status = new JPanel(new FlowLayout(FlowLayout.LEFT));
status.setBackground(Color.GRAY);
JTextArea textArea = new JTextArea();
String text = "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog.";
textArea.setText(text);
textArea.enable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
status.add(textArea);
add(pic, BorderLayout.WEST);
add(status, BorderLayout.CENTER);
setBorder(BorderFactory.createEtchedBorder());
}
public static void main(String[] args) {
JFrame f = new JFrame("Panel Test");
f.add(new Panel());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
我不想要 JTextArea 的滚动条。我希望它的高度与完整查看其内容所需的高度相同。
I have a sample panel which have other component, in my case JTextArea. I want to increase the height of the panel as the height of JTextArea increases, with fixed width. I have set fixed width for panel.
public class Panel extends JPanel {
public Panel() {
setPreferredSize(new Dimension(300, 85));
setLayout(new BorderLayout());
JPanel pic = new JPanel(new FlowLayout(FlowLayout.LEFT));
pic.setBackground(Color.GRAY);
pic.add(new JLabel(new ImageIcon("img/icon.png")));
JPanel status = new JPanel(new FlowLayout(FlowLayout.LEFT));
status.setBackground(Color.GRAY);
JTextArea textArea = new JTextArea();
String text = "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog. "
+ "The quick brown fox jumps over the lazy dog.";
textArea.setText(text);
textArea.enable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
status.add(textArea);
add(pic, BorderLayout.WEST);
add(status, BorderLayout.CENTER);
setBorder(BorderFactory.createEtchedBorder());
}
public static void main(String[] args) {
JFrame f = new JFrame("Panel Test");
f.add(new Panel());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
And I don't want scrollbar for JTextArea. I want it to take as much height as it takes for complete viewing of its content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非文本区域的大小已知,否则无法确定文本区域的首选大小。也就是说,在知道文本区域的宽度之前,无法完成文本的换行。所以你需要给文本区域一个宽度。
另外,您不能为面板指定首选大小,因为这将违背使用 pack() 的目的;
The preferred size of the text area can't be determined unless the size of text area is known. That is the wrapping of the text can't be done until the width of the text area is known. So you need to give the text area a width.
Also, you can't give the panel a preferred size since this will defeat the purpose of using pack();