我正在使用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();
答案 0 :(得分:2)
作为一个建议(这当然可能是你的答案)我会将进度对话框窗口中显示的路径压缩到进度条所需最大宽度的最大宽度(以字符为单位),例如:
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);
}
通过使用字体指标,您还可以创建一种方法,使路径达到特定的最大宽度(以像素为单位)。