JLabel 将项目推出屏幕
JLabel pushing items off screen
我正在使用 NetBeans gui 构建器为应用程序设计一个 gui,有一点我想要一个带有进度条、取消按钮和弹出标签的小型 JFrame。标签的文本(居中)有时会显示一些不适合屏幕的长文件名,但它不是不显示溢出的文本,而是占用进度条并拉伸它,以便它的右侧离开屏幕。我认为这与使用 GroupLayout 的 NetBeans 有关,但我不确定。这是生成的构造函数代码:
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();
作为一个建议(这当然可能是你的答案)我会压缩进度对话框中显示的路径 window 到你所需的最大宽度的最大宽度(以字符为单位)进度条,例如:
C:\Users...\Documents\Helicoil Charts.docx
在大多数情况下,向用户显示完整路径并不是真正的要求,但是如果是这样,那么如果用户将鼠标移到 his/her 上,则可以将完整路径长度显示为工具提示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);
}
通过使用 Font Metrics,您还可以创建一种方法,使您的路径达到特定的最大宽度(以像素为单位)。
我正在使用 NetBeans gui 构建器为应用程序设计一个 gui,有一点我想要一个带有进度条、取消按钮和弹出标签的小型 JFrame。标签的文本(居中)有时会显示一些不适合屏幕的长文件名,但它不是不显示溢出的文本,而是占用进度条并拉伸它,以便它的右侧离开屏幕。我认为这与使用 GroupLayout 的 NetBeans 有关,但我不确定。这是生成的构造函数代码:
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();
作为一个建议(这当然可能是你的答案)我会压缩进度对话框中显示的路径 window 到你所需的最大宽度的最大宽度(以字符为单位)进度条,例如:
C:\Users...\Documents\Helicoil Charts.docx
在大多数情况下,向用户显示完整路径并不是真正的要求,但是如果是这样,那么如果用户将鼠标移到 his/her 上,则可以将完整路径长度显示为工具提示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);
}
通过使用 Font Metrics,您还可以创建一种方法,使您的路径达到特定的最大宽度(以像素为单位)。