java - JLabel 将项目推出屏幕

标签 java swing netbeans jlabel

我正在使用 NetBeans gui 构建器为应用程序设计一个 gui,有一次我想要一个带有进度条、取消按钮和弹出标签的小型 JFrame。标签的文本(居中)有时会显示有点长的文件名,不适合屏幕,但它不仅仅是不显示溢出的文本,而是占用进度条并拉伸(stretch)它,以便它的右侧离开屏幕。我认为这与 NetBeans 使用 GroupLayout 有关,但我不确定。这是生成的构造函数代码:

progressLabel = new javax.swing.JLabel();
cancelBtn = new javax.swing.JButton();
progressBar = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

progressLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

cancelBtn.setText("Cancel");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
            .addComponent(progressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cancelBtn)
        .addGap(18, 18, 18))
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelBtn))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(progressLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
);

pack();   

最佳答案

作为建议(这当然可能是您的答案),我会将进度对话框窗口中显示的路径压缩为进度栏所需最大宽度的最大宽度(以字符为单位),例如:

C:\Users...\Documents\Helicoil Charts.docx

在大多数情况下,向用户显示完整路径并不是真正的要求,但如果是这样,那么例如,如果用户将鼠标移到 JLabel 上,则完整路径长度可以显示为工具提示:

jLabel1.setToolTip(myFullPathVariable);

要压缩文件路径,您可以使用以下方法:

/**
* Compact a file path into a given number of characters. <u>Similar</u> to the
* Windows Win32 API PathCompactPathExA API function.
* @param path (String) The Path to compact.
* @param charLimit (Integer) The maximum number of characters the path is allowed to be.
* @return (String) The compacted Path.
*/
public static String CompactPathBC(String path, int charLimit) {
    if (path.length() <= charLimit) { return path; }

    char shortPathArray[] = new char [charLimit];
    char pathArray [] = path.toCharArray();
    char ellipseArray [] = "...".toCharArray();

    int pathindex = pathArray.length - 1 ;
    int shortpathindex = charLimit - 1;

    // fill the array from the end
    int i = 0;
    for (; i < charLimit  ; i++) {
        if (pathArray[pathindex - i] != '/' && pathArray[pathindex - i] != '\\') {
            shortPathArray[shortpathindex - i] = pathArray[pathindex - i] ;
        }
        else { break; }
    }
    // check how much space is left
    int free = charLimit - i;

    if (free < "...".length()) {
        // fill the beginning with ellipse
        System.arraycopy(ellipseArray, 0, shortPathArray, 0, ellipseArray.length);
    }
    else {
        // fill the beginning with path and leave room for the ellipse
        int j = 0;
        for(; j + ellipseArray.length < free; j++) { shortPathArray[j] = pathArray[j] ; }
        // ... add the ellipse
        for(int k = 0; j + k < free;k++) { shortPathArray[j + k] = ellipseArray[k] ; }
    }
    return new String(shortPathArray);
}

通过使用字体指标,您还可以创建一种方法来使路径达到特定的最大宽度(以像素为单位)。

关于java - JLabel 将项目推出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35530523/

相关文章:

java - 在 Netbeans 中生成 WSDL 时出错

java - 如何构建语法荧光笔和缩进?

zend-framework - Netbeans 中的 Zend 编码标准

java - Selenium:无法切换窗口

java - 计算输入中数字的幂

java - 将 Fragment 置于堆栈顶部

java - JFileChooser 返回错误的文件名?

java - JFrame 在图标化时监听 KeyEvent

java - 为 JSlider 中的每次更改获取值(value)

java - 如何在JavaFX中创建后台服务