2011-03-12 71 views
1

我在jscrollpane中添加了一個jtextarea,但我不知道它爲什麼不起作用! 你會幫我嗎?java中的jscrollpane問題

問題是我的文本下降,但滾動不工作在GUI中。

我上傳所有的代碼,因爲它很容易運行它,只需創建一個主文件&設置它的大小。 u能爲這些JScrollPane的跟蹤問題:jScrollPane1,jScrollPane16 jScrollPane4

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import com.borland.jbcl.layout.*; 

public class Frame1 extends JFrame { 
     JPanel contentPane; 
     BorderLayout borderLayout1 = new BorderLayout(); 
     JMenuBar jMenuBar1 = new JMenuBar(); 
     JMenu jMenuFile = new JMenu(); 
     JMenuItem jMenuFileExit = new JMenuItem(); 
     JMenu jMenuHelp = new JMenu(); 
     JMenuItem jMenuHelpAbout = new JMenuItem(); 
     JToolBar jToolBar = new JToolBar(); 
     JButton jButton1 = new JButton(); 
     JButton jButton2 = new JButton(); 
     JButton jButton3 = new JButton(); 
     ImageIcon image1 = new ImageIcon(masterproject.Frame1.class.getResource(
       "openFile.png")); 
     ImageIcon image2 = new ImageIcon(masterproject.Frame1.class.getResource(
       "closeFile.png")); 
     ImageIcon image3 = new ImageIcon(masterproject.Frame1.class.getResource(
       "help.png")); 
     JLabel statusBar = new JLabel(); 
     JSplitPane jSplitPane1 = new JSplitPane(); 
     JPanel jPanel1 = new JPanel(); 
     JPanel jPanel2 = new JPanel(); 
     DynamicTree jtProjectList = new DynamicTree(); 
     BorderLayout borderLayout2 = new BorderLayout(); 
     JToolBar jToolBar1 = new JToolBar(); 
     JToolBar jToolBar2 = new JToolBar(); 
     JButton jbAdd = new JButton(); 
     JButton jbRemove = new JButton(); 
     int newNodeSuffix = 0; 
     JToolBar jToolBar3 = new JToolBar(); 
     BorderLayout borderLayout3 = new BorderLayout(); 
     JPanel jpCore = new JPanel(); 
     JPanel jpExclusion = new JPanel(); 
     JLabel jLabel1 = new JLabel(); 
     JTextArea jTextArea1 = new JTextArea(); 
     BorderLayout borderLayout4 = new BorderLayout(); 
     JTable jTable1 = new JTable(); 
     JPanel jpScope = new JPanel(); 
     JLabel jLabel2 = new JLabel(); 
     JTextArea jTextArea2 = new JTextArea(); 
     JTable jTable2 = new JTable(); 
     BorderLayout borderLayout5 = new BorderLayout(); 
     VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout(); 
     JPanel jpDeliverable = new JPanel(); 
     JTable jTable3 = new JTable(); 
     JLabel jLabel3 = new JLabel(); 
     JTextArea jTextArea3 = new JTextArea(); 
     BorderLayout borderLayout6 = new BorderLayout(); 
     JPanel jpAcceptance = new JPanel(); 
     JPanel jpConstraints = new JPanel(); 
     JPanel jpAssumption = new JPanel(); 
     JPanel jpRisk = new JPanel(); 
     BorderLayout borderLayout7 = new BorderLayout(); 
     BorderLayout borderLayout8 = new BorderLayout(); 
     BorderLayout borderLayout9 = new BorderLayout(); 
     JTable jTable4 = new JTable(); 
     JTable jTable5 = new JTable(); 
     JTable jTable6 = new JTable(); 
     JLabel jLabel4 = new JLabel(); 
     JLabel jLabel5 = new JLabel(); 
     JLabel jLabel6 = new JLabel(); 
     JTextArea jTextArea4 = new JTextArea(); 
     JTextArea jTextArea5 = new JTextArea(); 
     JTextArea jTextArea6 = new JTextArea(); 
     JLabel jLabel7 = new JLabel(); 
     JTextArea jTextArea7 = new JTextArea(); 
     JTable jTable7 = new JTable(); 
     BorderLayout borderLayout10 = new BorderLayout(); 
     JScrollPane jScrollPane1 = new JScrollPane(); 
     JScrollPane jScrollPane2 = new JScrollPane(); 
     JScrollPane jScrollPane3 = new JScrollPane(); 
     JScrollPane jScrollPane4 = new JScrollPane(); 
     JScrollPane jScrollPane5 = new JScrollPane(); 
     JScrollPane jScrollPane6 = new JScrollPane(); 
     JScrollPane jScrollPane7 = new JScrollPane(); 
     JScrollPane jScrollPane8 = new JScrollPane(); 
     JScrollPane jScrollPane9 = new JScrollPane(); 
     JScrollPane jScrollPane10 = new JScrollPane(); 
     JScrollPane jScrollPane11 = new JScrollPane(); 
     JScrollPane jScrollPane12 = new JScrollPane(); 
     JScrollPane jScrollPane13 = new JScrollPane(); 
     JScrollPane jScrollPane14 = new JScrollPane(); 
     JScrollPane jScrollPane15 = new JScrollPane(); 
     JScrollPane jScrollPane16 = new JScrollPane(); 
     public Frame1() { 
      try { 
       setDefaultCloseOperation(EXIT_ON_CLOSE); 
       jbInit(); 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
     } 

     /** 
     * Component initialization. 
     * 
     * @throws java.lang.Exception 
     */ 
     private void jbInit() throws Exception { 
      contentPane = (JPanel) getContentPane(); 
      contentPane.setLayout(borderLayout1); 
      this.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14)); 
      setSize(new Dimension(753, 483)); 
      setTitle("Master Project"); 
      statusBar.setText(" "); 
      jMenuFile.setText("File"); 
      jMenuFileExit.setText("Exit"); 
      jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this)); 
      jMenuHelp.setText("Help"); 
      jMenuHelpAbout.setText("About"); 
      jMenuHelpAbout.addActionListener(new 
              Frame1_jMenuHelpAbout_ActionAdapter(this)); 
      jPanel1.setLayout(borderLayout2); 
      jbAdd.setMaximumSize(new Dimension(51, 25)); 
      jbAdd.setPreferredSize(new Dimension(51, 25)); 
      jbAdd.setText("Add"); 
      jbAdd.addActionListener(new Frame1_jbAdd_actionAdapter(this)); 
      jbRemove.setText("Remove"); 
      jbRemove.addActionListener(new Frame1_jbRemove_actionAdapter(this)); 
      contentPane.setMinimumSize(new Dimension(400, 600)); 
      contentPane.setPreferredSize(new Dimension(400, 600)); 
      jSplitPane1.setMinimumSize(new Dimension(200, 55)); 
      jSplitPane1.setPreferredSize(new Dimension(200, 355)); 
      jSplitPane1.setLastDividerLocation(300); 
      jtProjectList.setPreferredSize(new Dimension(200, 324)); 
      jPanel2.setLayout(borderLayout3); 
      jLabel1.setText("Exclusion :"); 
      jTextArea1.setPreferredSize(new Dimension(280, 60)); 
      jTextArea1.setText("jTextArea1"); 
      jTextArea1.setLineWrap(true); 
      jpExclusion.setLayout(borderLayout4); 
      jLabel2.setText("Scope :"); 
      jTextArea2.setPreferredSize(new Dimension(280, 60)); 
      jTextArea2.setText("jTextArea1"); 
      jTextArea2.setLineWrap(true); 
      jpScope.setLayout(borderLayout5); 
      jpCore.setLayout(verticalFlowLayout1); 
      jLabel3.setText("Deliverable :"); 
      jTextArea3.setPreferredSize(new Dimension(280, 60)); 
      jTextArea3.setText("jTextArea3"); 
      jTextArea3.setLineWrap(true); 
      jpDeliverable.setLayout(borderLayout6); 
      jpAssumption.setLayout(borderLayout7); 
      jpConstraints.setLayout(borderLayout8); 
      jpAcceptance.setLayout(borderLayout9); 
      jLabel4.setText("Assumption :"); 
      jLabel5.setText("Constraints :"); 
      jLabel6.setText("Acceptance :"); 
      jTextArea4.setMinimumSize(new Dimension(80, 18)); 
      jTextArea4.setPreferredSize(new Dimension(280, 60)); 
      jTextArea4.setText("jTextArea4"); 
      jTextArea4.setLineWrap(true); 
      jTextArea5.setMinimumSize(new Dimension(80, 18)); 
      jTextArea5.setPreferredSize(new Dimension(280, 60)); 
      jTextArea5.setText("jTextArea5"); 
      jTextArea5.setLineWrap(true); 
      jTextArea6.setMinimumSize(new Dimension(80, 18)); 
      jTextArea6.setPreferredSize(new Dimension(280, 60)); 
      jTextArea6.setText("jTextArea6"); 
      jTextArea6.setLineWrap(true); 

      jpScope.setToolTipText(""); 
      jLabel7.setText("Risk :"); 
      jTextArea7.setPreferredSize(new Dimension(280, 60)); 
      jTextArea7.setText("jTextArea7"); 
      jTextArea7.setLineWrap(true); 
      jpRisk.setLayout(borderLayout10); 
      jpRisk.setMinimumSize(new Dimension(280, 60)); 
      jScrollPane6.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane16.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane5.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane7.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane14.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane12.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane10.setPreferredSize(new Dimension(284, 44)); 
      jScrollPane3.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane9.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane11.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane13.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane8.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane4.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane15.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jScrollPane1.setVerticalScrollBarPolicy(JScrollPane. 
                VERTICAL_SCROLLBAR_ALWAYS); 
      jMenuBar1.add(jMenuFile); 
      jMenuFile.add(jMenuFileExit); 
      jMenuBar1.add(jMenuHelp); 
      jMenuHelp.add(jMenuHelpAbout); 
      setJMenuBar(jMenuBar1); 
      jButton1.setIcon(image1); 
      jButton1.setToolTipText("Open File"); 
      jButton2.setIcon(image2); 
      jButton2.setToolTipText("Close File"); 
      jButton3.setIcon(image3); 
      jButton3.setToolTipText("Help"); 
      jToolBar.add(jButton1); 
      jToolBar.add(jButton2); 
      jToolBar.add(jButton3); 
      contentPane.add(statusBar, BorderLayout.SOUTH); 
      contentPane.add(jSplitPane1, java.awt.BorderLayout.CENTER); 
      jSplitPane1.add(jPanel1, JSplitPane.LEFT); 
      jSplitPane1.add(jPanel2, JSplitPane.RIGHT); 
      jToolBar1.add(jtProjectList); 
      jPanel1.add(jToolBar1, java.awt.BorderLayout.CENTER); 
      jPanel1.add(jToolBar2, java.awt.BorderLayout.NORTH); 
      jToolBar2.add(jbAdd); 
      jToolBar2.add(jbRemove); 
      jPanel2.add(jToolBar3, java.awt.BorderLayout.CENTER); 
      jToolBar3.add(jScrollPane3); 
      jScrollPane3.getViewport().add(jpCore); 
      jpScope.add(jScrollPane1, java.awt.BorderLayout.CENTER); 

      jScrollPane1.getViewport().add(jTextArea2); 

      jScrollPane16.getViewport().add(jTable1); 
      jScrollPane14.getViewport().add(jTable5); 
      jScrollPane12.getViewport().add(jTable6); 
      jScrollPane10.getViewport().add(jTable7); 
      jScrollPane8.getViewport().add(jTextArea4); 
      jScrollPane6.getViewport().add(jTable2); 
      jScrollPane5.getViewport().add(jTable3); 
      jScrollPane4.getViewport().add(jTextArea3); 

      contentPane.add(jToolBar, java.awt.BorderLayout.NORTH); 
      jpDeliverable.add(jScrollPane4, java.awt.BorderLayout.CENTER); 
      jpDeliverable.add(jScrollPane5, java.awt.BorderLayout.SOUTH); 
      jpDeliverable.add(jLabel3, java.awt.BorderLayout.NORTH); 
      jpScope.add(jScrollPane6, java.awt.BorderLayout.SOUTH); 
      jpScope.add(jLabel2, java.awt.BorderLayout.NORTH); 
      jScrollPane7.getViewport().add(jTable4); 
      jpAcceptance.add(jLabel6, java.awt.BorderLayout.NORTH); 
      jScrollPane9.getViewport().add(jTextArea7); 
      jpRisk.add(jScrollPane10, java.awt.BorderLayout.SOUTH); 
      jpRisk.add(jScrollPane9, java.awt.BorderLayout.CENTER); 
      jpRisk.add(jLabel7, java.awt.BorderLayout.NORTH); 
      jScrollPane11.getViewport().add(jTextArea6); 
      jpAssumption.add(jLabel4, java.awt.BorderLayout.NORTH); 
      jpAssumption.add(jScrollPane12, java.awt.BorderLayout.SOUTH); 
      jpAssumption.add(jScrollPane11, java.awt.BorderLayout.CENTER); 
      jScrollPane13.getViewport().add(jTextArea5); 
      jpConstraints.add(jLabel5, java.awt.BorderLayout.NORTH); 
      jpConstraints.add(jScrollPane14, java.awt.BorderLayout.SOUTH); 
      jpConstraints.add(jScrollPane13, java.awt.BorderLayout.CENTER); 
      jScrollPane15.getViewport().add(jTextArea1); 
      jpExclusion.add(jLabel1, java.awt.BorderLayout.NORTH); 
      jpExclusion.add(jScrollPane16, java.awt.BorderLayout.SOUTH); 
      jpExclusion.add(jScrollPane15, java.awt.BorderLayout.CENTER); 

      jpAcceptance.add(jScrollPane8, java.awt.BorderLayout.CENTER); 
      jpAcceptance.add(jScrollPane7, java.awt.BorderLayout.SOUTH); 


      jpCore.add(jpScope, 0); 
      jpCore.add(jpExclusion, 1); 
      jpCore.add(jpDeliverable, 2); 
      jpCore.add(jpAcceptance, 3); 
      jpCore.add(jpConstraints, 4); 
      jpCore.add(jpAssumption, 5); 
      jpCore.add(jpRisk, 6); 
      jSplitPane1.setDividerLocation(230); 
     } 

     /** 
     * File | Exit action performed. 
     * 
     * @param actionEvent ActionEvent 
     */ 
     void jMenuFileExit_actionPerformed(ActionEvent actionEvent) { 
      System.exit(0); 
     } 

     /** 
     * Help | About action performed. 
     * 
     * @param actionEvent ActionEvent 
     */ 
     void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) { 
      Frame1_AboutBox dlg = new Frame1_AboutBox(this); 
      Dimension dlgSize = dlg.getPreferredSize(); 
      Dimension frmSize = getSize(); 
      Point loc = getLocation(); 
      dlg.setLocation((frmSize.width - dlgSize.width)/2 + loc.x, 
          (frmSize.height - dlgSize.height)/2 + loc.y); 
      dlg.setModal(true); 
      dlg.pack(); 
      dlg.setVisible(true); 
     } 

     public void jbRemove_actionPerformed(ActionEvent actionEvent) { 
      jtProjectList.removeCurrentNode(); 
     } 

     public void jbAdd_actionPerformed(ActionEvent actionEvent) { 
      String response = ""; 
      while(response.trim().equals("")){ 
       response = JOptionPane.showInputDialog(this, 
         "Please enter name of node?", 
         "Enter your name", 
         JOptionPane.QUESTION_MESSAGE); 
       //System.out.println(response); 
       if(response != null && response.trim().equals("")) 
        JOptionPane.showMessageDialog(this,"Please enter valid name", 
                "Error",JOptionPane.ERROR_MESSAGE); 
      } 
      jtProjectList.addObject(response); 

     } 
    } 


    class Frame1_jbAdd_actionAdapter implements ActionListener { 
     private Frame1 adaptee; 
     Frame1_jbAdd_actionAdapter(Frame1 adaptee) { 
      this.adaptee = adaptee; 
     } 

     public void actionPerformed(ActionEvent actionEvent) { 
      adaptee.jbAdd_actionPerformed(actionEvent); 
     } 
    } 


    class Frame1_jbRemove_actionAdapter implements ActionListener { 
     private Frame1 adaptee; 
     Frame1_jbRemove_actionAdapter(Frame1 adaptee) { 
      this.adaptee = adaptee; 
     } 

     public void actionPerformed(ActionEvent actionEvent) { 
      adaptee.jbRemove_actionPerformed(actionEvent); 
     } 
    } 


    class Frame1_jMenuFileExit_ActionAdapter implements ActionListener { 
     Frame1 adaptee; 

     Frame1_jMenuFileExit_ActionAdapter(Frame1 adaptee) { 
      this.adaptee = adaptee; 
     } 

     public void actionPerformed(ActionEvent actionEvent) { 
      adaptee.jMenuFileExit_actionPerformed(actionEvent); 
     } 
    } 


    class Frame1_jMenuHelpAbout_ActionAdapter implements ActionListener { 
     Frame1 adaptee; 

     Frame1_jMenuHelpAbout_ActionAdapter(Frame1 adaptee) { 
      this.adaptee = adaptee; 
     } 

     public void actionPerformed(ActionEvent actionEvent) { 
      adaptee.jMenuHelpAbout_actionPerformed(actionEvent); 
     } 
    } 
+3

有至少16'JScollPane'在那裏。如果你在一個(很多)較短的例子中重現,或者至少告訴你使用哪一個窗格不像你期望的那樣工作,那麼調試可能會更容易。 – Mat 2011-03-12 11:01:03

+1

請參閱[sscce](http://sscce.org/)瞭解修剪示例的提示。 – trashgod 2011-03-12 11:06:25

+0

我上傳所有的代碼,因爲它很容易運行它,你可以跟蹤這些Jscrollpane的問題:jScrollPane1 ,jScrollPane16 jScrollPane4 – Amir 2011-03-12 11:41:18

回答

-2

我解決我的問題是小檢討我的代碼:

我只是刪除此行:

jTextArea1.setPreferredSize(new Dimension(280, 60)); 

不要忘了,如果您對JTextArea的滾動面板設置尺寸(任意大小)將不會出現在屏幕上爲你:)

工作完成,享受它......

+0

'JTextArea'的首選大小可以通過在構造函數中使用適當的參數來設置。這是既不必要也不可取當有更好的方法來指定大小時,猜測組件的大小 – 2011-04-17 19:54:54

+0

我想你不明白我的問題?!!如果你爲你的jte設置大小xtArea並將其添加到jscrollpane,您的JSCrollepane無法正常工作! – Amir 2011-04-17 21:35:37

+1

對不起,這只是錯誤的。問題不在於設置大小,而在於JScrollPane沒有正確鏈接到JTextArea。我的答案仍然存在。 – BoffinbraiN 2011-04-18 11:15:55

4

我相信你的代碼是如此凌亂的原因是因爲它是由一些GUI構建器自動生成。由於大量組件和界面的複雜性,您可能需要手動編寫它。這裏必須有某種模式,可以利用它來減少代碼量並使其更易於理解和維護。

除此之外,我認爲這個問題可能與組件添加到JScrollPane的方式有關。您使用

jScrollPaneX.getViewport().add(comp);

這是不分配以滾動窗格的常用方法。的標準方法是通過選擇在構建時間視分量,如通過調用

jScrollPanex = new JScrollPane(comp)

,或者如果以後需要設置,叫

jScrollPaneX.setViewportView(comp)

(在內部,這個調用getViewport().setView(view)這是類似的,但不一樣的,你在做它的方式)