JPanels 中的 JLabels 大小问题

发布于 2024-11-16 16:20:16 字数 1962 浏览 3 评论 0原文

我目前正在编写一个 Java 应用程序来显示数据库中的想法列表。每个想法都有一个标题和一个数字,我希望将其与类似的想法一起显示在列中。正如您从下图中看到的,我遇到了一些包装问题。

手头问题的一小部分

我想要的是一种以编程方式切断长想法名称尾部的方法以适合分配的空间。所以:

如果列宽仅为

##########################

A real long idea titl

将成为

A very long i... #123

代码

public class IdeaBar extends JPanel implements MouseListener {

private SortingColumn column;
private Idea idea;

private JPanel headPanel;
private JPanel bodyPanel;
private boolean isShrunk;
private boolean mouseToggle;

public IdeaBar(Idea id) {
    
    this.idea = id;
            
    this.setDoubleBuffered(true);

    setLayout(new MigLayout("insets 0, hidemode 2", "[grow]", "[32px:32px:32px]0[]"));

    // Head Panel
    headPanel = new JPanel();
    headPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    headPanel.addMouseListener(this);
    headPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
            null, null));
    headPanel.setBackground(Color.GRAY);
    headPanel.setBackground(idea.getStatus().getColor());
    headPanel.setLayout(new BorderLayout(0, 0));
    
    // Head Panel Labels
    JLabel titleLabel = new JLabel("" + idea.getTitle());
    titleLabel.setFont(new Font("Dialog", Font.PLAIN, 20));
    titleLabel.setForeground(idea.getStatus().getTextColor());
    headPanel.add(titleLabel, BorderLayout.WEST);

    JLabel numLabel = new JLabel("New label");
    numLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    numLabel.setForeground(idea.getStatus().getTextColor());
    numLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
    numLabel.setText("#" + idea.getNumber());
    headPanel.add(numLabel, BorderLayout.EAST);
    add(headPanel, "cell 0 0, grow, wrap");

    // Body Panel
    bodyPanel= new IdeaBarBodyPanel(idea);
    this.add(bodyPanel, "grow");

    this.shrink();

}

I am currently writing a Java application to display a list of ideas from a database. Each Idea has a title and a number which i would like to display in a column along with similar ideas. As you can see from the picture below, i am having a bit of a packaging issue.

A small clipping of the problem at hand

What i would like is a way to programatically cut off the tail of a long idea name to fit in the allotted space. So:

if the column width is only

########################

A really long idea titl

would become

A really long i... #123

Code

public class IdeaBar extends JPanel implements MouseListener {

private SortingColumn column;
private Idea idea;

private JPanel headPanel;
private JPanel bodyPanel;
private boolean isShrunk;
private boolean mouseToggle;

public IdeaBar(Idea id) {
    
    this.idea = id;
            
    this.setDoubleBuffered(true);

    setLayout(new MigLayout("insets 0, hidemode 2", "[grow]", "[32px:32px:32px]0[]"));

    // Head Panel
    headPanel = new JPanel();
    headPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    headPanel.addMouseListener(this);
    headPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
            null, null));
    headPanel.setBackground(Color.GRAY);
    headPanel.setBackground(idea.getStatus().getColor());
    headPanel.setLayout(new BorderLayout(0, 0));
    
    // Head Panel Labels
    JLabel titleLabel = new JLabel("" + idea.getTitle());
    titleLabel.setFont(new Font("Dialog", Font.PLAIN, 20));
    titleLabel.setForeground(idea.getStatus().getTextColor());
    headPanel.add(titleLabel, BorderLayout.WEST);

    JLabel numLabel = new JLabel("New label");
    numLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    numLabel.setForeground(idea.getStatus().getTextColor());
    numLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
    numLabel.setText("#" + idea.getNumber());
    headPanel.add(numLabel, BorderLayout.EAST);
    add(headPanel, "cell 0 0, grow, wrap");

    // Body Panel
    bodyPanel= new IdeaBarBodyPanel(idea);
    this.add(bodyPanel, "grow");

    this.shrink();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-11-23 16:20:16

尝试将 titleLabel 添加到 CENTER 而不是 WEST。因此,应调整其大小以填充剩余空间并自动将省略号添加到文本中。请注意,这可能不适用于每个系统,请参见此处:Java JLabel/JButton:在某些系统上我得到“...”(省略号),而在某些系统上我没有。我怎样才能强制禁用省略号?

Try and add the titleLabel to CENTER instead of WEST. Thus it should be resized to fill the space left and automatically add the ellipis to the text. Note that this might not work on every system, see here: Java JLabel/JButton: on some systems I get "..." (an ellipsis) and on some systems I don't. how can I force to disable the ellipsis at all?

记忆消瘦 2024-11-23 16:20:16

通用代码。查看字符串是否比您可以显示的长度长。如果是,它会删除无法显示的部分并添加...


int colLength = 20;

public void addEntry(String entry)
{
    if(entry.length() > colLength)
    {
        entry = entry.substring(0,colLength -3) + "...";
    }
    add(entry)
}

General code. Sees if the string is longer than what you can display. If it is, it cuts of the part that cant be displayed and adds the ...


int colLength = 20;

public void addEntry(String entry)
{
    if(entry.length() > colLength)
    {
        entry = entry.substring(0,colLength -3) + "...";
    }
    add(entry)
}

无尽的现实 2024-11-23 16:20:16

好吧,最后,我决定绘制自己的组件。无论如何,我想仔细控制背景,所以这对我来说是最好的解决方案。

给你们俩投票,因为你们的回答对帮助我找到方向很有帮助!

Well, in the end, i just decided to paint my own component. I want careful control of the background anyway, so this was the best solution for me.

Upvotes to both of you because your answers were instrumental in helping me find my way!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文