2016-03-02 20 views
1

我已經在Netbeans 8.1(Java版本1.8.0_65)中編寫了一個簡單的程序,其中它發現了一個已發現文件的列表並將它們寫入了一個jTextArea。這工作正常,除了有很多行寫入jTextArea(即超過500行左右)時,滾動條中的拇指消失。我看到這個post描述的確切問題。當TextArea有很多行時,JScrollPane不會顯示Nimbus L&F中的縮略圖

這似乎是一個known issue有雨雲大號& F和鄉親已經發布了這一解決方法如上,他們說,要解決這個問題,只是增加一行在後描述:

UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 

問題我的確是在哪裏放這行代碼?我已經嘗試在創建jScrollPanel之前將它添加到initComponents中(如下面的代碼所示)。

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 

我清楚地揮舞着剛剛約儘量堅持它不理解我在做什麼不同的地方,並且,沒有:檢查時,如果雨雲可我也已經試過了if語句之後驚喜,不工作。

有人可以幫我確定這種解決方法行代碼應該去哪裏?

我的代碼:

public class ViewFiles extends javax.swing.JFrame { 

    /** 
    * Creates new form ViewFiles 
    */ 
    public ViewFiles() { 
     initComponents(); 

    } 

    public ViewFiles(ArrayList<DiscoveredFile> files){ 
     initComponents(); 
     discoveredFiles = files; 
     displayFiles(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     javax.swing.UIManager.getLookAndFeelDefaults().put("Scrollbar.minimumThumbSize", new Dimension(30,30)); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     viewFilesTextArea = new javax.swing.JTextArea(); 
     viewFilesCloseButton = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     viewFilesTextArea.setColumns(20); 
     viewFilesTextArea.setRows(5); 
     jScrollPane1.setViewportView(viewFilesTextArea); 

     viewFilesCloseButton.setText("Close"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE) 
       .addContainerGap()) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(viewFilesCloseButton) 
       .addGap(29, 29, 29)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE) 
       .addGap(18, 18, 18) 
       .addComponent(viewFilesCloseButton) 
       .addContainerGap()) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 

     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(ViewFiles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(ViewFiles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(ViewFiles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(ViewFiles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new ViewFiles().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JButton viewFilesCloseButton; 
    private javax.swing.JTextArea viewFilesTextArea; 
    // End of variables declaration     
    private ArrayList<DiscoveredFile> discoveredFiles; 

    public void displayFiles() { 
     for (DiscoveredFile file : discoveredFiles){ 
      viewFilesTextArea.append(file.getFullPath() + "\n"); 
     }  
    } 
} 
+0

* *使用'JList'代替。另請參閱[File Browser GUI](http://codereview.stackexchange.com/q/4446/7784),瞭解可能提供一些提示的文件瀏覽器(例如,良好的渲染器)。 –

+0

謝謝。我固定了懸掛的括號,並且也會使用jlist。 – Monty

+0

您可能需要查看「Scrollbar.minimumThumbSize」和「ScrollBar.minimumThumbSize」鍵之間的差異。 – aterai

回答

1

這裏是另一種方法: 「..它需要發現的文件列表,並將其寫入到一個JTextArea」

UIDefaults def = new UIDefaults(); 
def.put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 
jScrollPane1 = new JScrollPane(); 
jScrollPane1.getVerticalScrollBar().putClientProperty("Nimbus.Overrides", def); 
import java.awt.*; 
import java.util.*; 
import javax.swing.*; 

public class ViewFiles2 extends JFrame { 
    private JScrollPane jScrollPane1; 
    private JButton viewFilesCloseButton; 
    private JTextArea viewFilesTextArea; 
    public ViewFiles2() { 
    initComponents(); 
    displayFiles(); 
    } 
    private void initComponents() { 
     //NG?: UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 
     //OK?: UIManager.getDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 

    jScrollPane1 = new JScrollPane(); 

    UIDefaults def = new UIDefaults(); 
    def.put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 
    jScrollPane1.getVerticalScrollBar().putClientProperty("Nimbus.Overrides", def); 

    viewFilesTextArea = new JTextArea(20, 5); 
    viewFilesCloseButton = new JButton("Close"); 
    jScrollPane1.setViewportView(viewFilesTextArea); 

    GroupLayout layout = new GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE) 
       .addContainerGap()) 
     .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(viewFilesCloseButton) 
       .addGap(29, 29, 29)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE) 
       .addGap(18, 18, 18) 
       .addComponent(viewFilesCloseButton) 
       .addContainerGap()) 
    ); 

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    pack(); 
    } 
    public void displayFiles() { 
    viewFilesTextArea.setText(String.join("\n", Collections.nCopies(500, "aaaaaaaaaaaaa"))); 
    } 
    public static void main(String[] args) { 
    try { 
     for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
     if ("Nimbus".equals(info.getName())) { 
      UIManager.setLookAndFeel(info.getClassName()); 
      break; 
     } 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    //OK?: UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 
    //NG?: UIManager.getDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30)); 

    EventQueue.invokeLater(() -> { 
     new ViewFiles2().setVisible(true); 
    }); 
    } 
} 
+0

謝謝!這解決了我的問題。在Netbeans中,我添加了您在JScrollPane的Pre-Init代碼中提供的4行代碼,因爲它受NetBeans保護。之後,像冠軍一樣工作。 – Monty