如何在JPanel中创建垂直的TitledBorder(javax swing) [英] How to create vertical TitledBorder in JPanel (javax swing)

查看:134
本文介绍了如何在JPanel中创建垂直的TitledBorder(javax swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在JPanel中创建垂直的TitledBorder.

I'm trying to figure out how to create a vertical TitledBorder in a JPanel.

我有这种情况:

我想垂直放置执行机构st ...",以便用户阅读.

I'd like to have "Actuators st..." placed vertically, so user can read it.

有没有办法做到这一点,或者我应该实现自己的自定义JPanel& TitledBorder?

Is there a way to do it, or should I implement my own customized JPanel & TitledBorder?

推荐答案

As shown in How to Use Borders, you can create a compound border using an empty border and a titled border.

附录:作为替代,您可以使用边框的getMinimumSize()方法来确保标题可见.另请参阅与此相关的问题与解答.

Addendum: As an alternative, you can use the border's getMinimumSize() method to ensure that the title is visible. See also this related Q&A.

f.add(createPanel("Actuator status"), BorderLayout.WEST);
f.add(createPanel("Indicator result"), BorderLayout.EAST);
...
private Box createPanel(String s) {
    Box box = new Box(BoxLayout.Y_AXIS);
    TitledBorder title = BorderFactory.createTitledBorder(null, s,
        TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
    box.setBorder(title);
    for (int i = 0; i < 6; i++) {
        JButton b = new JButton(null, UIManager.getIcon("html.pendingImage"));
        b.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box.add(b);
    }
    box.validate();
    Dimension db = box.getPreferredSize();
    int max = Math.max(title.getMinimumSize(box).width, db.width);
    box.setPreferredSize(new Dimension(max, db.height));
    return box;
}

这篇关于如何在JPanel中创建垂直的TitledBorder(javax swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆