Dee*_*pak 12 java swing jbutton
是否有可能在JButtons中实现文本的自动换行?我在运行时创建的动态按钮很少.我想在按钮上放置自动换行功能,这样我就可以在按钮上看到更好的测试.有可能吗?
And*_*son 24
此示例使用Java的内置CSS呈现功能来执行确定何时执行换行的"繁重工作".它使用a JLabel,但相同的原则适用于将呈现HTML的任何组件.
import javax.swing.*;
class FixedWidthText {
public static void showLabel(int width, String units) {
String content1 = "<html>"
+ "<body style='background-color: white; width: ";
String content2 = "'>"
+ "<h1>Fixed Width</h1>"
+ "<p>Body width fixed at ";
String content3
= " using CSS. "
+ "Java's HTML"
+ " support includes support"
+ " for basic CSS.</p>";
final String content = content1 + width + units
+ content2 + width + units + content3;
Runnable r = () -> {
JLabel label = new JLabel(content);
JOptionPane.showMessageDialog(null, label);
};
SwingUtilities.invokeLater(r);
}
public static void main(String[] args) {
showLabel(160, "px");
showLabel(200, "px");
showLabel(50, "%");
}
}
Run Code Online (Sandbox Code Playgroud)



Rom*_*eau 12
使用HTML ...
button.setText("<html><center>"+"This is a"+"<br>"+"swing button"+"</center></html>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17500 次 |
| 最近记录: |