1

我对 jlabel 有一点问题。当标签显示对于屏幕来说太大的双精度时,我希望有一个滚动条来查看它们。我刚刚在整个面板中添加了一个滚动条,但是当显示超长双精度时,它不会检查。

这是我的代码

 public class OverviewPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JLabel textNoNodes = new JLabel();
    private JLabel textNoEdges = new JLabel();
    private JLabel textInitial = new JLabel();
    private JLabel textTargets = new JLabel();
    private JLabel textFilename = new JLabel();
    private JLabel textProbabilityModelCheck = new JLabel();
    private JLabel textProbabilityCounterExample = new JLabel();
    private JLabel textNoSteps = new JLabel();

    public OverviewPanel() {
        super(new SpringLayout());
        addRow("Number states", textNoNodes);
        addRow("Number edges", textNoEdges);
        addRow("Initial", textInitial);
        addRow("Targets", textTargets);
        addRow("Filename", textFilename);
        addRow("Prob. model check", textProbabilityModelCheck);
        addRow("Prob. counter example", textProbabilityCounterExample);
        addRow("Number steps", textNoSteps);
        SpringUtilities.makeCompactGrid(this, 8, 2, // rows, cols
                6, 6, // initX, initY
                6, 6); // xPad, yPad
        setDefault();
    }

    private void addRow(String text, JComponent component) {
        JLabel l = new JLabel(text);
        add(l);
        l.setLabelFor(component);       
        add(component);
    }
   ...
   }
4

3 回答 3

3

使用pack(),“导致Window其大小以适合其子组件的首选大小和布局。” sscce可能会有所帮助。

附录:一个实例DecimalFormat可能有助于控制极端的尺寸变化。

于 2012-01-10T13:22:45.970 回答
2

恕我直言,我会使用修改后的JTextArea(即禁用、包装和不透明)。无需滚动、调整大小或字体度量计算

于 2012-01-10T15:26:32.443 回答
1

我不确定,因为我不知道springlayout,反正我认为你必须将标签的大小设置为文本的大小。要获得它,您可以使用:

FontMetrics fm = someComponent.getFontMetrics(someFont);
  int width = fm.stringWidth(someString);

也许会增加一些额外的空间。然后使用实际宽度的标签,容器应该滚动......

当然我说的是 minimumSize 或者它是无效的......

于 2012-01-10T13:20:24.880 回答