gpt4 book ai didi

java - 当 TextArea 有很多行时,JScrollPane 在 Nimbus L&F 中不显示拇指

转载 作者:行者123 更新时间:2023-11-30 07:33:16 25 4
gpt4 key购买 nike

我在 Netbeans 8.1(Java 版本 1.8.0_65)中编写了一个简单的程序,它获取已发现文件的列表并将它们写入 jTextArea。这工作正常,除非有很多行写入 jTextArea(即超过 500 行左右),滚动条中的拇指消失。我看到了这个 post 的确切问题描述。

这似乎是 known issue Nimbus L&F 和人们已经发布了解决方法,如上面的帖子中所述,他们说要解决此问题只需添加以下行:

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;
}
}

我显然只是在不理解我在做什么的情况下试图将其粘贴到不同的地方,毫不奇怪,这不起作用。

有人可以帮助我确定此解决方法代码行应该放在哪里吗?

我的代码:

    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");
}
}
}

最佳答案

这是另一种方法:

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);
});
}
}

关于java - 当 TextArea 有很多行时,JScrollPane 在 Nimbus L&F 中不显示拇指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com