【发布时间】:2016-06-16 01:36:19
【问题描述】:
我已经写在Netbeans的8.1(Java版本1.8.0_65),它需要发现的文件列表,并将其写入到一个JTextArea一个简单的程序。这工作得很好,当有许多行写入的JTextArea(即任何超过500行左右),在滚动条消失拇指除外。我看到了 post 所描述的确切问题。
这似乎是 Nimbus L&F 的 known issue,人们已经发布了解决方法,如上面的帖子中所述,他们说要解决此问题,只需添加以下行:
UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
我遇到的问题是我将这行代码放在哪里?我已经尝试在创建 jScrollPanel 之前将其添加到 initComponents(如下面的代码所示)。在检查 Nimbus 是否可用时,我也在 if 语句之后尝试过:
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
我清楚地挥舞着刚刚约尽量坚持它不理解我在做什么不同的地方,并且,没有惊喜,没有工作。 P>
有人可以帮我确定这个解决方法的代码行应该去哪里吗?
我的代码:
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");
}
}
}
【问题讨论】:
-
“..它需要发现的文件列表,并将其写入到一个JTextArea。” i>的使用
JList代替。也看到了File Browser GUI的文件可能会提供一些提示的浏览器(例如漂亮的渲染器)。 -
谢谢。我固定了悬挂的封闭支架,并且还将看看使用 jlist。
-
您可能需要检查
Scrollbar.minimumThumbSize和ScrollBar.minimumThumbSize的密钥之间的差异。
标签: java swing textarea nimbus scrollpane