我相信我们所有人都在Facebook状态(或其他地方)看过省略号,然后点击"显示更多"并且只有另外两个字符左右.我猜这是因为懒惰的编程,因为肯定有一种理想的方法.
我将细长的字符[iIl1]称为"半字符",但是当它们几乎不隐藏任何字符时,这并不会使省略号看起来很傻.
有理想的方法吗?这是我的:
/**
* Return a string with a maximum length of <code>length</code> characters.
* If there are more than <code>length</code> characters, then string ends with an ellipsis ("...").
*
* @param text
* @param length
* @return
*/
public static String ellipsis(final String text, int length)
{
// The letters [iIl1] are slim enough to only count as half a character.
length += Math.ceil(text.replaceAll("[^iIl]", "").length() / 2.0d);
if (text.length() > length)
{
return text.substring(0, length - …Run Code Online (Sandbox Code Playgroud) 我在Java Swing中创建了一个应用程序.我提供了从菜单中更改应用程序外观的选项,但是在添加新选项卡后JTabbedPane,它没有使用新的外观进行更新.
我已经使用过这段代码:
Window windows[] = Frame.getWindows();
for(Window window : windows) {
SwingUtilities.updateComponentTreeUI(window);
}
Run Code Online (Sandbox Code Playgroud) 我想减少JButton的垂直大小.以下代码适用于K> 1,但我似乎无法减小大小.有什么建议?
JButton button = /* ... get button here ... */
Dimension d = button.getPreferredSize();
d.setSize(d.getWidth(), d.getHeight()*K);
button.setPreferredSize(d);
Run Code Online (Sandbox Code Playgroud)
编辑:我正在使用JavaBuilders + MigLayout.看起来我必须做button.setMaxSize(d);而不是setPreferredSize(),不知道为什么.
我遇到了字体大小的问题,它比它应该小得多,甚至增加size参数也没什么区别.
首选大小是为了表明它不是缩小它的大小,文本是随机的,长的是显示文本的小小.它是所有字体,不仅仅是sans-serif,但如果我使用系统默认字体,这是正常的.我还重新启动了我的电脑,以确保它没有将字体加载到缓存错误.
JLabel time = new JLabel("sdfghgfdcfghgvghbvhgvghhvghgvghhbhbhb");
time.setFont(new Font("sans-serif", 13, Font.ITALIC));
time.setPreferredSize(new Dimension(50, 50));
panel.add(time);
Run Code Online (Sandbox Code Playgroud)
panel只是一个具有正常flowlayout的JPanel.
这是文字图片:
文字高约5像素.谢谢
我有一个java应用程序 - 一个计算器.我想通过调整应用程序窗口的大小来动态调整按钮的字体大小.怎么实现呢?
我的想法是使用ComponentEvents.我有应用程序窗口的初始大小和初始字体的大小.我想根据按钮的大小更改字体大小,受窗口大小更改的影响.问题是如何在overriden方法中使用[初始窗口大小]/[初始字体大小]的比例?每种字体的比例都不同.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class Main extends JFrame {
public Main() {
super("Test");
JPanel cPane = (JPanel) getContentPane();
cPane.setLayout(new BorderLayout());
MyButton sampleButton = new MyButton("Sample text");
sampleButton.setFont(new Font("Sans Serif", Font.PLAIN, 20));
MyButton a, b, c, d;
a = new MyButton("a");
b = new MyButton("b");
c = new MyButton("c");
d = new MyButton("d");
cPane.add(a, BorderLayout.PAGE_START);
cPane.add(b, BorderLayout.PAGE_END);
cPane.add(c, BorderLayout.LINE_START);
cPane.add(d, BorderLayout.LINE_END);
cPane.add(sampleButton, BorderLayout.CENTER);
setSize(300, 200);
setResizable(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void …Run Code Online (Sandbox Code Playgroud)