我正在將文本從文本文件掃描到JPanel。文本正在顯示,但不會向下滾動。有任何想法嗎?這裏是我的代碼:JScrollPane不滾動
rFrame = new JFrame();
rFrame.setBounds(10, 10, 502, 502);
rFrame.getContentPane().setLayout(null);
JPanel pan = new JPanel();
pan.setBackground(Color.WHITE);
pan.setBounds(100, 100, 400, 400);
rFrame.getContentPane().add(pan);
pan.setEnabled(false);
JScrollPane scrollBar=new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
rFrame.add(scrollBar);
JTextArea area = new JTextArea();
area.setText(printToFrame()); //Function prints from text file
pan.add(area);
rFrame.add(pan);
rFrame.setVisible(true);
rFrame.setLayout(new FlowLayout());
rFrame.getContentPane().add(pan);
rFrame.pack();
ClientWindow window = new ClientWindow();
window.rFrame.setVisible(true);
鍋是完全無用的在這裏。直接將文本區域添加到滾動窗格。另外,如果我沒有記錯,setEnabled不適用於嵌套組件 - 但我可能在這裏是錯誤的。 – ignis
你是對的,我因此刪除了我的答案。 WM,你需要學習Swing教程,因爲這裏解釋了所有這些。 –
另外,請研究在JDK安裝目錄的「demo」子目錄中找到的SwingSet2的源代碼。另外:不要setLayout(null),除非你有一個很好的理由(在這種情況下你沒有),滾動窗格不是滾動條,也不會將文本區域和框架添加到面板,因爲你已經添加了滾動窗格。 – ignis