📄 multilinelabel.java
字号:
/* HEDAER */
package com.sshtools.ui.swing;
import java.util.StringTokenizer;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Swing component that takes a string, splits it up into lines based on the
* newline character and displays each line.
*
* @author $Author: lee $
* @version $Revision: 1.3 $
*/
public class MultilineLabel
extends JPanel {
// Private instance variables
private GridBagConstraints constraints;
private String text;
/**
* Creates a new MultilineLabel object.
*/
public MultilineLabel() {
this("");
}
/**
* Creates a new MultilineLabel object.
*
* @param text
*/
public MultilineLabel(String text) {
super(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.fill = GridBagConstraints.NONE;
setText(text);
}
/**
* Set the font
*
* @param f font
*/
public void setFont(Font f) {
super.setFont(f);
for (int i = 0; i < getComponentCount(); i++) {
getComponent(i).setFont(f);
}
}
/**
* Set the font
*
* @param text
*/
public void setText(String text) {
this.text = text;
removeAll();
StringTokenizer tok = new StringTokenizer(text, "\n");
constraints.weighty = 0.0;
constraints.weightx = 1.0;
while (tok.hasMoreTokens()) {
String t = tok.nextToken();
if (!tok.hasMoreTokens()) {
constraints.weighty = 1.0;
}
UIUtil.jGridBagAdd(this, new JLabel(t), constraints,
GridBagConstraints.REMAINDER);
}
revalidate();
repaint();
}
/**
* Get the text
*
* @return text
*/
public String getText() {
return text;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -