[英]Why does the JScrollPane not appear around my JTextField?
I'm pretty new to GUI but I'm trying to create a simple version of notepad and would like scroll bars to appear around the text area. 我是GUI的新手,但是我想创建一个简单的记事本版本,希望滚动条出现在文本区域周围。 However, I'm not sure why it isn't appearing.
但是,我不确定为什么它不出现。
public class NutPad extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("NutPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NutPad(), BorderLayout.CENTER);
frame.setSize(500,300);
frame.setVisible(true);
}
private NutPad() {
add(makeTextAreaPanel());
}
private JPanel makeTextAreaPanel() {
JPanel textAreaPanel = new JPanel();
textAreaPanel.setSize(100,100);
JTextArea textArea = new JTextArea(20, 60); //15,43
JScrollPane scrollPane = new JScrollPane(textArea);
textAreaPanel.add(scrollPane,BorderLayout.CENTER);
textAreaPanel.add(textArea);
return textAreaPanel;
}
}
Thanks 谢谢
If you're going to use the BorderLayout.CENTER
constraint, then the container needs to have its layout set to BorderLayout
. 如果要使用
BorderLayout.CENTER
约束,则容器需要将其布局设置为BorderLayout
。
Also you don't need textAreaPanel
since you can just add the scrollPane
straight into your NutPad
panel. 另外,您不需要
textAreaPanel
因为您可以直接将scrollPane
添加到NutPad
面板中。
private NutPad() {
setLayout(new BorderLayout());
add(makeScrollPane(), BorderLayout.CENTER);
}
private JScrollPane makeScrollPane() {
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}
Now your text area will fill the frame and the scrollbars will appear when the text takes up more than the available space. 现在,您的文本区域将填满框架,并且当文本占用的空间超过可用空间时,将显示滚动条。
Hope that helps :) 希望有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.