我想向我的 JTextArea
添加滚动条,但它不会显示。我在论坛上读了很多东西,但都是徒劳。任何建议都将受到高度赞赏。
提前致谢。下面是我的代码。
JPanel pan, pan2;
JTextArea text = new JTextArea();
JTextField fname = new JTextField(18);
JLabel filename = new JLabel("Filename");
JButton view = new JButton("View");
public FileReading() {
setLayout(new BorderLayout());
pan = new JPanel();
pan2 = new JPanel();
JScrollPane scroll = new JScrollPane(text);
//scroll.setBounds(400,400,400,400);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
text.setEditable(false);
scroll.setViewportView(text);
pan2.add(scroll);
//scrollpane.setViewportView(text);
pan2.setLayout(new BorderLayout());
//pan2.add(scrollpane);
pan.setLayout(new FlowLayout());
pan.add(filename, FlowLayout.LEFT);
pan.add(fname, FlowLayout.CENTER);
pan.add(view, FlowLayout.RIGHT);
view.addActionListener(this);
fname.addActionListener(this);
pan2.add(text, BorderLayout.CENTER);
pan2.add(pan, BorderLayout.SOUTH);
//BorderLayout.EAST
//add(pan, BorderLayout.SOUTH);
add(pan2);//, BorderLayout.CENTER
setVisible(true);
}
public static void main(String args[]) {
FileReading frame = new FileReading();
frame.setTitle("Enter The Full Path to the File");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,400,400,400);
//frame.setSize(400,400);
}
最佳答案
您需要将 scroll
(JScrollPane) 添加到 pan2
而不是 text
(JTextArea)
试试这个
pan2.add(scroll, BorderLayout.CENTER);
代替pan2.add(text, BorderLayout.CENTER);
编辑
当我们将JScrollPane
添加到面板中时,JTextArea
会自动添加,因为您已在 JScrollPane
中添加了 text
(JTextArea)
此处 -> JScrollPane 滚动 = new JScrollPane(text);
关于java - JScrollPane 在具有两个面板的 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13761133/