4

我正在使用html 标签在按钮上显示参数及其值。有些按钮包含一个参数,有些按钮包含 2 或 3 个参数。“/”用于分隔值。名称和值之间有一个空行。但在长值的情况下,我想使用该空行来显示值,如“ Table H/V ”按钮。

我正在尝试使用值字符串的长度来确定是否需要空行。它不能正常工作,因为字符编号不反映该字符串的绘图长度。按钮大小是固定的。

我在问我怎么知道什么时候应该再有一个代表那个空行的“< br>”?或者我怎么知道值字符串何时会被换行?

这是我的代码:

 private static String getBtnDisplayStr(String name, String value)
    {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("<html><center><b>");
        sBuilder.append(name);
        if(value.length() <= 12)  //add one empty line for short value string
        {
            sBuilder.append("</center></b><br><br><font size=\"2\">");
        }
        else
        {
            sBuilder.append("</center></b><br><font size=\"2\">");
        }
        sBuilder.append(value);
        sBuilder.append("</font></html>");

        return sBuilder.toString();
    }

这是按钮: 在此处输入图像描述

4

2 回答 2

3
  • 默认情况下,没有理由PreferredSize计算Html <=3.2Swing JComponents

  • LayoutManager例如 GridLayout 从最大元素计算屏幕尺寸

在此处输入图像描述

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class HtmlAndJButton {

    final String buttonText = " Whatever words, <br> but nothing wise";
    final String buttonText1 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, ";
    final String buttonText2 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, <br> plus 2nd. line,";
    private JButton btn1 = new JButton("Toggle");
    private JButton button = new JButton(buttonText);
    private JButton button1 = new JButton("Toggle");
    private JButton button2 = new JButton("Toggle");

    public HtmlAndJButton() {
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setText("<html><font color=" + (button.isEnabled()
                        ? "blue" : "red") + ">" + buttonText + "</font></html>");
                button.setEnabled(!button.isEnabled());
                button1.setText("<html><font color=" + (button1.isEnabled()
                        ? "red" : "green") + ">" + buttonText1 + "</font></html>");
                button1.setEnabled(!button1.isEnabled());
                button2.setText("<html><font color=" + (button2.isEnabled()
                        ? "green" : "yellow") + ">" + buttonText2 + "</font></html>");
                button2.setEnabled(!button2.isEnabled());
            }
        });
        button.setText("<html><font color=red>" + buttonText + "</font></html>");
        button1.setText("<html><font color=green>" + buttonText1 + "</font></html>");
        button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>");
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button);
        f.add(button1);
        f.add(button2);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                HtmlAndJButton t = new HtmlAndJButton();
            }
        });
    }
}
于 2012-12-20T20:29:21.590 回答
0

设置按钮上的文本,然后检查按钮的“首选宽度”。如果太大,请相应地调整文本。

这是上面两个按钮的示例:

private static int BUTTON_WIDTH = 90;  // Adjust this as necessary

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel pnl = new JPanel();

    JButton btn1 = new JButton(getBtnDisplayStr("Table H/V",null,"909.0 / 500.0"));
    if (btn1.getPreferredSize().width > BUTTON_WIDTH) {
        btn1.setText(getBtnDisplayStr("Table H/V", "909.0 /", "500.0"));
    }
    JButton btn2 = new JButton(getBtnDisplayStr("Acq/ISD",null,"1 / 1"));
    if (btn2.getPreferredSize().width > BUTTON_WIDTH) {
        btn2.setText(getBtnDisplayStr("Acq/ISD","1 /","1"));
    }

    pnl.add(btn1,BorderLayout.LINE_START);
    pnl.add(btn2,BorderLayout.LINE_END);

    frame.add(pnl);
    frame.pack();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

private static String getBtnDisplayStr(String name, String valueLine1, String valueLine2) {
    StringBuilder sBuilder = new StringBuilder();
    sBuilder.append("<html><center><b>");
    sBuilder.append(name);
    sBuilder.append("</b><br/>");
    if (valueLine1 != null) {
        sBuilder.append("<font size=\"2\">");
        sBuilder.append(valueLine1);
        sBuilder.append("</font>");
    }
    sBuilder.append("<br/>");
    if (valueLine2 != null) {
        sBuilder.append("<font size=\"2\">");
        sBuilder.append(valueLine2);
        sBuilder.append("</font>");
    }
    sBuilder.append("<br/>");

    sBuilder.append("</center></html>");

    return sBuilder.toString();
}
于 2012-12-20T20:03:36.470 回答